Skip to content

Commit bdde392

Browse files
authored
Fix web frame height (#3709)
1 parent 4f4210d commit bdde392

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

.changeset/giant-jobs-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@gitbook/react-contentkit": patch
3+
---
4+
5+
Fix web frame height calculation

bun.lock

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"devDependencies": {
77
"@biomejs/biome": "^1.9.4",
88
"@changesets/cli": "^2.29.7",
9-
"turbo": "^2.5.0",
10-
"vercel": "^39.3.0",
9+
"turbo": "^2.5.8",
10+
"vercel": "^39.4.2",
1111
},
1212
},
1313
"packages/browser-types": {
@@ -241,6 +241,7 @@
241241
"@gitbook/api": "catalog:",
242242
"@gitbook/icons": "workspace:*",
243243
"classnames": "^2.5.1",
244+
"usehooks-ts": "^3.1.0",
244245
},
245246
"devDependencies": {
246247
"typescript": "^5.5.3",

packages/react-contentkit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"dependencies": {
1212
"classnames": "^2.5.1",
1313
"@gitbook/api": "catalog:",
14-
"@gitbook/icons": "workspace:*"
14+
"@gitbook/icons": "workspace:*",
15+
"usehooks-ts": "^3.1.0"
1516
},
1617
"peerDependencies": {
1718
"react": "*"

packages/react-contentkit/src/ElementWebframe.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import type { ContentKitWebFrame } from '@gitbook/api';
44
import React from 'react';
5+
import { useResizeObserver } from 'usehooks-ts';
56

7+
import { Icon } from '@gitbook/icons';
68
import { useContentKitClientContext } from './context';
79
import { resolveDynamicBinding } from './dynamic';
810
import type { ContentKitClientElementProps } from './types';
@@ -47,9 +49,7 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
4749
[renderer.security]
4850
);
4951

50-
//
51-
// Listen to message coming from the webframe
52-
//
52+
// Listen to messages coming from the webframe
5353
React.useEffect(() => {
5454
const callback = (event: MessageEvent) => {
5555
if (!iframeRef.current) {
@@ -83,7 +83,7 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
8383
// https://docs.embed.ly/reference/provider-height-resizing
8484
const parsed = JSON.parse(message);
8585
if (parsed.context === 'iframe.resize' && typeof parsed.height === 'number') {
86-
const width = contentWindow.outerWidth;
86+
const width = iframeRef.current.clientWidth;
8787
const height = parsed.height;
8888

8989
setSize({
@@ -145,9 +145,7 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
145145
};
146146
}, [renderer, sendMessage]);
147147

148-
//
149148
// Send data to the webframe
150-
//
151149
React.useEffect(() => {
152150
if (!element.data) {
153151
return;
@@ -161,12 +159,20 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
161159
return sendMessage({ state });
162160
}, [element.data, renderer.state, sendMessage]);
163161

164-
if (!mounted) {
165-
return null;
166-
}
162+
const { width: observedWidth = 0 } = useResizeObserver({ ref: iframeRef });
163+
const liveWidth = observedWidth || iframeRef.current?.clientWidth || 0;
167164

168165
const aspectRatio = size.aspectRatio || element.aspectRatio;
169166

167+
const height =
168+
liveWidth && aspectRatio && liveWidth > (size.height ?? 0)
169+
? Math.min(Math.round(liveWidth / aspectRatio), size.height ?? 32)
170+
: 'auto';
171+
172+
if (!mounted) {
173+
return <Icon icon="spinner" className="contentkit-button-loading" style={{ height }} />;
174+
}
175+
170176
return (
171177
<iframe
172178
ref={iframeRef}
@@ -179,7 +185,8 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
179185
width: '100%',
180186
maxWidth: '100%',
181187
aspectRatio,
182-
height: size.height ? Math.max(size.height, 32) : '100%',
188+
maxHeight: height,
189+
height: 'fit-content',
183190
border: 'none',
184191
}}
185192
/>

0 commit comments

Comments
 (0)