Menu
A dropdown menu with support for items, groups, checkboxes, radios, and submenus.
Import
import { Menu } from '@matix/components/menu';Usage
With Groups
Use Menu.Group and Menu.GroupLabel to organize related items.
Destructive Items
Set destructive on Menu.Item to render it in a danger style.
<Menu.Root>
<Menu.Trigger>More</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup>
<Menu.Item>Edit</Menu.Item>
<Menu.Item>Duplicate</Menu.Item>
<Menu.Separator />
<Menu.Item destructive>Delete</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>Checkbox Items
Use Menu.CheckboxItem for togglable options. Put a Menu.CheckboxItemIndicator in each item to show a check when the item is on. To replace the check, pass your own icon as children.
Radio Items
Use Menu.RadioGroup with Menu.RadioItem for mutually exclusive options. Menu.RadioItemIndicator shows a dot on the selected item.
Submenu
Nest a Menu.Root inside a parent menu and use Menu.SubmenuTrigger to open it.
<Menu.Root>
<Menu.Trigger>Options</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup>
<Menu.Item>New File</Menu.Item>
<Menu.Root>
<Menu.SubmenuTrigger>Share</Menu.SubmenuTrigger>
<Menu.Portal>
<Menu.Positioner side="right">
<Menu.Popup>
<Menu.Item>Email</Menu.Item>
<Menu.Item>Link</Menu.Item>
<Menu.Item>Slack</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
<Menu.Separator />
<Menu.Item destructive>Delete</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>Custom Styles
Use the sx prop on any sub-component to override styles.
import * as stylex from '@stylexjs/stylex';
const custom = stylex.create({
popup: { minWidth: '200px', borderRadius: '12px' },
item: { borderRadius: '6px' },
});
<Menu.Root>
<Menu.Trigger>Styled Menu</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup sx={custom.popup}>
<Menu.Item sx={custom.item}>Option A</Menu.Item>
<Menu.Item sx={custom.item}>Option B</Menu.Item>
<Menu.Item sx={custom.item}>Option C</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>;Server Components
Menu is a client component. A Server Component cannot read a property of a client component, so Menu.Root is undefined there. In a Server Component, import the named parts. Each Menu.<Part> is also exported as Menu<Part>.
import {
MenuRoot,
MenuTrigger,
MenuPortal,
MenuPositioner,
MenuPopup,
MenuItem,
MenuSeparator,
MenuGroup,
MenuGroupLabel,
MenuSubmenuTrigger,
MenuCheckboxItem,
MenuCheckboxItemIndicator,
MenuRadioGroup,
MenuRadioItem,
MenuRadioItemIndicator,
} from '@matix/components/menu';In a client component, both forms work.
API Reference
| Component | Prop | Type | Default | Description |
|---|---|---|---|---|
Menu.Root | open | boolean | — | Controlled open state |
Menu.Root | onOpenChange | (open: boolean) => void | — | Called when the open state changes |
Menu.Trigger | sx | StyleXStyles | — | StyleX style override |
Menu.Portal | container | HTMLElement | document.body | Portal target |
Menu.Positioner | side | 'top' | 'right' | 'bottom' | 'left' | 'bottom' | Preferred side |
Menu.Positioner | align | 'start' | 'center' | 'end' | 'start' | Alignment along the side |
Menu.Popup | sx | StyleXStyles | — | StyleX style override |
Menu.Item | destructive | boolean | false | Renders the item in a danger style |
Menu.Item | disabled | boolean | false | Disables the item |
Menu.Item | sx | StyleXStyles | — | StyleX style override |
Menu.Separator | sx | StyleXStyles | — | StyleX style override |
Menu.GroupLabel | sx | StyleXStyles | — | StyleX style override |
Menu.SubmenuTrigger | sx | StyleXStyles | — | StyleX style override |
Menu.CheckboxItem | checked | boolean | — | Checkbox checked state |
Menu.CheckboxItem | onCheckedChange | (checked: boolean) => void | — | Called when checked state changes |
Menu.CheckboxItem | sx | StyleXStyles | — | StyleX style override |
Menu.RadioGroup | value | string | — | Selected radio value |
Menu.RadioGroup | onValueChange | (value: string) => void | — | Called when selection changes |
Menu.RadioItem | value | string | — | The value of this radio item |
Menu.RadioItem | sx | StyleXStyles | — | StyleX style override |
Menu.Group | children | ReactNode | — | Group items |
Menu.CheckboxItemIndicator | sx | StyleXStyles | — | StyleX style override |
Menu.RadioItemIndicator | sx | StyleXStyles | — | StyleX style override |
All sub-components forward additional props to their underlying Base UI elements.
Accessibility
- Built on Base UI
Menu-- full WAI-ARIAmenupattern withrole="menu"androle="menuitem". - Items receive
role="menuitemcheckbox"orrole="menuitemradio"when applicable. - Focus is managed within the menu. The highlighted item follows keyboard navigation.
- Dismissed on outside click or
Escape.
Keyboard
| Key | Action |
|---|---|
Enter / Space | Opens the menu (on trigger) or activates the highlighted item |
ArrowDown | Moves highlight to the next item |
ArrowUp | Moves highlight to the previous item |
ArrowRight | Opens a submenu (on submenu trigger) |
ArrowLeft | Closes a submenu and returns to the parent menu |
Escape | Closes the menu |
Home | Moves highlight to the first item |
End | Moves highlight to the last item |
Performance
For lists exceeding 100 items, render items virtualized rather than as static DOM nodes. Mounting hundreds of Menu.Item elements at once costs initial paint time and memory.
Virtualization with @tanstack/react-virtual
Menu.Popup renders whatever you pass as children, so a virtualized scroll container drops in directly in place of a flat list of Menu.Items.
import { useRef } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
import * as stylex from '@stylexjs/stylex';
import { Menu } from '@matix/components/menu';
const ITEM_HEIGHT = 32; // px — must match the CSS height of Menu.Item
const virtual = stylex.create({
scroller: { height: '320px', overflowY: 'auto' },
spacer: { position: 'relative' },
item: { position: 'absolute', left: 0, right: 0, height: ITEM_HEIGHT },
});
interface MenuOption {
value: string;
label: string;
}
function VirtualizedMenu({
options,
onSelect,
}: {
options: MenuOption[];
onSelect: (value: string) => void;
}) {
const parentRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer({
count: options.length,
getScrollElement: () => parentRef.current,
estimateSize: () => ITEM_HEIGHT,
overscan: 5,
});
return (
<Menu.Root>
<Menu.Trigger>Open Menu</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup>
{/* Fixed-height scroll container */}
<div ref={parentRef} {...stylex.props(virtual.scroller)}>
{/* Spacer div that represents total list height */}
<div {...stylex.props(virtual.spacer)} style={{ height: virtualizer.getTotalSize() }}>
{virtualizer.getVirtualItems().map((virtualRow) => {
const option = options[virtualRow.index];
return (
<Menu.Item
key={option.value}
onClick={() => onSelect(option.value)}
sx={virtual.item}
style={{ top: virtualRow.start }}
>
{option.label}
</Menu.Item>
);
})}
</div>
</div>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
);
}Rule: Use virtualization for any menu with more than 100 items. Below that threshold, lettingMenu.Popuprender everyMenu.Itemeagerly is negligible overhead and keeps Base UI's built-in keyboard highlight tracking working with no extra wiring.