Hulian UIv0.93.5413 components0 components
  • Start
  • Components
  • Blocks
  • Pages
  • Demos
  • Changelog
ChineseEnglish
  • ComponentsComponents
  • ThemeTheme

Containers

  • Layout
  • AdminLayout
  • SettingsLayout
  • ScrollArea
  • Viewport
  • Resizable
  • AspectRatio
  • FitScreen
  • PreviewSandbox
  • Masonry
  • Container
  • AccentScope

Arrangement

  • Stack
  • Grid
  • Spacer
  • Divider
  • Separator

PreviewSandbox

preview-sandbox

Isolated preview shell with device viewports, scale-to-fit, and error capture.

Usage

Basic usage

Pass a complete HTML document string as code and it goes straight into the iframe srcDoc. It renders at the desktop viewport by default and scales down until it fits the container.

tsx
<div className="h-[420px] w-full overflow-hidden rounded-[var(--radius)] border border-border">
  <PreviewSandbox code={html} />
</div>

Device switch and frame

Switching devices only resizes the box: the iframe node and its document are never rebuilt, so state inside the preview survives. Phone and tablet presets can also wear a device frame.

tsx
const [device, setDevice] = useState("iphone");

<PreviewSandbox code={html} device={device} showDeviceFrame />

Runtime errors and retry

Click the button in the preview to make the script throw: the injected bootstrap posts the error back to the host, the error state covers the preview, and Retry reloads the document.

tsx
<PreviewSandbox
  code={crashingHtml}
  onError={(e) => console.warn(e.source, e.message)}
/>

Same-document mode

Pass children instead of code: the subtree renders in the current document behind a real React error boundary, and the error object has the same shape as in iframe mode. Click the button to make the subtree throw and see the error state.

Subtree rendering normally
tsx
<PreviewSandbox device={{ width: 480, height: 320 }}>
  <YourComponent />
</PreviewSandbox>

When to use

Use it to embed somebody else's interface inside your page without letting it touch the host: live preview of AI-generated UI, template and theme stores, the canvas area of a visual builder, WYSIWYG for landing page or email HTML.

It is a shell, not an execution engine: it does not bundle, transpile, or install packages. code means exactly one thing: a complete HTML document string that can go straight into an iframe, never a piece of JSX waiting to be compiled (see the first pitfall below).

  • For responsive layout by device width where the content still belongs to this app, use Viewport: container queries, no isolation, no iframe.
  • To scale a fixed design size up to fill a wall display, use FitScreen; it does scale up, while fit here deliberately does not.
  • For a static screenshot in a device body, use IPhone / Android / Tablet directly; showDeviceFrame reuses exactly those.
  • To pick elements inside the preview and get a path back (point-and-edit), pair it with ElementSelectionOverlay, which needs contentDocument access (see the sandbox trade-off below).

Import

ts
import {
  PreviewSandbox,
  PREVIEW_SANDBOX_DEFAULT_SANDBOX,
  PREVIEW_SANDBOX_DEVICES,
  PREVIEW_SANDBOX_MESSAGE_KEY,
  PREVIEW_SANDBOX_SAME_ORIGIN_SANDBOX,
  bootstrapScript,
  buildSrcDoc,
  computePreviewScale,
  normalizeIframeMessage,
  normalizeReactError,
  resolveFrameKind,
  resolveViewport,
} from "@hulianui/ui"

Props

