Skip to main content

Editor Mode

Live.Editor and Live.Preview share code through Live's context — edit in the editor and the preview updates automatically. No manual wiring between them is needed. (The preview itself isn't an editing surface — only Live.Editor writes to the shared code.)

import { useState } from 'react';

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={3}>Hello from the editor</ui.Typography.Title>
<ui.Button type="primary">Edit me</ui.Button>
</div>
);

export default App;
`;

export default function EditorMode() {
const [value, setValue] = useState(SAMPLE);

return (
<Live>
<Live.Preview
showError
dynamicTailwind
modules={uiKitModules}
frame={{ mode: 'shadow', syncStyle: true }}
/>
<Live.Editor value={value} onChange={setValue} />
</Live>
);
}
  • Live.Editor is a CodeMirror surface. Pass value / onChange to own the code yourself, or omit value to let Live hold it (see Who owns the code). Either way it pushes to the shared preview code debounce ms after you stop typing (default 1000) — a full second of lag is expected, not a bug. Pass debounce={0} for immediate updates.
  • Live.Preview compiles the shared code and renders it; showError surfaces compile/runtime errors inline.
  • dynamicTailwind compiles Tailwind classes from the rendered preview at runtime and injects them as a <style> tag — needed in shadow mode, which has no separate document to load a stylesheet into (see Preview Modes).

See Overview for the full prop list on both components.

Who owns the code​

note

The previewed code runs with your page's privileges. Use the editor for code from people you trust, or see Security for the options when you can't.

Every surface inside one <Live> reads and writes the same code. Who holds it depends on whether you pass value:

SurfaceHolds the codeTold about an editShared preview code updates
Live.Editor with valueyouonChange, on every editdebounce ms after typing stops
Live.Editor without valueLiveonChange, on every editdebounce ms after typing stops; the editor shows each keystroke at once
Live.Dnd with valueyouonChange, on every editat once
Live.Dnd without valueLiveonChange, on every editat once
Live.Preview——reads the shared code, unless you pass code
  • Pick one owner per <Live>. Either pass the same value / onChange state to every surface, or pass value to none of them. Mixing the two is not supported: a surface you control ignores changes the others make to Live's own copy.
  • Without value, surfaces stay in step. A Live.Editor and a Live.Dnd that both omit value share Live's copy, so an edit in one shows up in the other. An editor without value or defaultValue starts from that shared code.
  • A pending update is never lost. The editor pushes a waiting update at once when it loses focus or unmounts, instead of dropping it. Switching to another tab, or clicking into a Live.Dnd panel, right after typing still works on the typed code.
  • Formatting (Cmd/Ctrl+S) applies at once. It calls onChange with the formatted code and updates the shared code without waiting for debounce.
  • Separate <Live> providers are independent. Each holds its own code.

Undo and redo​

useLiveHistory holds the document with its undo history. Pass its value and onChange to every surface:

import Live, { useLiveHistory } from '@jbpark/live-editor';

const Editor = () => {
const doc = useLiveHistory(initialCode);

return (
<Live>
<button disabled={!doc.canUndo} onClick={doc.undo}>
Undo
</button>
<button disabled={!doc.canRedo} onClick={doc.redo}>
Redo
</button>
<Live.Dnd value={doc.value} onChange={doc.onChange} />
<Live.Editor value={doc.value} onChange={doc.onChange} />
</Live>
);
};

Each onChange call says where the change came from, as a second argument: { source: 'dnd' } from Live.Dnd and { source: 'editor' } from Live.Editor. The hook uses it to decide what one undo step is:

  • A canvas edit is one step: a panel field, a drop, a move, a copy or a delete. Live.Dnd calls onChange once per edit.
  • A burst of typing is one step. Live.Editor changes less than mergeWindow milliseconds apart (500 by default) replace the current step, so one undo restores the code from before the burst. A canvas edit, an undo or a pause ends the burst.
  • A change equal to the current value adds nothing, and a new step drops the redo history.
  • limit (100 by default) caps the number of steps; the oldest go first.
ReturnsDescription
valueThe current document.
onChange(value, change?) => void, for Live.Dnd and Live.Editor. Without change, one step.
set(value) => void: a change of your own, such as applying a template. One step.
reset(value) => void: replaces the document and clears the history, such as when opening another document.
undo / redoMove through the history.
canUndo / canRedoWhether there's anything to move to.

Keyboard. Cmd/Ctrl+Z undoes, and Cmd/Ctrl+Shift+Z and Ctrl+Y redo, anywhere on the page except in an input, a text area, a select, a contenteditable element or the code editor, which keep their own text undo. Pass shortcuts: false to bind nothing and call undo and redo yourself. A key pressed inside an iframe frame doesn't reach the page, so it isn't seen.

What undo covers. The history holds what was committed. Text typed into a panel field and not committed yet (it commits when the field loses focus) isn't in it, and the field shows the restored value after an undo. A selected section stays selected when it's still in the restored document. Undo also works while the document doesn't parse: it's how to get back to one that does.

The second argument is there without the hook too, for a history of your own: onChange={(value, { source }) => ...}. A one-argument onChange, such as setValue, works as before.