Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions apps/virtuoso.dev/src/components/LiveCodeBlock/LiveCodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ function configureMonacoWorkers() {
}
}

const isFirefox = typeof navigator !== 'undefined' && navigator.userAgent.toLowerCase().includes('firefox')

function getCodeTypographyFromCSS(): {
fontFamily: string
fontSize: number
Expand Down Expand Up @@ -95,32 +93,40 @@ const iframeThemeStyles = {
`,
}

const iframeSource = '<!doctype html><html><head></head><body></body></html>'

const IframePortal: React.FC<{ children: React.ReactNode; theme: Theme }> = ({ children, theme }) => {
const [iFrameEl, setIframeEl] = React.useState<HTMLIFrameElement | null>(null)
const [portalRoot, setPortalRoot] = React.useState<HTMLElement | null>(null)

useEffect(() => {
if (!iFrameEl) {
setPortalRoot(null)
return
}

const updatePortalRoot = () => {
setPortalRoot(iFrameEl.contentDocument?.body ?? null)
}

updatePortalRoot()
iFrameEl.addEventListener('load', updatePortalRoot)

return () => {
iFrameEl.removeEventListener('load', updatePortalRoot)
}
}, [iFrameEl])

return (
<iframe
onLoad={(e) => {
if (isFirefox) {
// oxlint-disable-next-line typescript-eslint(no-unsafe-type-assertion)
setIframeEl(e.target as HTMLIFrameElement)
}
}}
ref={(el) => {
if (!isFirefox) {
setIframeEl(el)
}
}}
style={{ height: '100%', width: '100%' }}
>
{iFrameEl?.contentDocument
<iframe ref={setIframeEl} srcDoc={iframeSource} style={{ height: '100%', width: '100%' }} title="Live code preview">
{portalRoot
? createPortal(
<>
<style>{iFrameStyle}</style>
<style>{iframeThemeStyles[theme]}</style>
{children}
</>,
iFrameEl.contentDocument.body
portalRoot
)
: null}
</iframe>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
margin: 0;
}

html,
body {
height: 100%;
}

body {
line-height: 1.5;
-webkit-font-smoothing: antialiased;
Expand Down
Loading