Context Menu
A menu that appears at the pointer on right click or long press.
API reference
Import the components and place them together:
import { ContextMenu } from '@base-ui-components/react/context-menu';
<ContextMenu.Root>
<ContextMenu.Trigger />
<ContextMenu.Portal>
<ContextMenu.Backdrop />
<ContextMenu.Positioner>
<ContextMenu.Popup>
<ContextMenu.Arrow />
<ContextMenu.Item />
<ContextMenu.Separator />
<ContextMenu.Group>
<ContextMenu.GroupLabel />
</ContextMenu.Group>
<ContextMenu.RadioGroup>
<ContextMenu.RadioItem />
</ContextMenu.RadioGroup>
<ContextMenu.CheckboxItem />
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
Root
A component that creates a context menu activated by right clicking or long pressing. Doesn’t render its own HTML element.
Prop | Type | Default | |
---|---|---|---|
defaultOpen |
|
| |
open |
|
| |
onOpenChange |
|
| |
actionsRef |
|
| |
closeParentOnEsc |
|
| |
modal |
|
| |
onOpenChangeComplete |
|
| |
disabled |
|
| |
openOnHover |
|
| |
delay |
|
| |
loop |
|
| |
orientation |
|
| |
children* |
|
|
Trigger
An area that opens the menu on right click or long press.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Portal
A portal element that moves the popup to a different part of the DOM.
By default, the portal element is appended to <body>
.
Prop | Type | Default | |
---|---|---|---|
container |
|
| |
children |
|
| |
keepMounted |
|
|
Positioner
Positions the menu popup against the trigger.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
align |
|
| |
alignOffset |
|
| |
side |
|
| |
sideOffset |
|
| |
arrowPadding |
|
| |
anchor |
|
| |
collisionBoundary |
|
| |
collisionPadding |
|
| |
sticky |
|
| |
positionMethod |
|
| |
trackAnchor |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-open | Present when the menu popup is open. | |
data-closed | Present when the menu popup is closed. | |
data-anchor-hidden | Present when the anchor is hidden. | |
data-align | Indicates how the popup is aligned relative to specified side. | |
data-side | Indicates which side the popup is positioned relative to the trigger. |
CSS Variable | Description | |
---|---|---|
--anchor-height | The anchor's height. | |
--anchor-width | The anchor's width. | |
--available-height | The available height between the trigger and the edge of the viewport. | |
--available-width | The available width between the trigger and the edge of the viewport. | |
--transform-origin | The coordinates that this element is anchored to. Used for animations and transitions. |
Popup
A container for the menu items.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
id |
|
| |
children |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-open | Present when the menu is open. | |
data-closed | Present when the menu is closed. | |
data-align | Indicates how the popup is aligned relative to specified side. | |
data-instant | Present if animations should be instant. | |
data-side | Indicates which side the popup is positioned relative to the trigger. | |
data-starting-style | Present when the menu is animating in. | |
data-ending-style | Present when the menu is animating out. |
Arrow
Displays an element positioned against the menu anchor.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-open | Present when the menu popup is open. | |
data-closed | Present when the menu popup is closed. | |
data-uncentered | Present when the menu arrow is uncentered. | |
data-anchor-hidden | Present when the anchor is hidden. | |
data-align | Indicates how the popup is aligned relative to specified side. | |
data-side | Indicates which side the popup is positioned relative to the trigger. |
Item
An individual interactive item in the menu.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
label |
|
| |
onClick |
|
| |
closeOnClick |
|
| |
disabled |
|
| |
id |
|
| |
children |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-highlighted | Present when the menu item is highlighted. | |
data-disabled | Present when the menu item is disabled. |
Group
Groups related menu items with the corresponding label.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
children |
|
| |
className |
|
| |
render |
|
|
GroupLabel
An accessible label that is automatically associated with its parent group.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
render |
|
|
RadioGroup
Groups related radio items.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
defaultValue |
|
| |
value |
|
| |
onValueChange |
|
| |
disabled |
|
| |
children |
|
| |
className |
|
| |
render |
|
|
RadioItem
A menu item that works like a radio button in a given group.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
label |
|
| |
value* |
|
| |
onClick |
|
| |
closeOnClick |
|
| |
disabled |
|
| |
id |
|
| |
children |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-checked | Present when the menu radio item is selected. | |
data-unchecked | Present when the menu radio item is not selected. | |
data-highlighted | Present when the menu radio item is highlighted. | |
data-disabled | Present when the menu radio item is disabled. |
RadioItemIndicator
Indicates whether the radio item is selected.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
keepMounted |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-checked | Present when the menu radio item is selected. | |
data-unchecked | Present when the menu radio item is not selected. | |
data-disabled | Present when the menu radio item is disabled. | |
data-starting-style | Present when the radio indicator is animating in. | |
data-ending-style | Present when the radio indicator is animating out. |
CheckboxItem
A menu item that toggles a setting on or off.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
label |
|
| |
defaultChecked |
|
| |
checked |
|
| |
onCheckedChange |
|
| |
onClick |
|
| |
closeOnClick |
|
| |
disabled |
|
| |
id |
|
| |
children |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-checked | Present when the menu checkbox item is checked. | |
data-unchecked | Present when the menu checkbox item is not checked. | |
data-highlighted | Present when the menu checkbox item is highlighted. | |
data-disabled | Present when the menu checkbox item is disabled. |
CheckboxItemIndicator
Indicates whether the checkbox item is ticked.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
className |
|
| |
keepMounted |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-checked | Present when the menu checkbox item is checked. | |
data-unchecked | Present when the menu checkbox item is not checked. | |
data-disabled | Present when the menu checkbox item is disabled. | |
data-starting-style | Present when the indicator is animating in. | |
data-ending-style | Present when the indicator is animating out. |
SubmenuTrigger
A menu item that opens a submenu.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
label |
|
| |
onClick |
|
| |
id |
|
| |
children |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-popup-open | Present when the corresponding submenu is open. |
Separator
A separator element accessible to screen readers.
Renders a <div>
element.
Prop | Type | Default | |
---|---|---|---|
orientation |
|
| |
className |
|
| |
render |
|
|
Examples
Advanced demos are available at the main Menu page, most of which apply to the context menu as well.
Nested menu
To create a submenu, nest another menu inside the parent context menu. Use the <ContextMenu.SubmenuTrigger>
part for the menu item that opens the nested menu.