NameTypeDefaultDescription
codestring-iframe mode content: a complete HTML document string written into srcDoc. Passing it selects srcDoc iframe mode
srcstring-External or hosted URL mode (<iframe src={src} />, #435) for multi-file static prototype bundles and live links. Mutually exclusive with code and children (code > src > children)
childrenReactNode-Same-document mode content: a React subtree rendered directly. Ignored when code or src is present
device"desktop" | "iphone" | "android" | "tablet" | "watch" | { width, height }"desktop"Preview viewport; this is what window.innerWidth and media queries see inside the preview
orientation"portrait" | "landscape""portrait"Device orientation (#437). Applies to iphone, android, and tablet; "landscape" swaps viewport width/height and forwards to the device frame
showDeviceFramebooleanfalseWrap in a device body (phone/tablet/watch presets) or a desktop shell when paired with desktopFrame
statusBarbooleanfalseFor iphone and android device frames (showDeviceFrame={true}), renders the top system status bar and deducts its safe-area height from the viewport so the Dynamic Island or punch-hole never overlaps headers (#447)
statusBarTimestring"9:41"Clock text forwarded to the IPhone / Android status bar (#447)
statusBarTone"auto" | "light" | "dark""auto"Color scheme forwarded to the phone status bar and home indicator (#447)
homeIndicatorbooleanfalseFor iphone and android device frames (showDeviceFrame={true}), renders the bottom home indicator strip and deducts its height from the viewport (#447)
desktopFrame"safari" | "chrome" | "window" | "none""none"Desktop shell rendered for device="desktop" or custom sizes when showDeviceFrame={true} (#436): wraps the viewport in Safari, Chrome, or WindowFrame with unified frameScale fitting
frameUrlstring-Address-bar URL for safari / chrome desktop shells; defaults to src in URL mode when omitted
frameTitleReactNode-Desktop shell title: forwarded to WindowFrame.title or Chrome tab title (when string)
frameHeaderExtraReactNode-Extra slot on the right side of the desktop shell top bar (headerExtra for safari / chrome, toolbar for window)
frameWidthnumber-Device body width in px (or content width for desktop shells); for phone/tablet/watch frames it defaults to 1:1 logical screen width plus both borders (for example iphone 410, android 428, tablet 862), scaling down automatically via frameScale when the container is smaller (#441)
elevatedbooleanfalseIn frameless mode (showDeviceFrame={false}), elevates the scaled viewport stage with rounded corners, border, surface background, and shadow (rounded-2xl border border-border bg-surface shadow-2xl); automatically suppressed when a device or desktop frame is active (#439)
stageClassNamestring-Additional class name applied directly to the scaled inner viewport stage rectangle (#439)
scale"fit" | number"fit"Scale mode. fit scales down to fit and never scales up; a number is used as is. When showDeviceFrame renders a device or desktop shell, numeric scale applies to the whole outer frame (frameScale) while the inner screen stays "fit" edge to edge with no blank gap (#441)
sandboxstring"allow-scripts"The iframe sandbox attribute. allow-same-origin is deliberately absent; see the trade-off below
allowstring-The iframe Permissions Policy allow attribute (for example "clipboard-write; fullscreen", #435)
referrerPolicyHTMLAttributeReferrerPolicy"no-referrer"The iframe referrerPolicy attribute (#435)
instrumentbooleantrueInject the error-forwarding bootstrap (code mode only). Turning it off means runtime errors inside the iframe never arrive
apiRefMutableRefObject<PreviewSandboxApi | null>-Imperative control handle exposing reload() (code reloads srcDoc, src reloads the iframe, children resets the error boundary, #435)
titlestringFrom localeAccessible name of the iframe. Omit it and the sandbox follows the ConfigProvider locale.
errorTitlestringFrom localeTitle of the built-in error state. Omit it and the sandbox follows the ConfigProvider locale.
retryLabelstringFrom localeLabel of the retry button. Omit it and the sandbox follows the ConfigProvider locale.
renderError(error: PreviewSandboxError, retry: () => void) => ReactNode-Custom error state; it takes over completely, retry entry included

PreviewSandboxError has the same shape in both modes: { source: "iframe" | "react", kind: "error" | "unhandledrejection", message, stack, filename, lineno, colno, componentStack, error }. The original Error instance is only available in same-document mode; across the iframe realm it is always null.

Events

EventTypeDescription
onError(error: PreviewSandboxError) => voidSomething failed inside the preview: forwarded error / unhandledrejection in iframe mode, the React error boundary in same-document mode
onReady() => voidPreview ready: after load in iframe mode, after mount in same-document mode; fires again on every reload
onLoadingChange(loading: boolean) => voidLoading changed; swapping code or hitting retry goes back to true first. Always false in same-document mode

Accessibility

  • The iframe always carries a title. An unnamed iframe is the classic screen reader dead end, so rename it to something informative such as "Login page preview".
  • The error state uses Alert in its danger tone, which already is role="alert" and interrupts. The outer layer deliberately avoids a second element with the same role so it is not announced twice.
  • Retry is a real button, reachable by Tab and Enter, not a click-only icon; its label comes from the locale by default and retryLabel overrides it (prop, then locale, then the built-in Chinese fallback).
  • Accessibility of the previewed content is yours to own: the iframe document is a separate accessibility tree, and host landmarks, language, and contrast do not reach it. Ship <html lang> and semantic markup inside code.
  • Scaling uses transform: scale, so font sizes and DOM stay untouched and screen readers still get the original structure. Shrunken text is still harder to read, so do not leave important actions only inside a heavily scaled preview.

Pitfalls

  • The device list is derived from a single source, so a new device is added in one place. desktop is the "no frame" tier and the only explicit exception in the list; every other tier, watch included, comes from lib/device-metrics and maps one-to-one onto a component in the mockups category. This file used to carry a second hand-written list, which is why watch was missing and why nothing guaranteed that the inner screen ratio matched the viewport ratio, the cause of the white band inside device frames (#117, #139).
  • code is an HTML document string, not JSX. Dropping a <Button/> or a TSX snippet in there renders it as plain text; nothing errors and nothing compiles, which makes it the easiest silent failure here. To really execute generated component code there are two roads: wire a compiler on the consumer side (esbuild-wasm or WebContainers class dependencies, which this library will not take on), or switch to same-document mode and pass the already compiled component as children.
  • The default sandbox is allow-scripts only, with allow-same-origin deliberately left out. This is a real trade-off:
  • A srcdoc document already inherits the host origin. Granting both values means no sandbox at all: scripts in the preview can read and write host DOM, localStorage, and cookies, and can even remove the sandbox attribute themselves. Never open it up for content you do not fully trust, meaning AI output, user-pasted markup, or third-party templates.
  • The price is that the host cannot read iframe.contentDocument either. That is why errors travel by postMessage, which an opaque origin can still send, instead of listeners attached inside the iframe document.
  • Open it only when you must read the inner DOM, typically to pair with ElementSelectionOverlay for point-and-edit, and say so explicitly: sandbox={PREVIEW_SANDBOX_SAME_ORIGIN_SANDBOX}. The whole point of that constant is to make "I am knowingly turning isolation off" readable, greppable, and reviewable.
  • Message validation ignores origin. An opaque-origin iframe always reports event.origin as "null", so a check on it can never pass. The component validates the instance id inside the message plus event.source === iframe.contentWindow; do the same if you handle postMessage yourself.
  • instrument={false} mutes onError in iframe mode. Without the injected script there is no return path; that is by design, not a bug. The legitimate reason to turn it off is usually a strict CSP in the previewed document, or your own error reporting already installed there.
  • Reloading works by changing the srcDoc string, not by location.reload(), which is unreachable across origins. Retry therefore appends a reload marker comment to srcdoc; that is intentional, do not strip it as junk.
  • Switching devices does not reload the document, swapping code does. The former only resizes the box, so scroll position, form input, and animation progress all survive; the latter is a new document. Force a fresh start through retry or by changing code.
  • fit never scales up (capped at 1), unlike computeFit in FitScreen: blowing a 390px phone preview up to 800px only produces a giant phone whose font sizes, hit areas, and breakpoints all lie. Pass scale={1.5} explicitly if you want that.
  • The wrapper needs a definite height. The component is h-full w-full, so a collapsed parent shows nothing, the same trap as Flow.
  • Failed resource loads inside the iframe, such as a 404 image, are not errors. The bootstrap deliberately skips the capture phase and reports only genuine runtime exceptions, otherwise a broken image would be reported as a crashed preview.
  • Scripts in srcDoc never run under jsdom and there is no layout engine, so injection position, scale factors, and error normalization are all exported pure functions (buildSrcDoc, computePreviewScale, normalizeIframeMessage, normalizeReactError). Assert on those instead of trying to test the iframe internals.

Related

Viewport · FitScreen · IPhone · Android · Tablet · ElementSelectionOverlay · CodeBlock

Playground

<PreviewSandbox code={html} device="iphone" showDeviceFrame />
PreviousFitScreenLayoutNextMasonry

On this page

  • Usage
  • Documentation
    • When to use
    • Import
    • Props
    • Events
    • Accessibility
    • Pitfalls
    • Related
  • Playground