Toolbar
Customize the DOCX editor toolbar with named controls, slots, application actions, groups, and overflow.
Customize the toolbar through named parts, slots, and groups. Each control keeps its command, enabled state, and refusal reason.
Customize a toolbar control
Pass className, icon, hidden, or asChild to a named part.
<DocxEditor.Toolbar>
<DocxEditor.Toolbar.Bold icon={<span>Bold</span>} className="contract-button" />
<DocxEditor.Toolbar.Italic hidden />
</DocxEditor.Toolbar><DocxEditor.Toolbar>
<DocxEditor.Toolbar.Bold class-name="contract-button" />
<DocxEditor.Toolbar.Italic hidden />
</DocxEditor.Toolbar>hidden on a named part equals <Toolbar.Slot slotId="text.italic" hidden />. With asChild, forward the part's attributes and events.
Choose the toolbar arrangement
Replace one built-in control with Toolbar.Slot. Your content keeps the control's place and collapses with its group.
<DocxEditor.Toolbar>
<DocxEditor.Toolbar.Slot slotId="zoom.level">
<MyZoomPicker />
</DocxEditor.Toolbar.Slot>
</DocxEditor.Toolbar><DocxEditor.Toolbar>
<DocxEditor.Toolbar.Slot slot-id="zoom.level">
<MyZoomPicker />
</DocxEditor.Toolbar.Slot>
</DocxEditor.Toolbar>| Configuration | Use |
|---|---|
Toolbar.Slot with slotId | Replaces or hides one registered control. |
Toolbar.Group with a built-in id | Adds children to that group. hidden removes it. |
preset={false} on Toolbar | You compose every visible control. |
onSave on Toolbar | Decides where saved bytes go. |
t on Toolbar | Resolves labels. Defaults to the active LocaleProvider catalog. |
overflow={false} on Toolbar | Wraps controls instead of moving them to More. |
For every group and slot identifier, see Chrome slots.
Add application actions
Put a Toolbar.Action inside your own Toolbar.Group. The action keeps the document selection.
<DocxEditor.Toolbar>
<DocxEditor.Toolbar.Group id="contract" label="Contract" priority={15}>
<DocxEditor.Toolbar.Action label="Ask legal" onSelect={askLegal} />
</DocxEditor.Toolbar.Group>
</DocxEditor.Toolbar><DocxEditor.Toolbar>
<DocxEditor.Toolbar.Group id="contract" label="Contract" :priority="15">
<DocxEditor.Toolbar.Action label="Ask legal" @select="askLegal" />
</DocxEditor.Toolbar.Group>
</DocxEditor.Toolbar>Toolbar.Action prop | Use |
|---|---|
label | Required accessible name and tooltip. |
icon, children | Visible content. |
onSelect | Runs your action. |
active | Sets aria-pressed and the active state. |
disabled, disabledReason | Disables the action and explains why. |
asChild, className | Custom element and styling. |
For an editor command, use useEditorCommand(slotId). It returns execute, isActive, isEnabled, and disabledReason. Show disabledReason on refusal.
Configure overflow
When width runs out, an application group moves into More as one unit. Built-in groups give up one control at a time.
Toolbar.Group prop | Use |
|---|---|
id | A built-in id (typed as ChromeGroupId) or your own id. |
label | Accessible name and overflow heading. Overrides labelKey. |
labelKey | Catalog key for the label. |
after | Places an application group after another group. |
priority | Lower numbers move to overflow first. |
pinned | Keeps the group visible at every width. |
hidden | Removes the group and its separator. |
overflowContent | Content shown inside the overflow menu. |
className | Styles the group wrapper. |
For the full overflow rules, see Toolbar guide.
Build a value menu
Value operations, such as line spacing, need a value. This React menu lists line-spacing rows from @docx-editor.dev/core/editor.
import { lineSpacingMenuRowLabel, lineSpacingMenuRows } from '@docx-editor.dev/core/editor';
import {
useDocxEditor,
useEditorState,
useTranslation,
type TranslationKey,
} from '@docx-editor.dev/react';
function MyLineSpacingMenu() {
const editor = useDocxEditor();
const { t, lang } = useTranslation();
const current = useEditorState((s) => s.formatting?.lineSpacing ?? null);
return lineSpacingMenuRows(current).map((row) => (
<button
key={`${row.spacing.rule}:${row.spacing.value}`}
role="menuitemradio"
aria-checked={row.selected}
onMouseDown={(event) => event.preventDefault()}
onClick={() => row.command && editor?.exec(row.command)}
>
{lineSpacingMenuRowLabel(row, (key) => t(key as TranslationKey), lang)}
</button>
));
}The selected row has a null command. A mixed selection selects no row. A spacing that matches no preset adds its own selected row, and a paragraph with no direct or inherited line spacing reports single spacing.
Limits
- Hiding a control does not disable its operation elsewhere. Use editor mode or document protection for permissions.
- Custom buttons must call
preventDefault()on mouse down to keep the caret. Skip inputs, selects, and text areas so they take focus normally. - Loose children outside a group never move into overflow.
- Application groups without a
prioritymove to overflow before built-in groups. - Fonts, colors, styles, image wrap, and table properties need value controls. An ordinary command button cannot replace them.
FontFamilyandStylePickerhaveTrigger,Content, andItemparts. Their hooks returnnullfor a mixed selection.- Table controls appear only when the selection is in a table.