Skip to content

Commit d7596bf

Browse files
Don't show toolbar when rendering within GitBook app preview (#3635)
1 parent aea5eb1 commit d7596bf

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { GitBookSiteContext } from '@/lib/context';
22
import { Icon } from '@gitbook/icons';
3-
import { headers } from 'next/headers';
43
import React from 'react';
54

65
import { tcls } from '@/lib/tailwind';
7-
86
import { DateRelative } from '../primitives';
7+
import { IframeWrapper } from './IframeWrapper';
98
import { RefreshChangeRequestButton } from './RefreshChangeRequestButton';
109
import { Toolbar, ToolbarBody, ToolbarButton, ToolbarButtonGroups } from './Toolbar';
1110

@@ -47,19 +46,21 @@ function ToolbarLayout(props: { children: React.ReactNode }) {
4746
*/
4847
export async function AdminToolbar(props: AdminToolbarProps) {
4948
const { context } = props;
50-
const mode = (await headers()).get('x-gitbook-mode');
51-
52-
if (mode === 'multi-id') {
53-
// We don't show the admin toolbar in multi-id mode, as it's used for previewing in the dashboard.
54-
return null;
55-
}
5649

5750
if (context.changeRequest) {
58-
return <ChangeRequestToolbar context={context} />;
51+
return (
52+
<IframeWrapper>
53+
<ChangeRequestToolbar context={context} />
54+
</IframeWrapper>
55+
);
5956
}
6057

6158
if (context.revisionId !== context.space.revision) {
62-
return <RevisionToolbar context={context} />;
59+
return (
60+
<IframeWrapper>
61+
<RevisionToolbar context={context} />
62+
</IframeWrapper>
63+
);
6364
}
6465

6566
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)