Menu
A list of actions in a dropdown, enhanced with keyboard navigation.
API reference
Import the component and assemble its parts:
import { Menu } from '@base-ui-components/react/menu';
<Menu.Root> <Menu.Trigger /> <Menu.Portal> <Menu.Backdrop /> <Menu.Positioner> <Menu.Popup> <Menu.Arrow /> <Menu.Item /> <Menu.Separator /> <Menu.Group> <Menu.GroupLabel /> </Menu.Group> <Menu.RadioGroup> <Menu.RadioItem /> </Menu.RadioGroup> <Menu.CheckboxItem /> </Menu.Popup> </Menu.Positioner> </Menu.Portal></Menu.Root>
Root
Groups all parts of the menu. Doesn’t render its own HTML element.
Prop | Type | Default | |
---|---|---|---|
defaultOpen |
|
| |
open |
|
| |
onOpenChange |
|
| |
closeParentOnEsc |
|
| |
modal |
|
| |
disabled |
|
| |
openOnHover |
|
| |
delay |
|
| |
loop |
|
| |
orientation |
|
|
Trigger
A button that opens the menu.
Renders a <button>
element.
Prop | Type | Default | |
---|---|---|---|
disabled |
|
| |
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-popup-open | Present when the corresponding menu is open. | |
data-pressed | Present when the trigger is pressed. |
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 |
|
| |
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 |
|
| |
className |
|
| |
keepMounted |
|
| |
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-side | Indicates which side the menu 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 | |
---|---|---|---|
className |
|
| |
render |
|
|
Attribute | Description | |
---|---|---|
data-open | Present when the menu is open. | |
data-closed | Present when the menu is closed. | |
data-side | Indicates which side the menu 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-side | Indicates which side the menu 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 |
|
|
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 |
|
| |
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 |
|
|
Attribute | Description | |
---|---|---|
data-checked | Present when the menu radio item is selected. | |
data-unchecked | Present when the menu radio item is not selected. |
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 |
|
|
Attribute | Description | |
---|---|---|
data-checked | Present when the menu checkbox item is checked. | |
data-unchecked | Present when the menu checkbox item is not checked. |
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 |
|
| |
disabled |
|
| |
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 | |
---|---|---|---|
className |
|
| |
render |
|
|
Examples
Open on hover
To create a menu that opens on hover, add the openOnHover
prop to the Root part. You can additionally configure how quickly the menu opens on hover using the delay
prop.
Checkbox items
Use the <Menu.CheckboxItem>
part to create a menu item that can toggle a setting on or off.
Radio items
Use the <Menu.RadioGroup>
and <Menu.RadioItem>
parts to create menu items that work like radio buttons.
Close on click
Use the closeOnClick
prop to change whether the menu closes when an item is clicked.
// Close the menu when a checkbox item is clicked<Menu.CheckboxItem closeOnClick />
// Keep the menu open when an item is clicked<Menu.Item closeOnClick={false} />
Group labels
Use the <Menu.GroupLabel>
part to add a label to a <Menu.Group>
Nested menu
To create a submenu, nest another menu inside the parent menu. Use the <Menu.SubmenuTrigger>
part for the menu item that opens the nested menu.
<Menu.Root> <Menu.Trigger /> <Menu.Portal> <Menu.Backdrop /> <Menu.Positioner> <Menu.Popup> <Menu.Arrow /> <Menu.Item />
{/* Submenu */} <Menu.Root> <Menu.SubmenuTrigger /> <Menu.Portal> <Menu.Positioner> <Menu.Popup> </Menu.Popup> </Menu.Positioner> </Menu.Portal> </Menu.Root> </Menu.Popup> </Menu.Positioner> </Menu.Portal></Menu.Root>
Navigate to another page
Use the render
prop to compose a menu item with an anchor element.
<Menu.Item render={<a href="/projects">Go to Projects</a>} />
Open a dialog
In order to open a dialog using a menu, control the dialog state and open it imperatively using the onClick
handler on the menu item.
Make sure to also use the dialog’s finalFocus
prop to return focus back to the menu trigger.
import * as React from 'react';import { Dialog } from '@base-ui/components/dialog';import { Menu } from '@base-ui/components/menu';
function ExampleMenu() { const menuTriggerRef = React.useRef<HTMLButtonElement>(null); const [dialogOpen, setDialogOpen] = React.useState(false);
return ( <React.Fragment> <Menu.Root> {/* Set the trigger ref */} <Menu.Trigger ref={menuTriggerRef}>Open menu</Menu.Trigger> <Menu.Portal> <Menu.Positioner> <Menu.Popup> {/* Open the dialog when the menu item is clicked */} <Menu.Item onClick={() => setDialogOpen(true)}>Open dialog</Menu.Item> </Menu.Popup> </Menu.Positioner> </Menu.Portal> </Menu.Root>
{/* Control the dialog state */} <Dialog.Root open={dialogOpen} onOpenChange={setDialogOpen}> <Dialog.Portal> <Dialog.Backdrop /> {/* Return focus to the menu trigger when the dialog is closed */} <Dialog.Popup finalFocus={menuTriggerRef}> </Dialog.Popup> </Dialog.Portal> </Dialog.Root> </React.Fragment> );}