Put it in your app React component
React component
DocsEditor is the whole editor in one component: toolbar, ruler, side panel, pages and status bar. It mounts in your page, not in an iframe.
Switch it on and off
A real DocsEditor with the agreement of the demo. The features are read when it is made, so each switch makes it again, with the document as it is: type something, then switch one. The buttons above it use the handle, through a ref.
Opening the editor…
Put it in a page
The package is @rebodocs/react. Everything it exports is in Components and hooks.
'use client';
import { DocsEditor } from '@rebodocs/react';
export function Contract({ file }: { file: Uint8Array }) {
return (
<div className="h-screen">
<DocsEditor
docx={file}
name="contract.docx"
author="Ana"
locale="en"
onSave={(docx, name) => upload(docx, name)}
/>
</div>
);
}- The element around it must have a height: the editor fills what it is given.
- It is safe to render on a server. There it draws an empty element, and mounts once the page is in the browser; the bundle starts with
'use client'. - Without
locale, the interface is in the first of the browser’s languages it speaks, or in English.
Props
Each with what happens when it changes. Follows: the editor takes the new one as it is. Last one called: a new function in each render is fine. Made again: the editor is made again with the document as it is, unsaved changes and all, but without what there was to undo. Opens it: another document, or another room, takes the place of this one.
- docxUint8Array | ArrayBuffer
- Opens it The document to open; an empty one without it. Another one opens in its place. Not read with collab, where the room has the document.
- unsavedboolean
- Read with docx Whether docx has changes not saved yet, as a draft your application kept: dirty from the start, with the dot beside the title.
- namestring
- Follows What the document is called: shown in the toolbar, given to onSave and to what is exported.
- authorstring
- Follows Who the person is: what their notes and changes are signed with, and what the others see them as. Without it, the name the person gave last time, kept by the browser.
- localestring
- Follows The language of the interface: 'en', 'es', or one given in locales, such as the Chinese of @rebodocs/editor/locales. Left out, the first of the browser’s languages it speaks, or English. It is the whole page's, not this editor's alone. See Languages.
- localesRecord<string, Texts>
- Follows More languages, or other words for these, as { fr: texts }. What one leaves out is said in English. See Languages.
- languagestring
- Follows The language the text is in, as a tag ('en-US'): written in the DOCX, used for the variables that are dates and amounts, and the one a screen reader reads the text with. Left out, the one the file says, or else the interface's. The person can choose another at the foot of the editor.
- spellcheckSpellChecker
- Follows What marks the words spelled wrong with a red wave and offers what they could be: createSpellChecker with the dictionaries you give, or one of your own. See Spelling.
- theme'light' | 'dark' | 'auto'
- Follows The colours of the interface: light as it comes, dark, or 'auto' for the one the system is set to. The pages stay white, as paper. See Styling.
- fontsFontSource[]
- Follows Typefaces of your own, each with its family and its file. For the whole page, and they stay; the same ones in a new array are not loaded again. See Fonts.
- embedFontsboolean | 'subset'
- Follows Whether the DOCX carries the fonts of fonts its text is in, when they are TrueType and their licence lets it: whole unless false, or cut down to the characters used with 'subset'.
- letterheadBlob | { bytes, type } | string | null
- Follows A PNG or JPG to use as letterhead, whatever the document came with; null for none. Given, the menu no longer offers to change it. See Letterheads.
- versions{ list, load, save? }
- Made again Where your application keeps the versions. The side panel then lists them, compares the document with one and goes back to it; with save, it keeps one. See Versions.
- collab{ url, room, ticket? }
- Opens it The document is a room on your collaboration server, and comes from there. See Collaboration.
- featuresDocsFeatures
- Made again What is switched on. See below.
- onSave(docx, name) => void | Promise
- Last one called Given the document when the person saves, from the menu or with ⌘S (Ctrl+S), or when you call save(). Keeping it is yours to do; a promise that fails is told to onError.
- onChange(editor) => void
- Last one called Called when the document changes.
- onCollab(state, peers) => void
- Last one called Called when the connection, or who is on the document, changes.
- onError(message) => void
- Last one called Told what went wrong, in the language of the interface. Without it, an alert.
- onReady(handle | undefined) => void
- Last one called Given the handle once the editor is there, and undefined when it goes: on every mount.
- refRef<DocsHandle | undefined>
- Follows The same handle, kept in a ref: given on every mount, and undefined when the editor goes.
- className · stylestring · CSSProperties
- Follows For the element the editor is in. Its height is 100% of what it is in.
With onSave and versions, the functions are always the last given; only giving one where there was none, or taking it away, makes the editor again.
Features
What an application of its own may not want is off until asked for. What every document needs is on until switched off.
<DocsEditor docx={file} features={{ ruler: false, files: true }} />- menu
- on
- The document menu: the file, export, the page, the letterhead. A screen of its own over the editor, or 'card' for a card that drops from its button.
- panel
- on
- The side panel: outline, variables, notes, tracked changes, and versions when given. On the right of the pages; 'left', or 'rail' for a rail of icons always there, with counts.
- panelOpen
- off
- The side panel open from the start, on a window wide enough for it to go beside the pages: true, or the tab to open it on ('variables', 'history'…). On a phone it starts closed, as it would cover the pages.
- ruler
- on
- The ruler, with margins, indents and tab stops.
- statusBar
- on
- Pages, words, the language of the text, the view, the zoom, and who is on the document, as pills floating over the pages; 'chips' or 'bar' to set them under them.
- toolbar
- on
- The document above and the formatting under it; 'tabs' for tabs of tools (Format, Insert, Page), or 'row' for one row.
- find
- on
- Find and replace, from the toolbar or ⌘F (Ctrl+F).
- slash
- on
- The menu a "/" brings up, typed at the start of a paragraph or after a space: headings, lists, a table, a picture, a page break, a variable, the signatures. Or { builtIns, commands }: which of those it keeps (false for none), and your own. See Headless, The “/” menu.
- variableSuggestions
- on
- The list "{{" brings up: the variables you fill, then those of the document. Or { variables, fromDocument, allowNew }. See Templates, In the editor.
- forms
- off
- Marking areas that can be changed and restricting editing to them, from the Review menu, the bar and the “/” menu. A restricted document is restricted without it. See Templates, Areas to fill in.
- focus
- on
- The pages alone, full screen, from the status bar or ⌘⇧. (Ctrl+Shift+.). Escape leaves.
- title
- on
- The name of the document in the toolbar; with onSave or files, a dot beside it while there are changes not saved.
- responsive
- on
- Making do with a narrow window and the keyboard of a phone. See Phones and touch.
- files
- off
- Open a file from the disk in place of the document (⌘O), and save a copy to it; ⌘S downloads it when there is no onSave.
- language
- off
- The choice of the interface language, in the menu.
- theme
- off
- The choice of light, dark or as the system is, in the menu; the theme prop says the one to start with.
- assistant
- off
- The button in the status bar that lets an AI assistant in: true for the MCP server on this machine, or { url, token }.
- share
- off
- The button that starts sharing from here: { url, token?, link? }. Not for a document that is already a room.
The bar beside the selection is always there. To lay the pieces out yourself, see Without a framework or Headless.
The handle
ref and onReady give the same object, to reach the editor from outside.
const docs = useRef<DocsHandle>(undefined);
<DocsEditor ref={docs} docx={file} />
<button onClick={() => upload(docs.current!.exportDocx())}>Send</button>- exportDocx()
- The document as a DOCX, as it is now.
- save()
- Gives the document to onSave, as ⌘S does, and once it is kept, dirty is false. What an application that saves on its own calls, a moment after each change.
- dirty
- Whether it changed since it was opened or last saved.
- name
- What the document is called now: the name given, or the one of a file the person opened.
- showPanel(tab?)
- Opens the side panel on a tab ('outline', 'variables', 'notes', 'changes', or 'history' with versions), or on the one it was on. false when the panel is turned off.
- hidePanel()
- Closes it.
- focus()
- Gives the keyboard to the document.
- setName · setAuthor · setLocale · setTheme · setLetterhead
- Change what the props set, for whoever keeps them outside React.
- editor
- The Editor itself, for everything else.
- collab
- The session with the others, while there is one.
A new handle comes with each mount, and the old one is of an editor that is gone: keep the one the ref has now, not one kept from before. For the methods of the editor, see Editor API.
Saving
Two ways, and they can be combined.
Pass onSave. The menu shows Save, ⌘S works, and the dot beside the title says there are changes not saved.
Call exportDocx() whenever you like: on a timer, on leaving the page, from your own button. Or save(), to go through onSave and clear dirty.
With collab, the collaboration server keeps the document through your backend, and neither is needed.
Saving on its own Call save() a moment after the last change, from onChange, and on leaving the page when dirty is true. The person never has to think of it, and nothing is sent while nothing changed.
Without React
The same editor mounts in any element, with the same options, and gives the same handle, with destroy().
import { mountDocs } from '@rebodocs/react/mount';
const docs = mountDocs(document.querySelector('#editor')!, { docx: bytes, locale: 'en' });
docs.destroy();It is the /mount entry of the package, which holds no React; React is an optional peer, so npm does not install it for this. See Without a framework.