Context Menu
A menu of actions for an element, opened with a right-click or a long press.
<ContextMenu>
<ContextMenuTrigger className="flex h-36 w-full max-w-sm items-center justify-center rounded-lg border border-dashed border-border-strong text-text-secondary">
Right-click this alert
</ContextMenuTrigger>
<ContextMenuContent className="w-52">
<ContextMenuItem>
<EyeIcon />
View details
</ContextMenuItem>
<ContextMenuItem>
<UserPlusIcon />
Assign to me
<ContextMenuShortcut>A</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem>
<CopyIcon />
Copy alert ID
<ContextMenuShortcut>⌘C</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="destructive">
<Trash2Icon />
Delete alert
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>Usage
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuTrigger,
} from "@zenoxai/ui/components/context-menu"
<ContextMenu>
<ContextMenuTrigger>{/* the alert row, card or chart */}</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem onClick={viewDetails}>View details</ContextMenuItem>
<ContextMenuItem onClick={assign}>Assign to me</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
A context menu opens at the pointer when the user right-clicks the trigger area, or long-presses it on a touch screen. It is a shortcut for people who know it is there: a faster way to reach the actions of a table row, an asset card or a chart.
- Every action in a context menu must also be reachable from a visible control, usually a Dropdown Menu on the same row. Keyboard users and many touch users never open a context menu.
- For the menus at the top of an editor or app, use Menubar.
ContextMenuTrigger renders a <div> around the area that responds to the right-click and
disables text selection inside it. The menu offers the same items as
Dropdown Menu: items with icons and shortcuts, a destructive
variant, checkbox and radio items, labels, separators and submenus.
Examples
Submenu
ContextMenuSub nests a second menu that opens to the side.
<ContextMenu>
<ContextMenuTrigger className="flex h-36 w-full max-w-sm items-center justify-center rounded-lg border border-dashed border-border-strong text-text-secondary">
Right-click srv-db-01.prod
</ContextMenuTrigger>
<ContextMenuContent className="w-52">
<ContextMenuItem>Open asset</ContextMenuItem>
<ContextMenuItem>Run vulnerability scan</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger>Move to tenant</ContextMenuSubTrigger>
<ContextMenuSubContent>
<ContextMenuItem>Acme Corp</ContextMenuItem>
<ContextMenuItem>Globex</ContextMenuItem>
<ContextMenuItem>Initech</ContextMenuItem>
</ContextMenuSubContent>
</ContextMenuSub>
<ContextMenuSeparator />
<ContextMenuItem variant="destructive">
Remove from inventory
</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>Checkbox and radio items
Settings of the element under the pointer, such as the display options of a chart, fit checkbox and radio items. They keep the menu open when toggled.
function ContextMenuSettings() {
const [showGrid, setShowGrid] = React.useState(true)
const [showLegend, setShowLegend] = React.useState(false)
const [range, setRange] = React.useState("7d")
return (
<ContextMenu>
<ContextMenuTrigger className="flex h-36 w-full max-w-sm items-center justify-center rounded-lg border border-dashed border-border-strong text-text-secondary">
Right-click the alerts chart
</ContextMenuTrigger>
<ContextMenuContent className="w-52">
<ContextMenuCheckboxItem
checked={showGrid}
onCheckedChange={setShowGrid}
>
Show grid lines
</ContextMenuCheckboxItem>
<ContextMenuCheckboxItem
checked={showLegend}
onCheckedChange={setShowLegend}
>
Show legend
</ContextMenuCheckboxItem>
<ContextMenuSeparator />
<ContextMenuLabel>Time range</ContextMenuLabel>
<ContextMenuRadioGroup value={range} onValueChange={setRange}>
<ContextMenuRadioItem value="24h">Last 24 hours</ContextMenuRadioItem>
<ContextMenuRadioItem value="7d">Last 7 days</ContextMenuRadioItem>
<ContextMenuRadioItem value="30d">Last 30 days</ContextMenuRadioItem>
</ContextMenuRadioGroup>
</ContextMenuContent>
</ContextMenu>
)
}Accessibility
- Inside the open menu, arrow keys move between items, Enter runs the focused item and Escape closes the menu. Opening it from the keyboard depends on the browser and operating system (Shift+F10 or the context menu key), which is why the actions need a visible alternative.
ContextMenuLabelis a visual heading and is not announced as the group’s name.
API reference
Generated from the TypeScript source. className, style and children are accepted everywhere and omitted below.
ContextMenu
ContextMenu is the menu that opens on right-click or long press over its ContextMenuTrigger, with actions for what is under the pointer. For a menu opened from a visible button, use DropdownMenu.
| Prop | Type | Default |
|---|---|---|
actionsRef | RefObject<MenuRootActions | null>A ref to imperative actions. - `unmount`: Ends the closing phase of the menu after an externally controlled closing animation finishes. Call `preventUnmountOnClose()` in `onOpenChange` first, otherwise the menu completes closing on its own. Whether it leaves the DOM is decided by `keepMounted` on the portal. - `close`: Closes the menu imperatively when called. | - |
closeParentOnEsc | boolean@ignore @deprecated This prop has no effect on Context Menu. | - |
defaultOpen | booleanWhether the menu is initially open. | false |
disabled | booleanWhether the component should ignore user interaction. | false |
highlightItemOnHover | booleanWhether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state. | true |
loopFocus | booleanWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys. | true |
onItemHighlighted | ((highlightedItem: HTMLElement, eventDetails: MenuRootHighlightEventDetails) => void)Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item element (or `undefined` if no item is highlighted) and details containing the reason for the change, the event, and the item's text label. The `reason` can be: - `'keyboard'`: the highlight changed due to keyboard navigation. - `'pointer'`: the highlight changed due to pointer hovering. The event may be a `MouseEvent` rather than a `PointerEvent`. - `'none'`: the highlight changed for another reason, such as automatic highlighting while filtering, the item list changing, or the popup opening or closing. | - |
onOpenChange | ((open: boolean, eventDetails: ContextMenuRootChangeEventDetails) => void)Event handler called when the menu is opened or closed. | - |
onOpenChangeComplete | ((open: boolean) => void)Event handler called after any animations complete when the menu is opened or closed. | - |
open | booleanWhether the menu is currently open. | - |
orientation | "horizontal" | "vertical"The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys. | 'vertical' |
ContextMenuTrigger
ContextMenuTrigger is the area that opens the menu on right-click.
| Prop | Type | Default |
|---|---|---|
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, ContextMenuTriggerState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
ContextMenuContent
ContextMenuContent is the menu popup, opened at the pointer.
| Prop | Type | Default |
|---|---|---|
align | "center" | "start" | "end"How to align the popup relative to the specified side. | start |
alignOffset | number | OffsetFunctionAdditional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment. | 4 |
finalFocus | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)Determines the element to focus when the menu is closed. | - |
id | string@ignore | - |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuPopupState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
side | "left" | "right" | "top" | "bottom" | "inline-end" | "inline-start"Which side of the anchor element to align the popup against. May automatically change to avoid collisions. | right |
sideOffset | number | OffsetFunctionDistance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment. | 0 |
ContextMenuItem
| Prop | Type | Default |
|---|---|---|
closeOnClick | booleanWhether to close the menu when the item is clicked. | true |
disabled | booleanWhether the component should ignore user interaction. | false |
id | string@ignore | - |
inset | booleanIndents the text to line up with items that have a leading icon. | - |
label | stringOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided. | - |
nativeButton | booleanWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | true |
onClick | ((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)The click handler for the menu item. | - |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuItemState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
variant | "default" | "destructive"`destructive` colors the item red, for delete and other risky actions. | default |
ContextMenuCheckboxItem
| Prop | Type | Default |
|---|---|---|
checked | booleanWhether the checkbox item is currently ticked. | - |
closeOnClick | booleanWhether to close the menu when the item is clicked. | false |
defaultChecked | booleanWhether the checkbox item is initially ticked. | false |
disabled | booleanWhether the component should ignore user interaction. | false |
id | string@ignore | - |
inset | booleanIndents the text to line up with items that have a leading icon. | - |
label | stringOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided. | - |
nativeButton | booleanWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | true |
onCheckedChange | ((checked: boolean, eventDetails: MenuRootChangeEventDetails) => void)Event handler called when the checkbox item is ticked or unticked. | - |
onClick | ((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)The click handler for the menu item. | - |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuCheckboxItemState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
ContextMenuRadioItem
| Prop | Type | Default |
|---|---|---|
closeOnClick | booleanWhether to close the menu when the item is clicked. | false |
disabled | booleanWhether the component should ignore user interaction. | false |
id | string@ignore | - |
inset | booleanIndents the text to line up with items that have a leading icon. | - |
label | stringOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided. | - |
nativeButton | booleanWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | true |
onClick | ((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void)The click handler for the menu item. | - |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuRadioItemState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
value* | anyValue of the radio item. This is the value that will be set in the MenuRadioGroup when the item is selected. | - |
ContextMenuLabel
ContextMenuLabel is a section heading inside the menu. It is a plain div, not linked to the items under it, because Base UI's GroupLabel throws outside a Group. When the items need that aria-labelledby association, wrap them in a ContextMenuGroup with `ContextMenu.GroupLabel` from "@base-ui/react/context-menu".
| Prop | Type | Default |
|---|---|---|
inset | booleanIndents the text to line up with items that have a leading icon. | - |
ContextMenuSeparator
| Prop | Type | Default |
|---|---|---|
orientation | "horizontal" | "vertical"The orientation of the separator. | vertical |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, SeparatorState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
ContextMenuShortcut
ContextMenuShortcut is the key hint at the right edge of an item. It is display text only: bind the keys yourself.
No props of its own. Accepts the props of the element it renders.
ContextMenuGroup
| Prop | Type | Default |
|---|---|---|
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuGroupState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
ContextMenuPortal
| Prop | Type | Default |
|---|---|---|
container | HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | nullA parent element to render the portal element into. | - |
keepMounted | booleanWhether to keep the portal mounted in the DOM while the popup is hidden. | false |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuPortalState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
ContextMenuSub
| Prop | Type | Default |
|---|---|---|
actionsRef | RefObject<MenuRootActions | null>A ref to imperative actions. - `unmount`: Ends the closing phase of the menu after an externally controlled closing animation finishes. Call `preventUnmountOnClose()` in `onOpenChange` first, otherwise the menu completes closing on its own. Whether it leaves the DOM is decided by `keepMounted` on the portal. - `close`: Closes the menu imperatively when called. | - |
closeParentOnEsc | booleanWhen in a submenu, determines whether pressing the Escape key closes the entire menu, or only the current child menu. | false |
defaultOpen | booleanWhether the menu is initially open. | false |
disabled | booleanWhether the component should ignore user interaction. | false |
highlightItemOnHover | booleanWhether moving the pointer over items should highlight them. Disabling this prop allows CSS `:hover` to be differentiated from the `:focus` (`data-highlighted`) state. | true |
loopFocus | booleanWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys. | true |
onItemHighlighted | ((highlightedItem: HTMLElement, eventDetails: MenuRootHighlightEventDetails) => void)Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item element (or `undefined` if no item is highlighted) and details containing the reason for the change, the event, and the item's text label. The `reason` can be: - `'keyboard'`: the highlight changed due to keyboard navigation. - `'pointer'`: the highlight changed due to pointer hovering. The event may be a `MouseEvent` rather than a `PointerEvent`. - `'none'`: the highlight changed for another reason, such as automatic highlighting while filtering, the item list changing, or the popup opening or closing. | - |
onOpenChange | ((open: boolean, eventDetails: MenuRootChangeEventDetails) => void)Event handler called when the menu is opened or closed. | - |
onOpenChangeComplete | ((open: boolean) => void)Event handler called after any animations complete when the menu is opened or closed. | - |
open | booleanWhether the menu is currently open. | - |
orientation | "horizontal" | "vertical"The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys. | 'vertical' |
ContextMenuSubContent
| Prop | Type | Default |
|---|---|---|
align | "center" | "start" | "end"How to align the popup relative to the specified side. | start |
alignOffset | number | OffsetFunctionAdditional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment. | 4 |
finalFocus | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)Determines the element to focus when the menu is closed. | - |
id | string@ignore | - |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuPopupState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
side | "left" | "right" | "top" | "bottom" | "inline-end" | "inline-start"Which side of the anchor element to align the popup against. May automatically change to avoid collisions. | right |
sideOffset | number | OffsetFunctionDistance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment. | 0 |
ContextMenuSubTrigger
| Prop | Type | Default |
|---|---|---|
closeDelay | numberHow long to wait before closing the menu that was opened on hover. Specified in milliseconds. | 0 |
delay | numberHow long to wait before the menu may be opened on hover. Specified in milliseconds. | 100 |
disabled | booleanWhether the component should ignore user interaction. | false |
id | string@ignore | - |
inset | booleanIndents the text to line up with items that have a leading icon. | - |
label | stringOverrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided. | - |
nativeButton | booleanWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). | true |
onClick | ((event: BaseUIEvent<MouseEvent<HTMLDivElement, MouseEvent>>) => void) | - |
openOnHover | booleanWhether the menu should also open when the trigger is hovered. | true |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuSubmenuTriggerState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
ContextMenuRadioGroup
| Prop | Type | Default |
|---|---|---|
defaultValue | anyThe uncontrolled value of the radio item that should be initially selected. | - |
disabled | booleanWhether the component should ignore user interaction. | false |
onValueChange | ((value: any, eventDetails: MenuRootChangeEventDetails) => void)Function called when the selected value changes. | - |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, MenuRadioGroupState>Allows you to replace the component's HTML element with a different tag, or compose it with another component. | - |
value | anyThe controlled value of the radio item that should be currently selected. | - |