Skip to content

Commit 6aaeae2

Browse files
authored
Fix an issue where ContentKit iframes were not resizing. (#2967)
1 parent e5f0cc8 commit 6aaeae2

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

.changeset/afraid-countries-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-contentkit': minor
3+
---
4+
5+
Fix an issue where ContentKit iframes were not resizing.

packages/react-contentkit/src/ElementWebframe.tsx

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
1414
const renderer = useContentKitClientContext();
1515
const iframeRef = React.useRef<HTMLIFrameElement>(null);
1616
const [size, setSize] = React.useState<{
17-
maxWidth?: number;
18-
maxHeight?: number;
17+
height?: number;
1918
aspectRatio?: number;
2019
}>({});
2120

@@ -88,9 +87,8 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
8887
const height = parsed.height;
8988

9089
setSize({
91-
maxWidth: width,
9290
aspectRatio: width / height,
93-
maxHeight: height,
91+
height: height,
9492
});
9593
}
9694
} catch (_err) {
@@ -108,11 +106,25 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
108106
messagesQueueRef.current = [];
109107
break;
110108
case '@webframe.resize':
111-
setSize({
112-
maxWidth: Number(message.action.size.maxWidth),
113-
maxHeight: Number(message.action.size.maxHeight),
114-
aspectRatio: Number(message.action.size.aspectRatio),
115-
});
109+
setSize((size) => ({
110+
aspectRatio:
111+
typeof message.action.size.aspectRatio !== 'undefined'
112+
? Number(message.action.size.aspectRatio)
113+
: size.aspectRatio,
114+
115+
height: (() => {
116+
if (typeof message.action.size.height !== 'undefined') {
117+
return Number(message.action.size.height);
118+
}
119+
120+
// maxHeight was used prior to moving to height, maintain it for backward compatibility.
121+
if (typeof message.action.size.maxHeight !== 'undefined') {
122+
return Number(message.action.size.maxHeight);
123+
}
124+
125+
return size.height;
126+
})(),
127+
}));
116128
break;
117129
default:
118130
renderer.update({
@@ -149,33 +161,33 @@ export function ElementWebframe(props: ContentKitClientElementProps<ContentKitWe
149161
return sendMessage({ state });
150162
}, [element.data, renderer.state, sendMessage]);
151163

164+
if (!mounted) {
165+
return null;
166+
}
167+
168+
const aspectRatio = size.aspectRatio || element.aspectRatio;
169+
152170
return (
153-
<div
154-
className={'contentkit-webframe'}
171+
<iframe
172+
ref={iframeRef}
173+
src={element.source.url}
174+
title={element.source.url}
175+
allowFullScreen
176+
allow="clipboard-write"
177+
className="contentkit-webframe"
155178
style={{
156-
aspectRatio: size.aspectRatio || element.aspectRatio || undefined,
157-
maxWidth: size.maxWidth || undefined,
158-
maxHeight: size.maxHeight || undefined,
179+
// If given an aspect ratio, use width as auto dimension and let height take precedence.
180+
...(aspectRatio
181+
? {
182+
width: 'auto',
183+
aspectRatio,
184+
}
185+
: { width: '100%' }),
186+
187+
maxWidth: '100%',
188+
height: Math.max(size.height || 0, 32),
189+
border: 'none',
159190
}}
160-
>
161-
{mounted ? (
162-
<iframe
163-
ref={iframeRef}
164-
src={element.source.url}
165-
allowFullScreen
166-
allow="clipboard-write"
167-
style={{
168-
position: 'absolute',
169-
top: 0,
170-
left: 0,
171-
bottom: 0,
172-
right: 0,
173-
width: '100%',
174-
height: '100%',
175-
border: 'none',
176-
}}
177-
/>
178-
) : null}
179-
</div>
191+
/>
180192
);
181193
}

0 commit comments

Comments
 (0)