Customization

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>

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>
ConfigurationUse
Toolbar.Slot with slotIdReplaces or hides one registered control.
Toolbar.Group with a built-in idAdds children to that group. hidden removes it.
preset={false} on ToolbarYou compose every visible control.
onSave on ToolbarDecides where saved bytes go.
t on ToolbarResolves labels. Defaults to the active LocaleProvider catalog.
overflow={false} on ToolbarWraps 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>
Toolbar.Action propUse
labelRequired accessible name and tooltip.
icon, childrenVisible content.
onSelectRuns your action.
activeSets aria-pressed and the active state.
disabled, disabledReasonDisables the action and explains why.
asChild, classNameCustom 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 propUse
idA built-in id (typed as ChromeGroupId) or your own id.
labelAccessible name and overflow heading. Overrides labelKey.
labelKeyCatalog key for the label.
afterPlaces an application group after another group.
priorityLower numbers move to overflow first.
pinnedKeeps the group visible at every width.
hiddenRemoves the group and its separator.
overflowContentContent shown inside the overflow menu.
classNameStyles 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 priority move to overflow before built-in groups.
  • Fonts, colors, styles, image wrap, and table properties need value controls. An ordinary command button cannot replace them.
  • FontFamily and StylePicker have Trigger, Content, and Item parts. Their hooks return null for a mixed selection.
  • Table controls appear only when the selection is in a table.

See also