Skip to content

Commit d7e6a3a

Browse files
committed
docs: AFCL examples report their height again once the docs page hydrates, so charts opened by a link or a reload are no longer cut to 100px AdminForth/2005
1 parent 2d75aef commit d7e6a3a

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎documentation/afcl-preview/main.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ app.use(createRouter({ history: createMemoryHistory(), routes: [{ path: '/:path(
2929
initI18n(app);
3030
app.mount('#app');
3131

32-
// the docs page sizes the iframe to the example
33-
new ResizeObserver(() => {
32+
// the docs page sizes the iframe to the example, and asks for the height again once it listens
33+
function postHeight() {
3434
window.parent.postMessage({ afclPreview: id, height: document.body.offsetHeight }, '*');
35-
}).observe(document.body);
35+
}
36+
new ResizeObserver(postHeight).observe(document.body);
37+
window.addEventListener('message', (e) => e.data === 'afclPreviewHeight' && postHeight());

‎documentation/src/components/AfclPreview/index.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState, type ReactNode } from 'react';
1+
import React, { useEffect, useRef, useState, type ReactNode } from 'react';
22
import useBaseUrl from '@docusaurus/useBaseUrl';
33
import styles from './styles.module.css';
44

@@ -10,6 +10,7 @@ const INITIAL_HEIGHT = 100;
1010
export default function AfclPreview({ id, minHeight = 0, children }: { id: string; minHeight?: number; children: ReactNode }) {
1111
const src = useBaseUrl(`/afcl-preview/index.html#${id}`);
1212
const [height, setHeight] = useState(minHeight || INITIAL_HEIGHT);
13+
const frame = useRef<HTMLIFrameElement>(null);
1314

1415
useEffect(() => {
1516
function onMessage(e: MessageEvent) {
@@ -18,12 +19,14 @@ export default function AfclPreview({ id, minHeight = 0, children }: { id: strin
1819
}
1920
}
2021
window.addEventListener('message', onMessage);
22+
// an example opened before the page hydrated has already reported its height, nobody listened then
23+
frame.current.contentWindow.postMessage('afclPreviewHeight', '*');
2124
return () => window.removeEventListener('message', onMessage);
2225
}, [id, minHeight]);
2326

2427
return (
2528
<div className={styles.preview}>
26-
<iframe src={src} title={`${id} example`} loading="lazy" style={{ height }} />
29+
<iframe ref={frame} src={src} title={`${id} example`} loading="lazy" style={{ height }} />
2730
{children}
2831
</div>
2932
);

0 commit comments

Comments
 (0)