Skip to content

Commit 16bf6b9

Browse files
committed
Detect iframe instead
1 parent baf378d commit 16bf6b9

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

packages/gitbook/src/components/AdminToolbar/AdminToolbar.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { GitBookSiteContext } from '@/lib/context';
22
import { Icon } from '@gitbook/icons';
33
import React from 'react';
44

5-
import { isPreviewRequest } from '@/lib/preview';
65
import { tcls } from '@/lib/tailwind';
76
import { DateRelative } from '../primitives';
7+
import { IframeWrapper } from './IframeWrapper';
88
import { RefreshChangeRequestButton } from './RefreshChangeRequestButton';
99
import { Toolbar, ToolbarBody, ToolbarButton, ToolbarButtonGroups } from './Toolbar';
1010

@@ -47,23 +47,20 @@ function ToolbarLayout(props: { children: React.ReactNode }) {
4747
export async function AdminToolbar(props: AdminToolbarProps) {
4848
const { context } = props;
4949

50-
// Get the current URL from the linker (which accounts for both static and dynamic routes)
51-
const currentURL = new URL(context.linker.toAbsoluteURL('/'));
52-
53-
// Check if this url is opened within the GitBook app's in-editor preview tab.
54-
const isInAppPreview = isPreviewRequest(currentURL);
55-
56-
// Don't show admin toolbar for preview requests
57-
if (isInAppPreview) {
58-
return null;
59-
}
60-
6150
if (context.changeRequest) {
62-
return <ChangeRequestToolbar context={context} />;
51+
return (
52+
<IframeWrapper>
53+
<ChangeRequestToolbar context={context} />
54+
</IframeWrapper>
55+
);
6356
}
6457

6558
if (context.revisionId !== context.space.revision) {
66-
return <RevisionToolbar context={context} />;
59+
return (
60+
<IframeWrapper>
61+
<RevisionToolbar context={context} />
62+
</IframeWrapper>
63+
);
6764
}
6865

6966
return null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use client';
2+
3+
import React from 'react';
4+
5+
interface IframeWrapperProps {
6+
children: React.ReactNode;
7+
}
8+
9+
/**
10+
* Client component that detects if we're in an iframe and conditionally renders children
11+
*/
12+
export function IframeWrapper({ children }: IframeWrapperProps) {
13+
const [isInIframe, setIsInIframe] = React.useState(false);
14+
15+
React.useEffect(() => {
16+
// Check if we're running inside an iframe
17+
const inIframe = window !== window.parent;
18+
setIsInIframe(inIframe);
19+
}, []);
20+
21+
// Don't render children if we're in an iframe (GitBook app preview)
22+
if (isInIframe) {
23+
return null;
24+
}
25+
26+
return children;
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './AdminToolbar';
2+
export * from './IframeWrapper';

0 commit comments

Comments
 (0)