docx-editor

Props

Every DocxEditor prop, the imperative handle, and which props are read once.

DocxEditor is the single component the React entry exports. This page lists its props and the handle behind its ref; the guides linked from each row carry the longer explanation.

Props

PropTypeDefaultWhat it does
documentArrayBuffer | Uint8Array | BlobrequiredThe document to open. Bytes open immediately; a Blob or File is read first, and whichever document arrived last is the one opened.
modeDocxEditorMode{ kind: "edit" }What the editor is for. See Mode.
renderImportError(error: DocxImportError) => ReactNodebuilt-in panelWhat to render in place of the editor when the document was refused.
showPageGuidesbooleantrueWhether to draw approximate page boundaries over the document's paper. Drawn in read-only mode too.
zoomDocxEditorZoomuncontrolledThe visual scale, when the host owns the selection.
defaultZoomDocxEditorZoom"fit-width"The initial scale when the editor owns the selection.
onZoomChange(zoom: DocxEditorZoom) => voidReceives toolbar requests. A controlled host must update zoom for one to take effect.
fontFallbacksFontFallbacksDEFAULT_FONT_FALLBACKSThe fonts drawn in place of names missing from the reader's machine. See Styling.
presetsDocxEditorPresetspackage defaultsThe lists the built-in pickers offer. See Custom controls.
commentAuthorCommentAuthor{ name: "Anonymous" }The identity written by the built-in comment and reply composers.
pluginsreadonly Plugin[]ProseMirror plugins placed ahead of the built-in ones, so a host keymap sees an event first.
classNamestringAdded to the editor's outer frame alongside its own class.
styleCSSPropertiesApplied to that same frame.
onReady(view: EditorView) => voidCalled with the view once the editor has mounted a document.
onChange() => voidCalled on every editor state change, which includes cursor and selection moves.

DocxEditorProps, DocxEditorMode, DocxEditorZoom, DocxEditorPresets, CommentAuthor, and DocxSource are all exported from the root entry, so a wrapper component can name them without restating their shapes.

Mode

mode is a union rather than a set of independent booleans, because a read-only editor accepts no edits and therefore has no toolbar and no context menus to offer.

type DocxEditorMode =
  | { kind: "readOnly" }
  | {
      kind: "edit";
      toolbar?: boolean;
      contextMenus?: boolean;
      locking?: boolean;
    };

toolbar and contextMenus both default to true. contextMenus: false leaves the right button to the browser, which is what a host drawing menus of its own wants. locking defaults to false and adds the controls for settling part of a document; a lock the document already carries is honored in every mode. See Locked content for the commands behind it.

The handle

interface DocxEditorHandle {
  view: EditorView;
  exportBytes: () => Uint8Array;
}

The ref holds null until a document has been opened, and holds null again for a document that was refused, so a control that exports should read it at click time rather than caching it. exportBytes writes the state currently on screen and throws DocxExportError when it cannot do so safely. Getting started covers downloadDocx, which wraps the handle for the ordinary browser download.

Props read once

Most props take effect on the render that changes them. Four are read when the editor mounts, because they go into the ProseMirror state and the view built around it:

  • plugins
  • fontFallbacks
  • defaultZoom
  • mode.contextMenus

Building any of them inline on every render is therefore harmless. To change one, change the component's key and let it remount, which is also how the component asks for a document to be swapped.

On this page