Skip to Content
Puzzle

Text Editor

Edit and render structured rich-text documents with an optional formatting toolbar.

Use TextEditor to edit a serializable TextDocument, TextEditorToolbar to provide formatting controls, and TextRenderer to display the saved document without editing controls.

Import

import { TextEditor, TextEditorToolbar, TextRenderer } from "@puzzle-ui/ui";
import type { TextDocument } from "@puzzle-ui/ui";

Basic usage

Pass defaultValue for an uncontrolled editor and place the toolbar in the toolbar prop so it receives the editor's formatting context.

390 × 240

For controlled editing, pass value and update it with the same document object emitted by onChange.

const [document, setDocument] = useState<TextDocument>({
  type: "doc",
  content: [],
});

<TextEditor
  value={document}
  onChange={setDocument}
  toolbar={<TextEditorToolbar />}
/>;

Display a document

<TextRenderer document={document} />

Documents support paragraphs, headings, and list items with theme typography variants. Inline marks include bold, italic, underline, and strike. TextRenderer renders text with semantic elements rather than inserting raw HTML.

API reference

TextEditor

PropTypeDescription
valueTextDocumentControlled document.
defaultValueTextDocumentInitial uncontrolled document.
onChange(document: TextDocument) => voidCalled when the document changes.
toolbarReactNodeOptional toolbar rendered in the editor context.

TextEditorToolbar

PropTypeDefaultDescription
classNamestring-Additional toolbar classes.
ariaLabelstring"Text formatting"Accessible toolbar name.
dir"ltr" | "rtl""ltr"Direction for keyboard navigation.

TextRenderer

PropTypeDescription
documentTextDocumentDocument to display. Required.
classNamestringAdditional renderer classes.

Accessibility

The formatting toolbar uses labeled buttons, pressed states, and arrow-key navigation. Keep it inside the editor's toolbar prop and provide an ariaLabel when multiple formatting toolbars need distinct names.

;