Overlay Components
[06] Overlay Components
Menu
A dropdown menu triggered by a button with full keyboard navigation. Contains MenuItem, Divider, and MenuLabel subcomponents.
Menu holds actions that apply to one object or view, such as edit, duplicate or share. The dropdown is a Popup with role="menu", each MenuItem is a <button> with role="menuitem" by default, and the trigger gets aria-haspopup="menu" and aria-expanded. To pick a value that a form submits, use Select; for navigation that stays visible in a sidebar or header, use NavLink.
Basic usage
A dropdown menu triggered by a button. Pass the trigger as a React element via the trigger prop; children are the menu contents. Includes full keyboard navigation (Arrow keys, Enter, Escape, Tab) and focus management with a keyboard-visible outline on each item. Focus returns to the trigger when the menu closes. Escape closes only the topmost layer: a submenu before its parent menu, and a menu inside a Modal before the dialog. Moving the pointer to another item closes a submenu that is open.
The popup takes its accessible name from the trigger through aria-labelledby, and a trigger without an id gets a generated one. An aria-label or aria-labelledby you set on the menu is kept. The item focused last is the menu's one tab stop, and hovering an item focuses it. These need @vaneui/ui 1.6.0 or later.
<Menu trigger={<Button>Actions</Button>}> <MenuItem>Edit</MenuItem> <MenuItem>Duplicate</MenuItem> <MenuItem>Share</MenuItem></Menu>Labels and dividers
Use MenuLabel for section headings and Divider for visual separators between groups. Divider automatically picks up the Menu's size via the menu-divider sub-theme.
<Menu trigger={<Button>Account</Button>}> <MenuLabel>Account</MenuLabel> <MenuItem>Profile</MenuItem> <MenuItem>Settings</MenuItem> <Divider /> <MenuLabel>Workspace</MenuLabel> <MenuItem>Members</MenuItem> <MenuItem>Billing</MenuItem> <Divider /> <MenuItem>Sign out</MenuItem></Menu>Items with icons
Place SVG icons directly inside MenuItem. They are automatically flex-aligned and pointer-events-disabled so clicks pass through to the item.
<Menu trigger={<Button>File</Button>}> <MenuItem><Edit size={14} /> Edit</MenuItem> <MenuItem><Copy size={14} /> Duplicate</MenuItem> <Divider /> <MenuItem danger><Trash2 size={14} /> Delete</MenuItem></Menu>Destructive item
Use the danger appearance prop on a MenuItem to flag a destructive action. Combine with a Divider to visually separate it from safe actions.
<Menu trigger={<Button>Manage</Button>}> <MenuItem>Rename</MenuItem> <MenuItem>Archive</MenuItem> <Divider /> <MenuItem danger>Delete permanently</MenuItem></Menu>Item appearances
Any appearance prop works on MenuItem for semantic coloring of individual actions.
<Menu trigger={<Button>Review</Button>}> <MenuItem success>Approve</MenuItem> <MenuItem warning>Request changes</MenuItem> <MenuItem danger>Reject</MenuItem></Menu>Disabled items
Set disabled on a MenuItem to prevent interaction. Disabled items are skipped during keyboard navigation and announced via aria-disabled.
<Menu trigger={<Button>Edit</Button>}> <MenuItem>Undo</MenuItem> <MenuItem disabled>Redo</MenuItem> <Divider /> <MenuItem>Cut</MenuItem> <MenuItem>Copy</MenuItem> <MenuItem disabled>Paste</MenuItem></Menu>Items as links
Pass href to render a MenuItem as an anchor tag. For client-side navigation in a Next.js app, also pass tag={Link}.
<Menu trigger={<Button>Navigate</Button>}> <MenuItem href="/settings">Settings</MenuItem> <MenuItem href="/profile">Profile</MenuItem> <Divider /> <MenuItem href="https://github.com" target="_blank">GitHub</MenuItem></Menu>Selected items
A MenuItem with aria-selected="true" takes the hover fill, the same rule NavLink follows, so an item stays highlighted while the pointer and focus are elsewhere. The rule needs @vaneui/ui 1.4.5 or later.
MenuItem renders role="menuitem" by default, and ARIA defines no selected state for a menu item, so assistive technology may ignore aria-selected there. Treat the fill as a visual highlight only. For a list of options with one selected, such as search results under a search box, use NavLink with role="option" inside a role="listbox" container, where aria-selected is announced.
Checkable items
A MenuItem keeps a role you pass, so role="menuitemcheckbox" or role="menuitemradio" with aria-checked makes a checkable item, and assistive technology announces the checked state. Keep your own state, show the mark yourself, and set closeMenuOnClick={false} to keep the menu open while the user toggles. This needs @vaneui/ui 1.5.0 or later.
const [grid, setGrid] = useState(true);const [density, setDensity] = useState('comfortable');const mark = (on: boolean) => <Check size={14} style={{ visibility: on ? 'visible' : 'hidden' }} />;
return ( <Menu trigger={<Button>View</Button>}> <MenuItem role="menuitemcheckbox" aria-checked={grid} closeMenuOnClick={false} onClick={() => setGrid(!grid)}> {mark(grid)} Show grid </MenuItem> <Divider /> <MenuItem role="menuitemradio" aria-checked={density === 'comfortable'} closeMenuOnClick={false} onClick={() => setDensity('comfortable')}> {mark(density === 'comfortable')} Comfortable </MenuItem> <MenuItem role="menuitemradio" aria-checked={density === 'compact'} closeMenuOnClick={false} onClick={() => setDensity('compact')}> {mark(density === 'compact')} Compact </MenuItem> </Menu>);Shape, variant and appearance
The props you set on Menu reach the dropdown frame, so the popup takes the shape, variant and appearance rather than the trigger.
<Row flexWrap> <Menu trigger={<Button>rounded, the default</Button>}> <MenuItem>Edit</MenuItem> <MenuItem>Duplicate</MenuItem> </Menu> <Menu sharp trigger={<Button>sharp</Button>}> <MenuItem>Edit</MenuItem> <MenuItem>Duplicate</MenuItem> </Menu> <Menu filled secondary trigger={<Button>filled secondary</Button>}> <MenuItem>Edit</MenuItem> <MenuItem>Duplicate</MenuItem> </Menu> <Menu border info trigger={<Button>info border</Button>}> <MenuItem>Edit</MenuItem> <MenuItem>Duplicate</MenuItem> </Menu></Row>Sizes
Set a size on Menu (e.g. <Menu lg>) and the dropdown popup, every MenuItem, MenuLabel, and nested Divider scale together automatically. No need to repeat the size on every child. Items render with larger font-size and padding, the popup frame lifts its inner padding, and dividers match. Individual children can still override with their own size prop. At every size the text of a MenuItem and a MenuLabel is centered by the height of its capital letters, in the <span class="vane-text-run"> that How Button centers its label explains.
<Row flexWrap> <Menu xs trigger={<Button xs>xs</Button>}> <MenuLabel>Actions</MenuLabel> <MenuItem>Edit</MenuItem> <Divider /> <MenuItem danger>Delete</MenuItem> </Menu> <Menu sm trigger={<Button sm>sm</Button>}> <MenuLabel>Actions</MenuLabel> <MenuItem>Edit</MenuItem> <Divider /> <MenuItem danger>Delete</MenuItem> </Menu> <Menu md trigger={<Button md>md</Button>}> <MenuLabel>Actions</MenuLabel> <MenuItem>Edit</MenuItem> <Divider /> <MenuItem danger>Delete</MenuItem> </Menu> <Menu lg trigger={<Button lg>lg</Button>}> <MenuLabel>Actions</MenuLabel> <MenuItem>Edit</MenuItem> <Divider /> <MenuItem danger>Delete</MenuItem> </Menu> <Menu xl trigger={<Button xl>xl</Button>}> <MenuLabel>Actions</MenuLabel> <MenuItem>Edit</MenuItem> <Divider /> <MenuItem danger>Delete</MenuItem> </Menu></Row>Controlled state
Pass open and onOpenChange to drive the menu from outside state. Useful when you need to open the menu programmatically or react to its state.
const [open, setOpen] = useState(false);
return ( <Menu open={open} onOpenChange={setOpen} trigger={<Button>Menu ({open ? "open" : "closed"})</Button>} > <MenuItem onClick={() => console.log("Edit")}>Edit</MenuItem> <MenuItem closeMenuOnClick={false}>Stay open on click</MenuItem> <MenuItem>Close on click (default)</MenuItem> </Menu>);Tooltip on the trigger
To label an icon-only trigger, wrap it in a Tooltip and pass the Tooltip as the trigger. The menu's ref, click handler and aria-haspopup and aria-expanded attributes go to the button inside, so the menu opens on click and anchors to it, while the tooltip shows on hover and focus. This needs @vaneui/ui 1.5.0 or later.
<Menu trigger={ <Tooltip content="Options"> <IconButton aria-label="Options"><Settings /></IconButton> </Tooltip>}> <MenuItem>Edit</MenuItem> <MenuItem>Duplicate</MenuItem> <Divider /> <MenuItem danger>Delete</MenuItem></Menu>Menus with more items than fit on screen
A menu that is taller than the space on screen scrolls inside its popup. Add clampHeight to cap the scrolling area at a maximum height that follows the menu's size. Scrolling needs @vaneui/ui 1.6.0 or later: before that, the last items ran off the screen.
<Menu clampHeight trigger={<Button>City</Button>}> {['Honolulu', 'Anchorage', 'Los Angeles', 'Denver', 'Chicago', 'New York', 'Halifax', 'London', 'Berlin', 'Athens', 'Dubai', 'Karachi', 'Dhaka', 'Bangkok', 'Shanghai', 'Tokyo', 'Sydney', 'Auckland'].map((city) => ( <MenuItem key={city}>{city}</MenuItem> ))}</Menu>Advanced props
Menu supports additional configuration for fine-tuning behavior:
| Prop | Default | Description |
|---|---|---|
open | Controlled open state | |
defaultOpen | false | Initial open state (uncontrolled) |
onOpenChange | Called when open state changes | |
onClose | Called when the menu closes | |
closeOnItemClick | true | Close menu when any item is clicked |
loop | true | Loop keyboard navigation from last to first |
disabled | false | Prevent menu from opening |
transitionDuration | 150 | Animation duration in ms |
MenuItem also accepts:
| Prop | Default | Description |
|---|---|---|
closeMenuOnClick | inherits | Override the menu-level closeOnItemClick for this item |
href | Render as anchor tag | |
disabled | false | Prevent interaction |
focusVisible | true | Render a keyboard focus-visible outline |
noFocusVisible | Disable the keyboard focus-visible outline |
Frequently asked questions
How do I add a submenu?
Nest a Menu inside a Menu and give it a MenuItem as its trigger, as in <Menu trigger={<MenuItem>Share</MenuItem>}>. The submenu opens on hover, on click and with ArrowRight, and ArrowLeft or Escape closes only that level. It opens to the right of its item and flips when there is no room, and picking an item in it closes every level.
Why doesn't my own trigger component open the Menu?
Menu clones the trigger element and passes it a ref, onClick, onKeyDown, aria-haspopup, aria-expanded and aria-controls. A component that does not hand the ref to its DOM element and spread the other props onto it drops them, so the click never reaches the Menu and the popup has nothing to anchor to. Forward the ref and spread the rest of the props onto the button.
Why do my Menu theme defaults have no effect?
Menu has a theme node for each part: menu.popup for the dropdown, menu.item for MenuItem, menu.label for MenuLabel and menu.divider for a Divider inside it. A flat menu: { md: true } matches no node and does nothing, and a development build warns about it. Use the nested form, for example themeDefaults={{ menu: { item: { md: true } } }}.