Put it in your app A complete interface
A complete interface
The repository has an application that uses @rebodocs/react the way yours would: apps/demo. It shows a document two ways: the editor as it comes (DocsEditor), and an interface built from nothing on the hooks, dark and laid out like a design tool. The second does everything the first does, so it is the place to copy from.
Your own interface, running
The component of apps/demo/src/HeadlessEditor.tsx, imported here as it is, on the contract it opens with: a template in English with headings, variables with a type, a table, text in two columns, places to sign, a header and a page number. The interface opens in the language of this page; the selector in its status bar changes it.
Opening the editor…
Both tabs, full screen, with a DOCX of your own: rebodocs.com/demo (/demo/#full opens on the included one).
Run it
It runs the package from its source, so a change in packages/react shows at once. That is the one line of its vite.config.ts a copy in a project of yours leaves out: there, @rebodocs/react comes from npm.
git clone https://github.com/JFranciscoRobles/rebodocs
cd rebodocs
npm install
npm run demo # http://localhost:5175, and /#headless for the second tabOpen a .docx opens a file in both tabs; there is no backend, so saving downloads it.
What is where
- src/App.tsx
- The two tabs, opening a file, and the sample it starts with. Holds the docx both editors are given.
- src/FullEditor.tsx
- DocsEditor, in a dozen lines.
- src/HeadlessEditor.tsx
- The provider and the layout: title bar, formatting bar, insert rail, find bar, ruler, pages, status bar.
- src/headless/Panels.tsx
- The side panel and its four tabs: format, page, content, notes.
- src/headless/texts.ts
- The interface’s own words, in Spanish and English, and useTexts, which draws a component again when the language changes.
- src/headless/Icon.tsx · options.ts
- The icons, and the lists offered: sizes, zooms, line heights, highlights, cell shades.
- src/download.ts
- Handing a file to the person: there is no backend, so saving downloads the DOCX.
- src/HeadlessEditor.css
- All of its look. Every rule is under .hd, so it touches nothing else; the colours of what the library puts in the body are set on body:has(.hd).
- scripts/sample.ts
- Writes the sample agreement with the engine, without a browser, in English.
Which piece uses what
Each hook is on the page Components and hooks, with what it takes and gives.
- Name, unsaved mark, save, export to Word, PDF and Markdown, print
- useDocs: state.name, state.dirty, docs.save(), docs.download(), editor.exportPdf(), editor.exportMarkdown(), commands.print()
- Undo, style, font, size, bold…, colour, alignment, lists, indent, zoom
- useDocs, useDocsStyles, fontFamilies, useDocsValue for the indent
- Table, pictures, signatures, page break
- commands.insertTable, insertImageFile, insertImageGrid, insertSignatures, insertPageBreak
- Link, variable and note buttons, and ⌘K
- useDocsBubble: bubble.ask(…), with <DocsProvider bubble>; useDocsLink, useDocsVariables and useDocsNotes say when each is on
- Find and replace, and ⌘F
- useDocsFind, opened by onFindKey
- Ruler
- DocsRuler, coloured with --dx-ruler…
- Format tab: highlight, spacing, the cells of a table
- useDocs, useDocsValue for activeParagraphProps(), activeCellProps() and tableBorders()
- Page tab: size, margins, columns, letterhead, header and footer, page number
- useDocsPage, useDocsValue for the columns, commands.setArea
- Content tab: table of contents, outline to drag, variables with a type
- useDocsOutline, useDocsVariables, commands.insertToc
- Notes tab
- useDocsNotes, NOTE_COLORS, docs.setAuthor
- Status bar: page, style and font, words and characters, saved, language, view, notes, zoom
- useDocs, useDocsStyles, useDocsCount, setLocale, commands.setView, setShowNotes, setZoom
- A narrow window
- useDocsResponsive, and a media query for the layout
The skeleton
Everything hangs from one provider. What a key must reach (⌘F opens the find bar) is above it; the rest is inside. This is the end of HeadlessEditor.tsx, read from the file.
/** What is inside the provider: the layout, and what a narrow window asks of it. */
function Shell({ finding, setFinding }: { finding: boolean; setFinding: (finding: boolean) => void }) {
const tr = useTexts();
const [panels, setPanels] = useState(false);
const locale = useSyncExternalStore(onLocaleChange, getLocale);
// The layout follows the window. The library fits the pages to the room they are left, and says how much the keyboard of the screen covers.
const narrow = useSyncExternalStore(watchNarrow, () => window.matchMedia(NARROW).matches);
const { keyboard } = useDocsResponsive();
return (
<div className={`hd ${narrow ? 'narrow' : ''}`} style={{ paddingBottom: keyboard || undefined }}>
<TitleBar panels={panels} onPanels={() => setPanels(!panels)} />
<Toolbar />
<div className="hd-body">
<Rail finding={finding} onFind={() => setFinding(!finding)} />
<main className="hd-main">
{finding && <FindBar onClose={() => setFinding(false)} />}
{/* Made again in another language: what its markers are called is read when it is made. A narrow window has no room for it. */}
{!narrow && <DocsRuler key={locale} />}
<DocsSurface className="hd-surface" />
</main>
{narrow && panels && <button className="hd-scrim" aria-label={tr('Cerrar paneles')} onClick={() => setPanels(false)} />}
<Panels open={panels} onClose={() => setPanels(false)} />
</div>
<StatusBar />
</div>
);
}
export function HeadlessEditor({ docx, name }: Props) {
// Up here, where the provider is given who answers ⌘F.
const [finding, setFinding] = useState(false);
return (
<DocsProvider docx={docx} name={name} author="Demo" onSave={download} bubble slash variableSuggestions forms spellcheck={spellcheck} onFindKey={() => setFinding(true)}>
<Shell finding={finding} setFinding={setFinding} />
</DocsProvider>
);
}The column the pages are in is display: flex; flex-direction: column; min-width: 0, and the surface flex: 1; min-height: 0: that is what gives it the height it needs and lets it scroll. Under 900 pixels of window a media query lays it out again: the bars become one row each that slides, the insert rail lies down, the panels open over the document from a button, and the ruler goes.
Things it had to get right
App keeps the bytes in state and gives the same Uint8Array at every render. A new array each time would open the document again and lose what was typed.
Every command gives the keyboard back to the document, so buttons need nothing. A select or an input takes it while it is used, and the command it ends in gives it back.
state.color is hex without #; a colour input wants one with it: options.ts has the line between them. A highlight is one of Word’s fifteen, so it is offered as swatches.
A document may use a size or a zoom that is not in the list offered. withCurrent adds the present one, so a select never shows something it is not.
The two indent buttons change the level of an item in a list (shiftListLevel) and the indent of a paragraph outside one (shiftIndent), as the included toolbar does.
The selector calls setLocale: the library’s own words change (the bar, the ruler, the names of styles), and useTexts draws the demo’s again. The ruler is made again, with a key, to take its words.
The library’s own pieces take its colours from variables, not from rules that reach into their classes: the ruler from --dx-ruler… set on .hd; the bar beside the selection, and the rest the library puts in document.body outside .hd, from the --dx-ui-… variables set on body:has(.hd), while the interface is on the page.
Using it for a demonstration
- rebodocs.com/demo has it online, with nothing to install.
npm run demoand open/#headlessfor your own interface, or/for the included one.- Open a .docx shows a real document of the person you are showing it to, in both.
npm run build:demowritesapps/demo/dist, a static site to put anywhere.- To change what it opens with, edit
scripts/sample.ts:npm run sample -w @rebodocs/demowrites the file again.