Customization

Menus

Customize the DOCX editor menu bar and context menu with packaged commands, application actions, and file handlers.

Compose DocxEditor.Menu and DocxEditor.ContextMenu independently. Packaged rows take their label, icon, state, and command from the editor.

Customize menu actions

Set preset={false} to choose every row. ContextMenu.Slot with a slotId renders a packaged command.

<DocxEditor.ContextMenu preset={false}>
  <DocxEditor.ContextMenu.Cut />
  <DocxEditor.ContextMenu.Copy />
  <DocxEditor.ContextMenu.Slot slotId="format.clear" />
</DocxEditor.ContextMenu>

To remove one default row, add its slot or named part with hidden: <DocxEditor.ContextMenu.Slot slotId="review.addComment" hidden />.

For slot identifiers, see Chrome slot reference.

Add an application action

ContextMenu.Item runs your handler. To disable it, pass disabled and disabledReason.

<DocxEditor.ContextMenu>
  <DocxEditor.ContextMenu.Item label="Ask legal" onSelect={askLegal} />
</DocxEditor.ContextMenu>

Group rows with Group, Separator, and Submenu.

Place rows above the default rows

Your rows render after the default rows. To put rows at the top of the menu, wrap them in ContextMenu.Start. Use it for rows about the item under the pointer, such as suggestions for the word that you right-click. To read that item, call one of these hooks in a component inside Start:

  • useContextMenuTarget() returns the element that you right-click.
  • useContextMenuAnchor() returns { x, y, trigger }, where the menu opened and how.
  • useContextMenuHighlights(name?) returns the highlights under the pointer, or at the caret for a keyboard open, topmost first. For an example, see Read highlights in the context menu.

Shift+F10 and the Menu key open the menu below the caret. A menu opened from the keyboard has no target element. To tell the two cases apart, call useContextMenuAnchor() and check trigger, which is 'pointer' or 'keyboard'. For a keyboard open, read the caret with editor.getSelectionRange(), and compare head.blockId and head.offset with your own ranges. For more information, see Read the selection and the caret.

ContextMenu.Start adds a separator after its rows. If Start renders no rows, the separator stays hidden. You can use more than one Start, and the menu shows them in written order.

<DocxEditor.ContextMenu>
  <DocxEditor.ContextMenu.Start>
    <DocxEditor.ContextMenu.Item label="Explain clause" onSelect={explainClause} />
    <Suggestions />
  </DocxEditor.ContextMenu.Start>
</DocxEditor.ContextMenu>

Keep these rules in mind:

  • Put ContextMenu.Start directly inside ContextMenu. A Fragment around it is fine. If your own component returns Start, the rows render at the end of the menu, and development builds print a warning. Put your component inside Start instead, as Suggestions does in the example.
  • Put only your own rows in Start. A packaged row, such as ContextMenu.Copy, also renders in its default place, so it appears twice. Development builds print a warning.
  • With preset={false}, the menu renders every row in written order, so Start stays where you write it.

Configure file actions

Set these props on DocxEditor.Menu.

PropBehavior
onOpenReplaces the default file picker.
onOpenFileReceives the file before the default open action loads it.
onSaveReplaces the default save and download.
fileNameDefault download name, without extension.
exportersMarkdown and PDF conversion handlers. Print uses the PDF handler.
printSettingstrue (default) asks what markup to print. false prints the current view.
onPageSetupOpens your UI when popups.pageSetup is absent.
onReportIssueReplaces the support action. reportIssue={false} removes it.

For converter wiring, see Export and Print.

Configure menu parts

Every part accepts className. Most accept hidden and children.

PartOptions
DocxEditor.Menut, preset, overflow, child menus.
DocxEditor.ContextMenut, preset, disabled, onOpenChange.
Named menu: File, Format, Insert, Review, Helppreset. Replaces its menu in place.
Menu.Entryid, label or labelKey, icon, preset. Appends a menu.
Menu.ItemslotId, labelKey, shortcutKey.
Context menu command partsicon, labelKey, shortcutKey.
Menu.Rowicon, shortcut, disabled, title, active, selected, onSelect.
Menu.Group, Menu.Submenulabel or labelKey. Submenu also takes paths.
Menu.SeparatorclassName only.

Use title to explain a disabled row.

Limits

  • A hidden row does not block its operation in other controls, shortcuts, or the API.
  • A missing export handler produces an error when the user requests that conversion.
  • popups.export: false hides export progress. It does not cancel conversion.
  • Browser clipboard access can refuse Paste. Keep the supplied refusal feedback.
  • Built-in menus handle keyboard navigation, Escape, dismissal, and selection protection, also for custom rows. A menu built from hooks must do this itself.
  • The context menu opens on pages and the canvas around them. disabled restores the browser menu everywhere.

See also