Skip to main content

Live Editor

An interactive editor for building UIs with real-time preview and drag-and-drop. Canvas edits sync back to source code via AST transforms, and the preview renders inside an isolated iframe (or shadow DOM) for DOM/CSS isolation.

Install​

react and react-dom (>=19) are required peer dependencies; include them in your app. prettier (^3) is an optional peer dependency. Install it for format-on-save; without it, the editor skips formatting. @jbpark/ui-kit (^10.5.0) is an optional peer dependency too, needed only for the @jbpark/live-editor/ui-kit entry (see below).

npm install @jbpark/live-editor react react-dom

To enable formatting:

npm install prettier@^3

To use the opt-in @jbpark/live-editor/ui-kit entry (ui-kit field controls, palette and preview modules), install @jbpark/ui-kit as well. See ui-kit controls.

Import the stylesheet once, near your app root:

import '@jbpark/live-editor/style.css';

The editor's own UI (palette, canvas and panel) takes its colors, font sizes and radii from CSS custom properties named --live-editor-*. Set them on :root, or on any element around the editor, to restyle it:

:root {
--live-editor-color-primary: #7c3aed;
}

The dark values apply under an element with data-theme="dark" or the dark class. src/tokens.css lists every token.

Smaller bundles with subpath imports​

import Live from '@jbpark/live-editor' pulls in every feature area (Dnd, Editor, Preview) whether you use it or not. If you only need one, import it from its own subpath instead:

import Dnd from '@jbpark/live-editor/dnd';
import Editor from '@jbpark/live-editor/editor';
import LiveError from '@jbpark/live-editor/error';
import Preview from '@jbpark/live-editor/preview';
import ContextProvider from '@jbpark/live-editor/provider';

./preview and ./editor genuinely shrink what gets bundled (no CodeMirror in a preview-only build, no @dnd-kit in an editor-only one). ./dnd is the exception: its property panel reuses the editor's own CodeMirror Core and prettier-based formatting internally for jsx/richtext field editing, so CodeMirror/prettier come along with ./dnd too — that's a real dependency of the feature, not something this subpath split was able to remove.

A page that only shows saved documents can leave the compiler out too: ./runtime runs a document transpiled ahead of time. See Serving a saved document.

Next.js and server rendering​

The components are client components. In the Next.js App Router, render them from a file that starts with 'use client':

'use client';

import Live from '@jbpark/live-editor';

export const Editor = ({ code }: { code: string }) => (
<Live>
<Live.Preview code={code} frame />
</Live>
);

Pre-rendering on the server is safe:

  • Live.Editor and a Live.Preview without frame render on the server.
  • Live.Dnd's sections and a Live.Preview with frame render nothing on the server and appear once the page hydrates.
  • A Live.Preview without frame compiles and runs the previewed code on the server while pre-rendering, with the server's privileges (see Security Notes). To avoid that, give it a frame, or load it with next/dynamic and ssr: false.
  • ./utils, ./utils/ast and ./utils/tailwind hold no React components and work in Server Components and plain Node.

Quick start​

Live is the provider that holds the shared editing context. Everything else (Live.Editor, Live.Preview, Live.Dnd) reads from it, so an edit on one surface flows to the others automatically.

import { useState } from 'react';

import Live from '@jbpark/live-editor';

const SAMPLE = `
const App = () => (
<div className="flex flex-col items-start gap-2 p-6">
<h3 className="text-xl font-semibold">Hello</h3>
<button type="button" className="rounded bg-indigo-500 px-3 py-1 text-white">
Edit me
</button>
</div>
);

export default App;
`;

export default function Example() {
const [code, setCode] = useState(SAMPLE);

return (
<Live>
<Live.Editor value={code} onChange={setCode} />
<Live.Preview showError frame={{ mode: 'iframe', syncStyle: true }} />
</Live>
);
}

How Tailwind reaches the preview​

Every sample on this site uses Tailwind utility classes (className="flex flex-col items-start gap-2 p-6"), and none of them work for free — the code a reader types is compiled and rendered at runtime, so there's no build step that could have generated CSS for classes that don't exist yet when the app first loads. There are three ways to make them actually apply, and the first two are complementary — use both together to cover each other's gaps.

syncStyle — clones the host document's already-compiled <link>/<style> tags in (into the iframe document, or directly into the shadow root — works under both frame.modes):

<Live.Preview frame={{ mode: 'shadow', syncStyle: true }} />

Since it's copying real, already-built CSS, it includes anything a consuming app's own build knew about — including custom theme tokens (e.g. ui-kit's Button rendering bg-primary, backed by a project-specific --primary token that Tailwind's own default theme has no idea exists). Its limitation is the mirror image: it can't include a utility class that only appears in code typed at runtime, since by definition no build ever saw it to compile CSS for it.

dynamicTailwind — pass it to Live.Preview and it scans the rendered DOM after mount, compiles whatever classes actually ended up on the page with the real tailwindcss engine, and injects the result as a <style> tag next to the rendered output. Scanning the DOM rather than the source text means it also picks up classes contributed by an imported component, not just literal className/cn(...) usage in the code the reader typed. Works with or without frame, and under either frame.mode:

