Skip to main content

Preview Modes

frame.mode is the switch that gates isolation entirely — every other frame option below is inert until it's set. Without mode, Live.Preview renders straight into the host document, regardless of any other frame options passed.

  • iframe — isolates the preview in a real iframe document. Note this is not a security sandbox: the code still executes in the host page's JS realm (the sandbox option below controls the iframe's sandbox attribute, which is DOM/CSS isolation, not a security boundary either). See Security. Supports every option in the table below.
  • shadow — isolates styles via a shadow DOM host in the same document, without an iframe boundary. scripts, styles, stylesheets, and autoHeight are all silently ignored, since there's no separate document to inject them into. The shadow host only gets whatever CSS naturally inherits across the shadow boundary (font, color, and similar inherited properties) on its own — not utility classes. Two ways to get those working too: syncStyle clones the host document's <link>/<style> tags directly into the shadow root (covers anything already in the host's compiled CSS, including a consuming app's own custom theme tokens), and dynamicTailwind recompiles whatever classes it finds in the rendered DOM at runtime (covers anything the host's build never saw, e.g. a class typed at runtime) — see How Tailwind reaches the preview. The two are complementary and can be used together.

The two samples run the same code, yet on a wide screen the shadow sample's title is larger. The title's classes are responsive (text-xl sm:text-2xl lg:text-3xl), and the two modes measure breakpoints against different widths:

  • iframe: media queries, and so Tailwind's sm:/md:/lg: classes, follow the iframe's own width. The preview lays out as it would on a screen that size.
  • shadow: a shadow root has no viewport of its own, so they follow the browser window's width, however narrow the preview box is.

Narrow the window below 640px and both titles come out the same size. Use iframe when the preview has to show how a layout responds to its own width, as when previewing a mobile layout in a narrow column.

To preview Live.Preview at a device width, size its container: an iframe preview takes the container's width. Live.Dnd takes a canvasWidth prop for the same thing, see Previewing at a width.

import Live from '@jbpark/live-editor';
import { uiKitModules } from '@jbpark/live-editor/ui-kit';
import '@jbpark/live-editor/ui-kit/style.css';

const SAMPLE = `
import * as ui from 'ui-kit';

const App = () => (
<div className="flex flex-col items-start gap-2 p-6">
<ui.Typography.Title level={4}>Preview Modes</ui.Typography.Title>
<ui.Button type="primary">A button</ui.Button>
</div>
);

export default App;
`;

export default function PreviewModes() {
return (
<>
{/* iframe isolation */}
<Live>
<Live.Preview
code={SAMPLE}
modules={uiKitModules}
dynamicTailwind
frame={{ mode: 'iframe', syncStyle: true }}
/>
</Live>

{/* shadow DOM isolation */}
<Live>
<Live.Preview
code={SAMPLE}
modules={uiKitModules}
dynamicTailwind
frame={{ mode: 'shadow', syncStyle: true }}
/>
</Live>
</>
);
}

Unlike the other demos on this site, this one is rendered directly in the page — not inside a sandboxed iframe — since it has no reader-authored code to isolate (the sample is a fixed string, not wired to an editor).

Modals and drawers​

A preview's default export receives a container prop when the preview has a frame. Pass it to an overlay's portal target, such as ui-kit's Modal and Drawer, so the overlay opens inside the preview instead of over the host page. The example imports ui-kit, so pass modules={uiKitModules} to the preview:

import { useState } from 'react';

import * as ui from 'ui-kit';

const App = ({ container }) => {
const [open, setOpen] = useState(false);

return (
<>
<ui.Button onClick={() => setOpen(true)}>Open</ui.Button>
<ui.Modal
open={open}
container={container}
onCancel={() => setOpen(false)}
>
Opened inside the preview.
</ui.Modal>
</>
);
};

export default App;

What container is, and where the overlay opens:

ModecontainerThe overlay opens
iframeThe iframe document's mount nodeInside the iframe. Its position: fixed resolves against the iframe's viewport.
shadowAn overlay layer inside the shadow root, over the preview's boxInside the preview's box, with the shadow root's styles. Clicks pass through the empty layer to the preview.
nonedocument.body in Live.Dnd; not passed in Live.PreviewOver the whole page, wherever the component portals.

Overlays in Live.Dnd sections:

  • Open them from the panel. The canvas covers each section so a click selects it, so a button inside a section can't open an overlay there. Bind open as a type: 'boolean' field, and switch it in the panel. A button still works in a preview outside the canvas.
  • In iframe mode, a section grows to fit an open overlay. autoHeight lays fixed elements out against the height the canvas can show, so a bottom drawer or a centred modal is measured where it appears.
  • In shadow mode, a section doesn't grow. There is no autoHeight, so the overlay is laid out in the section's own box. Give a section that opens one enough height, for example with min-height.

The app component takes container and passes it down. A section that opens an overlay can use a component defined above App in the same document:

const ModalExample = ({ open = false, container }) => (
<ui.Modal open={open} title="Modal" container={container}>
Opened from the panel.
</ui.Modal>
);

const App = ({ container }) => (
<main id="app-container">
<section data-name="Modal" style={{ minHeight: 320 }}>
<ModalExample
data-id=""
data-binding={[{ label: 'Open', property: 'open', type: 'boolean' }]}
open={false}
container={container}
/>
</section>
</main>
);

frame options​

FrameProps (importable from @jbpark/live-editor) is this shape:

OptionTypeApplies toDescription
mode'iframe' | 'shadow'bothIsolation strategy. Omit it and nothing else in this table has any effect.
titlestringiframeThe iframe's title attribute (accessibility).
sandboxstringiframeForwarded to the iframe's sandbox attribute, for DOM/CSS isolation only — not a security boundary (see the note above).
syncStylebooleanbothCopy the host document's already-compiled <link>/<style> tags in (into the iframe document, or directly into the shadow root). In iframe mode it also mirrors the classes and data-* attributes of the host's <html> onto the iframe's, and follows changes, so a theme switched there (.dark, data-theme="dark") reaches the preview. A shadow root sits under the host's <html> already. Can't include utility classes that only appear in code typed at runtime — see How Tailwind reaches the preview.
scriptsstring[]iframeExternal script URLs to load into the iframe document (e.g. a Tailwind runtime).
stylesstring[]iframeRaw CSS strings to inject as <style> tags into the iframe.
stylesheetsstring[]iframeExternal stylesheet URLs to <link> into the iframe.
autoHeightbooleaniframeSize the iframe to its content instead of a fixed height.
onLoaded() => voidiframeCalled once per iframe document: after its initial load, and again if moving the iframe (as reordering canvas sections does) reloads it. An inline function is fine.