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><DocxEditor.ContextMenu :preset="false">
<DocxEditor.ContextMenu.Cut />
<DocxEditor.ContextMenu.Copy />
<DocxEditor.ContextMenu.Slot slot-id="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><DocxEditor.ContextMenu>
<DocxEditor.ContextMenu.Item label="Ask legal" @select="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><DocxEditor.ContextMenu>
<DocxEditor.ContextMenu.Start>
<DocxEditor.ContextMenu.Item label="Explain clause" @select="explainClause" />
<Suggestions />
</DocxEditor.ContextMenu.Start>
</DocxEditor.ContextMenu>Keep these rules in mind:
- Put
ContextMenu.Startdirectly insideContextMenu. A Fragment around it is fine. If your own component returnsStart, the rows render at the end of the menu, and development builds print a warning. Put your component insideStartinstead, asSuggestionsdoes in the example. - Put only your own rows in
Start. A packaged row, such asContextMenu.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, soStartstays where you write it.
Configure file actions
Set these props on DocxEditor.Menu.
| Prop | Behavior |
|---|---|
onOpen | Replaces the default file picker. |
onOpenFile | Receives the file before the default open action loads it. |
onSave | Replaces the default save and download. |
fileName | Default download name, without extension. |
exporters | Markdown and PDF conversion handlers. Print uses the PDF handler. |
printSettings | true (default) asks what markup to print. false prints the current view. |
onPageSetup | Opens your UI when popups.pageSetup is absent. |
onReportIssue | Replaces 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.
| Part | Options |
|---|---|
DocxEditor.Menu | t, preset, overflow, child menus. |
DocxEditor.ContextMenu | t, preset, disabled, onOpenChange. |
Named menu: File, Format, Insert, Review, Help | preset. Replaces its menu in place. |
Menu.Entry | id, label or labelKey, icon, preset. Appends a menu. |
Menu.Item | slotId, labelKey, shortcutKey. |
| Context menu command parts | icon, labelKey, shortcutKey. |
Menu.Row | icon, shortcut, disabled, title, active, selected, onSelect. |
Menu.Group, Menu.Submenu | label or labelKey. Submenu also takes paths. |
Menu.Separator | className 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: falsehides 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.
disabledrestores the browser menu everywhere.