Skip to content

Commit

Permalink
requests iframe height for better sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
jongomez committed Nov 7, 2023
1 parent f856290 commit e995f9f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/components/mdx/StorybookDemo/StoryBookDemo.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
iframe {
transform: translateY(-20px);
width: 100%;
height: 6000px;
background-color: transparent;
}
}
37 changes: 36 additions & 1 deletion src/components/mdx/StorybookDemo/StorybookDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ import clsx from 'clsx'
import React, { useMemo, useRef, useState } from 'react'
import styles from './StoryBookDemo.module.scss'

const onIframeLoad = (iframe: HTMLIFrameElement) => {
const handleIframeMessage = (event: MessageEvent) => {
if (event.data && event.data.type === 'iframeHeightResponse') {
iframe.style.height = `${event.data.height}px`
}
}

// Request height from the iframe
const requestHeightFromIframe = () => {
if (!iframe || !iframe.contentWindow) {
return
}

iframe.contentWindow.postMessage(
{
type: 'requestHeight',
},
'*',
)
}

window.addEventListener('message', handleIframeMessage)

setInterval(() => {
requestHeightFromIframe()
}, 1000)
}

type GlobalType = {
name: string
description: string
Expand Down Expand Up @@ -124,7 +152,14 @@ export const StorybookDemo: React.FC<StorybookDemoProps> = ({
))}
</div>
<div className={styles.iframeContainer}>
<iframe ref={iframeRef} src={embedUrl} />
<iframe
ref={iframeRef}
src={embedUrl}
height={0}
onLoad={() => {
onIframeLoad(iframeRef.current)
}}
/>
</div>
</div>
)
Expand Down

0 comments on commit e995f9f

Please sign in to comment.