Getting started
The editor's model, how to save and load a document properly, and the props you will actually reach for.
Quick start gets an editor on the screen. This page is the next hour: how the component thinks, how to save without writing on every keystroke, how to load a document back, and which props matter.
Two packages, two jobs
| Package | Use it for | Ships |
|---|---|---|
@blockleaf/editor | Writing | The editor, the chrome, the schema |
@blockleaf/renderer | Reading | JSON to React or an HTML string, no ProseMirror |
A page that only displays a document should never import the editor. That separation is the whole reason the renderer exists — see server rendering.
The editor is uncontrolled
defaultValue is read once, when the editor mounts. Changing it later does
nothing.
// This does NOT swap the document.
<BlockleafEditor defaultValue={selectedDoc} />That is deliberate, not an omission. A controlled ProseMirror fights its own
transactions: every keystroke produces a transaction, re-rendering with a new
value replaces the document underneath the caret, and the undo stack dies
with it. Every editor on ProseMirror that offers a value prop is either
doing something clever and fragile, or quietly doing this.
To swap documents, remount:
<BlockleafEditor
key={docId}
defaultValue={doc}
/>Or drive the instance directly with useBlockleafEditor, below.
Saving
There are two change callbacks, and picking the wrong one is the most common mistake:
<BlockleafEditor
defaultValue={doc}
onChange={(next) => save(next)}
onChangeImmediate={() => setDirty(true)}
/>onChangeis debounced — 300 ms by default,debounceMsto change it. This is what you persist.onChangeImmediatefires on every transaction. This is for a dirty flag, an autosave indicator, a live preview or a word count.
Saving from onChangeImmediate means a network write per character. Driving
a live preview from onChange means a preview that lags a third of a second
behind the caret. Each callback is cheap for its own job and expensive for
the other one.
What arrives
Both callbacks receive a BlockleafDocument — plain JSON, with
attrs.schemaVersion already stamped on it. There is nothing to serialise
and nothing to clean up before it goes into a column:
await db.article.update({
where: { id },
data: { body: doc } // a jsonb column
});Loading
Read the JSON back and hand it to defaultValue. The only thing worth
checking first is that it is a Blockleaf document at all:
import { createEmptyDocument, isBlockleafDocument } from '@blockleaf/editor';
const doc = isBlockleafDocument(article.body)
? article.body
: createEmptyDocument();isBlockleafDocument is a structural guard — it answers "did this come from
Blockleaf?", not "is every node valid?". It is cheap enough to call on every
load, and it imports without pulling the editor in, so a server route can use
it too.
Read-only
editable={false} keeps the editor mounted and turns off input. It is the
right choice for a preview that toggles back into editing, and the wrong
choice for a page that only ever displays content — that page should use
the renderer and ship no editor at all.
<BlockleafEditor
defaultValue={doc}
editable={isEditing}
/>A block the editor does not know
A stored document can carry a node or mark this editor does not know: a block from a preset or subpath this instance did not load, or a node from a host extension that is not installed here. The editor keeps it. Each one opens as a placeholder that names its type — "Unsupported block: table" — and the rest of the document is editable as usual. When the document is saved, the placeholder is swapped back for the original JSON, exactly as it came in.
A placeholder can be selected, dragged, and deleted like any block. It cannot
be edited from here, and cut and paste drops it: a placeholder is never
rebuilt from clipboard HTML, because a payload read from the clipboard could
carry anything at all into your database. Its text is in
labels.unknown.
onChange and onChangeImmediate hand you the document with the originals
already back in place. If you go around them — editor.getJSON(), or
setContent with a stored document — do the swap yourself:
import {
unwrapUnknownContent,
wrapUnknownContent,
withSchemaVersion
} from '@blockleaf/editor';
editor.commands.setContent(wrapUnknownContent(stored, editor.schema));
const doc = withSchemaVersion(unwrapUnknownContent(editor.getJSON()));The placeholders come with both presets. If you pass your own extensions,
the editor adds them for you.
When the document cannot be read
Some documents still cannot be opened: a known block in a place the schema does not allow, like a list item outside a list, or JSON a migration got wrong. Tiptap's answer is to open an empty document behind a console warning. On its own that is how content gets lost, because the next save writes the empty document over the stored one.
So the editor refuses. When defaultValue cannot be read it opens read-only,
whatever editable says, and onChange and onChangeImmediate stay silent.
Nothing can reach your save path. onContentError tells you why, and that is
where to show a message:
function Article({ doc }: { doc: BlockleafDocument }) {
const [unreadable, setUnreadable] = useState<Error | null>(null);
return (
<BlockleafEditor
defaultValue={doc}
onChange={save}
onContentError={({ error }) => setUnreadable(error)}
>
{unreadable && (
<p role="alert">
This document contains content this editor cannot read. It
is open read-only so nothing is lost.
</p>
)}
</BlockleafEditor>
);
}The same lock applies to a document a newer editor wrote: one whose
attrs.schemaVersion is higher than this editor's SCHEMA_VERSION. Unlike
an unreadable document, it opens showing its content, because everything in
it could be read. It is still locked, because saving it from here would stamp
it with an older version than it has. onContentError fires once as the
editor is created, and its error is an IncompatibleDocumentError with
reason: 'newer-version', documentVersion and editorVersion.
incompatibleDocument is the override. The default is 'readonly'. The
other two:
'discard'opens the editor editable. For an unreadable document that is the empty one; for a newer one it is the document, saved from then on with this editor's version. Either way, you are accepting the loss.'throw'throws anIncompatibleDocumentErrorduring render, for your error boundary to catch. Nothing is mounted. Itsreasonis'unreadable'or'newer-version'.
The prop is live, so a "start over" button can flip it once the user has agreed:
<BlockleafEditor
defaultValue={doc}
incompatibleDocument={startOver ? 'discard' : 'readonly'}
/>Behind this sits Tiptap's content check, which the editor turns on for you. Three paths are checked, and they fail differently:
defaultValuefiresonContentError, then opens empty and locked.insertContentfires it and returnsfalse. Nothing is inserted and nothing locks.setContentthrows[tiptap error]: Invalid JSON content, does not fire the callback, and leaves the document as it was. Wrap the call.
enableContentCheck={false} turns the check off. That restores Tiptap's own
behaviour, an empty editable document and no callback, and nothing locks.
emitContentError only matters with the check off: it makes insertContent
still report, and does nothing for defaultValue.
Presets
preset chooses the extension set:
standard(the default) — everything in the schema: headings, lists, task lists, tables, callouts, images, code blocks, blockquotes, links, highlight.minimal— paragraphs and the inline marks only: bold, italic, underline, strike, inline code and links. No headings, lists, quotes, code blocks, rules, tables or images. For comment boxes and short-form fields where a table would be absurd.
<BlockleafEditor preset="minimal" />Passing your own extensions array replaces the preset entirely. See
extensions for adding to one instead.
The props you will reach for
| Prop | Type | Default | What it does |
|---|---|---|---|
defaultValue | BlockleafDocument | empty document | Initial content; read once |
onChange | (doc) => void | — | Debounced. Persist from here |
onChangeImmediate | (doc) => void | — | Every transaction. Dirty flags and previews |
debounceMs | number | 300 | How long onChange waits |
preset | 'standard' | 'minimal' | 'standard' | Which extension set |
extensions | Extensions | — | Replaces the preset entirely |
editable | boolean | true | Turns input off without unmounting |
autofocus | boolean | false | Place the caret on mount |
label | string | 'Document' | Accessible name for the editing surface |
placeholder | string | object | false | built-in | Empty-block hint |
className | string | — | Added alongside blockleaf-editor |
fallback | ReactNode | null | Rendered before the editor is ready |
children | ReactNode | — | Rendered after the document; see below |
onContentError | (props) => void | — | The document could not be read. See above |
incompatibleDocument | 'readonly' | 'discard' | 'throw' | 'readonly' | What happens to a document the editor cannot read |
children render inside the editor's box, after the document, and can
reach the editor with Tiptap's useCurrentEditor(). That is how a
word count footer or an outline finds the editor without
a prop.
Upload props are documented in uploads; every chrome prop —
slashItems, bubbleActions, blockActions, tableActions and the rest —
in the chrome.
Give it a label
A contenteditable is an ARIA textbox, and an unnamed one is announced as
nothing at all. label defaults to Document; set it to whatever the
user thinks they are editing.
Next.js
The editor is a client component. Three things are already handled for you, and one is not:
'use client'goes at the top of the file that rendersBlockleafEditor, as with any interactive component.- Hydration — the editor does not render until it is on the client, which is the single most common Tiptap and SSR mismatch. Nothing to configure.
- The stylesheet can be imported from a server component, a client component or your root layout. It is plain CSS.
fallbackis yours to set. Until the editor is ready the component rendersnull, which means the page is a little shorter for a frame. Pass a skeleton of roughly the right height if that shift bothers you.
<BlockleafEditor
fallback={<div style={{ minHeight: '24rem' }} />}
defaultValue={doc}
onChange={save}
/>Dropping to the hook
BlockleafEditor is a thin component over useBlockleafEditor. When you want
the Tiptap instance — to run commands, to add a toolbar of your own, to call
setContent — use the hook and render the chrome yourself:
'use client';
import { useBlockleafEditor } from '@blockleaf/editor';
import { EditorContent } from '@tiptap/react';
export const Editor = ({ doc }: { doc: BlockleafDocument }) => {
const editor = useBlockleafEditor({ defaultValue: doc });
if (!editor) return null;
return (
<div className="blockleaf-editor">
<button onClick={() => editor.commands.toggleBold()}>Bold</button>
<EditorContent editor={editor} />
</div>
);
};The hook returns a Tiptap Editor or null. It takes the same options as
the component minus the chrome flags, so the default menus are not included
— add them from the chrome, or build your own.