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.Editoris a CodeMirror surface. Passvalue/onChangeto own the code yourself, or omitvalueto letLivehold it (see Who owns the code). Either way it pushes to the shared preview codedebouncems after you stop typing (default1000) — a full second of lag is expected, not a bug. Passdebounce={0}for immediate updates.Live.Previewcompiles the shared code and renders it;showErrorsurfaces compile/runtime errors inline.dynamicTailwindcompiles Tailwind classes from the rendered preview at runtime and injects them as a<style>tag — needed inshadowmode, 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
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:
| Surface | Holds the code | Told about an edit | Shared preview code updates |
|---|---|---|---|
Live.Editor with value | you | onChange, on every edit | debounce ms after typing stops |
Live.Editor without value | Live | onChange, on every edit | debounce ms after typing stops; the editor shows each keystroke at once |
Live.Dnd with value | you | onChange, on every edit | at once |
Live.Dnd without value | Live | onChange, on every edit | at once |
Live.Preview | — | — | reads the shared code, unless you pass code |
- Pick one owner per
<Live>. Either pass the samevalue/onChangestate to every surface, or passvalueto none of them. Mixing the two is not supported: a surface you control ignores changes the others make toLive's own copy. - Without
value, surfaces stay in step. ALive.Editorand aLive.Dndthat both omitvalueshareLive's copy, so an edit in one shows up in the other. An editor withoutvalueordefaultValuestarts 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.Dndpanel, right after typing still works on the typed code. - Formatting (Cmd/Ctrl+S) applies at once. It calls
onChangewith the formatted code and updates the shared code without waiting fordebounce. - 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.DndcallsonChangeonce per edit. - A burst of typing is one step.
Live.Editorchanges less thanmergeWindowmilliseconds 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.
| Returns | Description |
|---|---|
value | The 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 / redo | Move through the history. |
canUndo / canRedo | Whether 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.