Context Menu
A menu that appears at the pointer on right click or long press.
View as MarkdownAnatomy
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.SubmenuRoot>
<ContextMenu.SubmenuTrigger />
</ContextMenu.SubmenuRoot>
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
API reference
Root
A component that creates a context menu activated by right clicking or long pressing. Doesn’t render its own HTML element.
defaultOpen
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the menu is initially open.
To render a controlled menu, use the
open
prop instead.- Type
boolean
- Default
false
open
boolean
boolean
- Name
- Description
Whether the menu is currently open.
- Type
boolean
onOpenChange
function
function
- Name
- Description
Event handler called when the menu is opened or closed.
- Type
((open: boolean, event: Event | undefined, reason: Menu.Root.OpenChangeReason | undefined) => void)
actionsRef
RefObject<Menu.Root.Actions>
RefObject<Menu.Root.Actions>
- Name
- Description
A ref to imperative actions.
unmount
: When specified, the menu will not be unmounted when closed. Instead, theunmount
function must be called to unmount the menu manually. Useful when the menu's animation is controlled by an external library.
- Type
RefObject<Menu.Root.Actions>
closeParentOnEsc
boolean
(default: true
)
boolean
true
- Name
- Description
When in a submenu, determines whether pressing the Escape key closes the entire menu, or only the current child menu.
- Type
boolean
- Default
true
onOpenChangeComplete
function
function
- Description
Event handler called after any animations complete when the menu is closed.
- Type
((open: boolean) => void)
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
loop
boolean
(default: true
)
boolean
true
- Name
- Description
Whether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.
- Type
boolean
- Default
true
orientation
Menu.Root.Orientation
(default: 'vertical'
)
Menu.Root.Orientation
'vertical'
- Name
- Description
The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.
- Type
Menu.Root.Orientation
- Default
'vertical'
children*
ReactNode
ReactNode
- Name
- Type
ReactNode
Trigger
An area that opens the menu on right click or long press.
Renders a <div>
element.
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: ContextMenu.Trigger.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: ContextMenu.Trigger.State) => ReactElement)
Portal
A portal element that moves the popup to a different part of the DOM.
By default, the portal element is appended to <body>
.
container
Union
Union
- Name
- Description
A parent element to render the portal element into.
- Type
| HTMLElement
| ShadowRoot
| RefObject<HTMLElement
| ShadowRoot
| null>
| null
children
ReactNode
ReactNode
- Name
- Type
ReactNode
keepMounted
boolean
(default: false
)
boolean
false
- Name
- Description
Whether to keep the portal mounted in the DOM while the popup is hidden.
- Type
boolean
- Default
false
Positioner
Positions the menu popup against the trigger.
Renders a <div>
element.
collisionAvoidance
CollisionAvoidance
CollisionAvoidance
- Description
Determines how to handle collisions when positioning the popup.
- Type
CollisionAvoidance
align
Union
(default: 'center'
)
Union
'center'
- Name
- Description
How to align the popup relative to the specified side.
- Type
'center' | 'start' | 'end'
- Default
'center'
alignOffset
number | OffsetFunction
(default: 0
)
number | OffsetFunction
0
- Name
- Description
Additional 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.
data.anchor
: the dimensions of the anchor element with propertieswidth
andheight
.data.positioner
: the dimensions of the positioner element with propertieswidth
andheight
.data.side
: which side of the anchor element the positioner is aligned against.data.align
: how the positioner is aligned relative to the specified side.
- Type
number | OffsetFunction
- Default
0
side
Side
(default: 'bottom'
)
Side
'bottom'
- Name
- Description
Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
- Type
Side
- Default
'bottom'
sideOffset
number | OffsetFunction
(default: 0
)
number | OffsetFunction
0
- Name
- Description
Distance 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.
data.anchor
: the dimensions of the anchor element with propertieswidth
andheight
.data.positioner
: the dimensions of the positioner element with propertieswidth
andheight
.data.side
: which side of the anchor element the positioner is aligned against.data.align
: how the positioner is aligned relative to the specified side.
- Type
number | OffsetFunction
- Default
0
arrowPadding
number
(default: 5
)
number
5
- Name
- Description
Minimum distance to maintain between the arrow and the edges of the popup.
Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
- Type
number
- Default
5
anchor
Union
Union
- Name
- Description
An element to position the popup against. By default, the popup will be positioned against the trigger.
- Type
| Element
| RefObject<Element
| null>
| VirtualElement
| (() => Element | VirtualElement | null)
| null
collisionBoundary
Boundary
(default: 'clipping-ancestors'
)
Boundary
'clipping-ancestors'
- Description
An element or a rectangle that delimits the area that the popup is confined to.
- Type
Boundary
- Default
'clipping-ancestors'
collisionPadding
Padding
(default: 5
)
Padding
5
- Name
- Description
Additional space to maintain from the edge of the collision boundary.
- Type
Padding
- Default
5
sticky
boolean
(default: false
)
boolean
false
- Name
- Description
Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.
- Type
boolean
- Default
false
positionMethod
'fixed' | 'absolute'
(default: 'absolute'
)
'fixed' | 'absolute'
'absolute'
- Name
- Description
Determines which CSS
position
property to use.- Type
'fixed' | 'absolute'
- Default
'absolute'
trackAnchor
boolean
(default: true
)
boolean
true
- Name
- Description
Whether the popup tracks any layout shift of its positioning anchor.
- Type
boolean
- Default
true
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.Positioner.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.Positioner.State) => ReactElement)
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.
--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.
finalFocus
RefObject<HTMLElement | null>
RefObject<HTMLElement | null>
- Name
- Description
Determines the element to focus when the menu is closed. By default, focus returns to the trigger.
- Type
RefObject<HTMLElement | null>
id
string
string
- Name
- Type
string
children
ReactNode
ReactNode
- Name
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.Popup.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.Popup.State) => ReactElement)
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.
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.Arrow.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.Arrow.State) => ReactElement)
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.
label
string
string
- Name
- Description
Overrides the text label to use when the item is matched during keyboard text navigation.
- Type
string
onClick
function
function
- Name
- Description
The click handler for the menu item.
- Type
MouseEventHandler<HTMLElement>
closeOnClick
boolean
(default: true
)
boolean
true
- Name
- Description
Whether to close the menu when the item is clicked.
- Type
boolean
- Default
true
nativeButton
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component renders a native
<button>
element when replacing it via therender
prop. Set totrue
if the rendered element is a native button.- Type
boolean
- Default
false
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
id
string
string
- Name
- Type
string
children
ReactNode
ReactNode
- Name
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.Item.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.Item.State) => ReactElement)
data-highlighted
Present when the menu item is highlighted.
data-disabled
Present when the menu item is disabled.
SubmenuRoot
Groups all parts of a submenu. Doesn’t render its own HTML element.
defaultOpen
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the menu is initially open.
To render a controlled menu, use the
open
prop instead.- Type
boolean
- Default
false
open
boolean
boolean
- Name
- Description
Whether the menu is currently open.
- Type
boolean
onOpenChange
function
function
- Name
- Description
Event handler called when the menu is opened or closed.
- Type
((open: boolean, event: Event | undefined, reason: Menu.Root.OpenChangeReason | undefined) => void)
actionsRef
RefObject<Menu.Root.Actions>
RefObject<Menu.Root.Actions>
- Name
- Description
A ref to imperative actions.
unmount
: When specified, the menu will not be unmounted when closed. Instead, theunmount
function must be called to unmount the menu manually. Useful when the menu's animation is controlled by an external library.
- Type
RefObject<Menu.Root.Actions>
closeParentOnEsc
boolean
(default: true
)
boolean
true
- Name
- Description
When in a submenu, determines whether pressing the Escape key closes the entire menu, or only the current child menu.
- Type
boolean
- Default
true
onOpenChangeComplete
function
function
- Description
Event handler called after any animations complete when the menu is closed.
- Type
((open: boolean) => void)
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
openOnHover
boolean
(default: true
)
boolean
true
- Name
- Description
Whether the submenu should open when the trigger is hovered.
- Type
boolean
- Default
true
delay
number
(default: 100
)
number
100
- Name
- Description
How long to wait before the menu may be opened on hover. Specified in milliseconds.
Requires the
openOnHover
prop.- Type
number
- Default
100
closeDelay
number
(default: 0
)
number
0
- Name
- Description
How long to wait before closing the menu that was opened on hover. Specified in milliseconds.
Requires the
openOnHover
prop.- Type
number
- Default
0
loop
boolean
(default: true
)
boolean
true
- Name
- Description
Whether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.
- Type
boolean
- Default
true
orientation
Menu.Root.Orientation
(default: 'vertical'
)
Menu.Root.Orientation
'vertical'
- Name
- Description
The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.
- Type
Menu.Root.Orientation
- Default
'vertical'
children*
ReactNode
ReactNode
- Name
- Type
ReactNode
SubmenuTrigger
A menu item that opens a submenu.
Renders a <div>
element.
label
string
string
- Name
- Description
Overrides the text label to use when the item is matched during keyboard text navigation.
- Type
string
onClick
function
function
- Name
- Type
MouseEventHandler<HTMLElement>
nativeButton
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component renders a native
<button>
element when replacing it via therender
prop. Set totrue
if the rendered element is a native button.- Type
boolean
- Default
false
id
string
string
- Name
- Type
string
children
ReactNode
ReactNode
- Name
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.SubmenuTrigger.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.SubmenuTrigger.State) => ReactElement)
data-popup-open
Present when the corresponding submenu is open.
Group
Groups related menu items with the corresponding label.
Renders a <div>
element.
children
ReactNode
ReactNode
- Name
- Description
The content of the component.
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.Group.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.Group.State) => ReactElement)
GroupLabel
An accessible label that is automatically associated with its parent group.
Renders a <div>
element.
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.GroupLabel.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.GroupLabel.State) => ReactElement)
RadioGroup
Groups related radio items.
Renders a <div>
element.
defaultValue
any
any
- Name
- Description
The uncontrolled value of the radio item that should be initially selected.
To render a controlled radio group, use the
value
prop instead.- Type
any
value
any
any
- Name
- Description
The controlled value of the radio item that should be currently selected.
To render an uncontrolled radio group, use the
defaultValue
prop instead.- Type
any
onValueChange
function
(default: () => {}
)
function
() => {}
- Name
- Description
Function called when the selected value changes.
- Type
((value: any, event: Event) => void)
- Default
() => {}
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
children
ReactNode
ReactNode
- Name
- Description
The content of the component.
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.RadioGroup.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.RadioGroup.State) => ReactElement)
RadioItem
A menu item that works like a radio button in a given group.
Renders a <div>
element.
label
string
string
- Name
- Description
Overrides the text label to use when the item is matched during keyboard text navigation.
- Type
string
value*
any
any
- Name
- Description
Value of the radio item. This is the value that will be set in the MenuRadioGroup when the item is selected.
- Type
any
onClick
function
function
- Name
- Description
The click handler for the menu item.
- Type
MouseEventHandler<HTMLElement>
closeOnClick
boolean
(default: false
)
boolean
false
- Name
- Description
Whether to close the menu when the item is clicked.
- Type
boolean
- Default
false
nativeButton
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component renders a native
<button>
element when replacing it via therender
prop. Set totrue
if the rendered element is a native button.- Type
boolean
- Default
false
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
id
string
string
- Name
- Type
string
children
ReactNode
ReactNode
- Name
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.RadioItem.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.RadioItem.State) => ReactElement)
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.
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.RadioItemIndicator.State) => string)
keepMounted
boolean
(default: false
)
boolean
false
- Name
- Description
Whether to keep the HTML element in the DOM when the radio item is inactive.
- Type
boolean
- Default
false
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.RadioItemIndicator.State) => ReactElement)
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.
label
string
string
- Name
- Description
Overrides the text label to use when the item is matched during keyboard text navigation.
- Type
string
defaultChecked
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the checkbox item is initially ticked.
To render a controlled checkbox item, use the
checked
prop instead.- Type
boolean
- Default
false
checked
boolean
boolean
- Name
- Description
Whether the checkbox item is currently ticked.
To render an uncontrolled checkbox item, use the
defaultChecked
prop instead.- Type
boolean
onCheckedChange
function
function
- Name
- Description
Event handler called when the checkbox item is ticked or unticked.
- Type
((checked: boolean, event: Event) => void)
onClick
function
function
- Name
- Description
The click handler for the menu item.
- Type
MouseEventHandler<HTMLElement>
closeOnClick
boolean
(default: false
)
boolean
false
- Name
- Description
Whether to close the menu when the item is clicked.
- Type
boolean
- Default
false
nativeButton
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component renders a native
<button>
element when replacing it via therender
prop. Set totrue
if the rendered element is a native button.- Type
boolean
- Default
false
disabled
boolean
(default: false
)
boolean
false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean
- Default
false
id
string
string
- Name
- Type
string
children
ReactNode
ReactNode
- Name
- Type
ReactNode
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.CheckboxItem.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.CheckboxItem.State) => ReactElement)
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.
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Menu.CheckboxItemIndicator.State) => string)
keepMounted
boolean
(default: false
)
boolean
false
- Name
- Description
Whether to keep the HTML element in the DOM when the checkbox item is not checked.
- Type
boolean
- Default
false
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Menu.CheckboxItemIndicator.State) => ReactElement)
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.
Separator
A separator element accessible to screen readers.
Renders a <div>
element.
orientation
Orientation
(default: 'horizontal'
)
Orientation
'horizontal'
- Name
- Description
The orientation of the separator.
- Type
Orientation
- Default
'horizontal'
className
string | function
string | function
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string
| ((state: Separator.State) => string)
render
ReactElement | function
ReactElement | function
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElement
or a function that returns the element to render.- Type
| ReactElement
| ((props: HTMLProps, state: Separator.State) => ReactElement)
Examples
Menu displays additional demos, many of which apply to the context menu as well.
Nested menu
To create a submenu, create a <ContextMenu.SubmenuRoot>
inside the parent context menu. Use the <ContextMenu.SubmenuTrigger>
part for the menu item that opens the nested menu.