ContextMenu
Open item-specific actions at a context interaction point.
Anatomy#
Content is portaled; grouped items use the same item subtree as ungrouped items.
Usage#
Use ContextMenu for actions tied to a specific surface or item at the pointer location. Keep essential actions reachable through another visible path.
- Use DropdownMenu when the same actions open from a visible trigger.
- Use Popover when the content is not a list of commands.
- Use Dialog when the user must complete or confirm a modal task.
Trigger#
Wrap the target with ContextMenu.Trigger. The menu opens at the pointer on right-click or a touch
or pen long press (700 ms), and at the center of the trigger when opened from the keyboard.
The trigger renders a div; pass as to render another element. With disabled on the root, the
native browser menu shows instead.
Items#
The items array describes the whole menu. An item has type 'item' (the default), 'checkbox',
'radio-group', 'group', or 'separator'; an item with children opens a submenu. An entry that
is an array of items forms its own group, and the menu draws a separator between groups.
Items accept icon, kbds, disabled, and danger; a separator takes no other
options. kbds only displays keycaps and does not register a shortcut.
Checkbox and radio state is controlled with checked / onCheckedChange and with value /
onValueChange on the radio-group (not on each radio). Omit them to let the item manage its own
state from defaultChecked or defaultValue. Selecting a checkbox or radio keeps the menu open;
selecting a regular item runs onSelect and closes it.
A parent item with children can take open and onOpenChange to control its submenu.
onOpenChange reports every open or close request from pointer, keyboard, or click interaction. With
a controlled open, the submenu stays at the prop value until you update it.
ContextMenu.Content also takes headerRender and footerRender components to add a header (such
as the target’s metadata) or a footer around the item groups. The root menu and every open submenu
mount their own copy, and each receives { sub }.
itemRender replaces the inner content of every item row; the row keeps its role, keyboard
handling, and selection.
Dismissal and scrolling#
Escape and outside presses close the menu. Set closeOnEscape or closeOnOutsideClick to false to
block them, and use onClosePrevented to react to the blocked attempt. While the menu is open, body
scroll is locked by default; set preventScroll to false to keep the page scrollable.
Keyboard interaction#
| Key | Description |
|---|---|
| Shift + F10 / ContextMenu | Toggles the menu from the focused trigger. |
| ↓ / ↑ | Moves focus to the next or previous item. |
| ↖ / ↘ | Moves focus to the first or last item. |
| Printable characters | Moves focus to the next item whose text starts with the typed characters. |
| → | Opens the submenu of the focused item. |
| ← | Closes the current submenu and returns to its parent item. |
| ↵ / Space | Activates the focused item, or opens its submenu. |
| Esc | Closes the current menu layer and restores focus. |
| ⇥ | Closes the menu. |
Examples#
Basic#
Right-click or long-press the trigger area to open the menu. Individual items can be marked disabled.
Grouped items#
Pass arrays of items to form groups; the menu draws a separator between them. danger: true marks
the destructive action.
Stateful actions#
Combine submenus, stateful toggles, disabled items, and dynamic actions in one item model.
Checkbox and radio#
Bind checkbox and radio menu items to application state.
Submenus#
Pass nested children on an item to open a submenu.
Long press#
Touch and hold for ~700 ms on touch devices, or right-click with a pointer, to open the menu.
API Reference#
Attributes#
data-closeddata-disableddata-expandeddata-aligndata-sidedata-dangerdata-highlighteddata-selectedProps#
ContextMenu#
Trigger#
Renders a <div> element by default.
Content#
Props for the ContextMenu content component. Renders a <div> element by default.