DocEditor

A word-processor style document editor with headings, text formatting, lists, quotes and alignment, and a download as a Word (.docx) file.

Installation

npx dafink-ui add doc-editor

Also installs: Button, Select, Icons

Requires: @tiptap/react, @tiptap/starter-kit, @tiptap/extension-text-align, jszip

Demo

Props

NameTypeDefaultDescription
valuestring-Required. The document as HTML (headings, paragraphs, lists, quotes). Pass the HTML from onChange back in to keep it controlled.
onChange(html: string) => voidNoneCalled with the document HTML after every edit.
titlestringNoneDocument title: the default download name and the title inside the .docx file.
filenamestringtitleDownload name, without the .docx extension. Falls back to title, then "document".
readOnlybooleanfalseShow the document without editing. The formatting controls are hidden.
downloadablebooleantrueShow the Download .docx button. Turn it off when the host provides its own download.
heightnumber | string'40rem'Height of the editor. The page scrolls inside it.
aria-labelstring'Document'Accessible name of the editing area.
classNamestring''Additional CSS classes on the root element.

Accessibility

DocEditor's writing area is a multi-line textbox with an accessible name, and its toolbar is a labelled group of buttons. Tab moves into the toolbar and through each control, and the Left and Right arrows also move between them. Toggle buttons such as Bold report their state with aria-pressed, and the toolbar never takes focus away from the text, so the selection is kept while you format. In read-only mode the writing area is not editable and the formatting controls are removed.

KeyFunction
TabMoves between the toolbar and the writing area.
←+→In the toolbar, moves between controls.
Ctrl/Cmd+BBold. Italic is I, underline is U.
Ctrl/Cmd+ZUndo. Add Shift to redo.
Tab+Shift+TabIn a list item, indents or outdents it.