<Live.Preview dynamicTailwind frame={{ mode: 'shadow' }} />

Its limitation is the mirror image of syncStyle's: it only knows Tailwind's own default theme (colors, spacing, font sizes, ...), not a consuming app's custom theme extensions, since it recompiles from scratch rather than reusing anything the host already built.

frame.scripts + a Tailwind runtime (iframe mode only) — load a standalone Tailwind runtime script into the iframe via frame.scripts so the iframe's own document processes its own classes live:

<Live.Preview
frame={{
mode: 'iframe',
syncStyle: true,
scripts: ['/path/to/tailwindcss-runtime.js'],
}}
/>

scripts doesn't apply under frame.mode: 'shadow' at all (there's no separate document to load it into) — see Preview Modes for what does and doesn't apply there.

Props reference​

Live.Preview​

PropTypeDescription
codestring?Explicit code to compile and render. Omit it to render whatever's in Live's shared context instead (the usual Live.Editor + Live.Preview pairing).
showErrorboolean?Show compile/runtime errors inline instead of failing silently.
frameboolean | FrameProps?Isolation strategy — see Preview Modes.
dynamicTailwindboolean?Generate Tailwind CSS for the current code at runtime — see How Tailwind reaches the preview above.
modulesRecord<string, unknown>?Specifiers compiled code can import, besides react. Pass uiKitModules from @jbpark/live-editor/ui-kit to make import * as ui from 'ui-kit' and ui-kit/utils resolvable, merged with your own entries.
propsRecord<string, unknown>?Props forwarded to the compiled code's default-exported component.
provider(children: ReactNode) => ReactNode?Wrap the rendered output in your own context providers. Live.Dnd takes it too; there, pass a stable function rather than an inline one — see Keeping edits fast.
containerHTMLElement | null?Deprecated: has no effect, and will be removed in the next major version.

Replacing injected modules​

Compilation results are cached by source and injected module entries. Module names are order-independent; objects and functions are compared by reference, and primitive values by Object.is. Separate module implementations keep separate cache entries within the shared 50-entry LRU limit.

To update a module in React, pass a new module object inside a new modules map. Recreating only the outer map with the same entries reuses the compilation. Mutating properties inside an existing module object does not invalidate the cache.

Direct callers of compile() can call clearCompilationCache() from @jbpark/live-editor/utils before compiling again after an in-place mutation. Clearing the cache does not trigger React renders or invalidate useCompiledModule's memo by itself; use new module references for mounted previews.

Live.Editor​

PropTypeDescription
valuestring?Current code (controlled). Omit it to use Live's shared context; an explicit empty string remains empty.
onChange(value: string, change: LiveChange) => void?Called on every edit, with { source: 'editor' }. See Undo and redo.
defaultValuestring?Initial code when value is omitted. Without it, the editor starts from Live's shared code (the built-in template unless another surface has written to it). An explicit empty string is preserved.
debouncenumber?Milliseconds to wait after the last keystroke before pushing to the shared preview code. Default 1000 — a full second of lag is expected, not a bug. Pass 0 for immediate updates. A waiting push runs at once when the editor loses focus or unmounts.
heightstring?Editor height (CSS value).
themeExtension | 'light' | 'dark' | 'none'?CodeMirror theme — a preset name or your own CodeMirror Extension.
prettierOptionsRecord<string, unknown>?Overrides for the built-in prettier config used on save.
fragmentboolean?Format the code as a JSX fragment (<>...</>) before/after prettier — for editing a snippet that has no single root element, rather than a whole component.
rawboolean?Skip prettier formatting on save entirely; commit the value verbatim.
renderEditor(data: EditorRenderData) => ReactNode?Replace the built-in CodeMirror surface — see Custom Editor.

Other exports​

  • Live.Error (LiveError) — the error display component Live.Preview's showError renders compile/runtime errors through internally (title, message, onReset props). Exposed in case you want to render the same error UI yourself. (Unrelated to the property panel's own edit-validation errors, which surface through a ui-kit Toast instead.)
  • LiveRenderer — an alias of Live.Preview/LivePreview, same component, same props.
  • useLiveHistory — the document with undo and redo across Live.Dnd and Live.Editor, and the Cmd/Ctrl+Z shortcuts. See Undo and redo. Also in @jbpark/live-editor/provider.
  • LiveProvider — the named export for what Live itself is (the shared-context provider). import Live from '@jbpark/live-editor' and import { LiveProvider } from '@jbpark/live-editor' are the same component.

Types​

DndPalette, DndPanel, DndLayout, PanelBinding, EditorRenderData, FrameProps, and Section are all importable from the package root:

import type { DndPanel, Section } from '@jbpark/live-editor';

BindingType/BindingOption live under a subpath instead — see Custom Palette & Panel.

Features​

PageWhat it covers
Editor ModeA code editor with a real-time preview, synced via AST transforms.
Custom EditorReplace the built-in CodeMirror editor with your own editing surface.
Drag & DropBuild UIs visually by dragging components onto a canvas.
Custom Palette & PanelReplace the built-in palette and property panel with your own markup.
Preview ModesRender the preview inside an isolated iframe or a shadow DOM host.