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 (thesandboxoption below controls the iframe'ssandboxattribute, 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, andautoHeightare 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:syncStyleclones 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), anddynamicTailwindrecompiles 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'ssm:/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:
| Mode | container | The overlay opens |
|---|---|---|
iframe | The iframe document's mount node | Inside the iframe. Its position: fixed resolves against the iframe's viewport. |
shadow | An overlay layer inside the shadow root, over the preview's box | Inside the preview's box, with the shadow root's styles. Clicks pass through the empty layer to the preview. |
| none | document.body in Live.Dnd; not passed in Live.Preview | Over 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
openas atype: 'boolean'field, and switch it in the panel. A button still works in a preview outside the canvas. - In
iframemode, a section grows to fit an open overlay.autoHeightlays fixed elements out against the height the canvas can show, so a bottom drawer or a centred modal is measured where it appears. - In
shadowmode, a section doesn't grow. There is noautoHeight, so the overlay is laid out in the section's own box. Give a section that opens one enough height, for example withmin-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:
| Option | Type | Applies to | Description |
|---|---|---|---|
mode | 'iframe' | 'shadow' | both | Isolation strategy. Omit it and nothing else in this table has any effect. |
title | string | iframe | The iframe's title attribute (accessibility). |
sandbox | string | iframe | Forwarded to the iframe's sandbox attribute, for DOM/CSS isolation only — not a security boundary (see the note above). |
syncStyle | boolean | both | Copy 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. |
scripts | string[] | iframe | External script URLs to load into the iframe document (e.g. a Tailwind runtime). |
styles | string[] | iframe | Raw CSS strings to inject as <style> tags into the iframe. |
stylesheets | string[] | iframe | External stylesheet URLs to <link> into the iframe. |
autoHeight | boolean | iframe | Size the iframe to its content instead of a fixed height. |
onLoaded | () => void | iframe | Called 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. |