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.

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

ComponentPropTypeDefaultDescription
Menu.Rootopenboolean—Controlled open state
Menu.RootonOpenChange(open: boolean) => void—Called when the open state changes
Menu.TriggersxStyleXStyles—StyleX style override
Menu.PortalcontainerHTMLElementdocument.bodyPortal target
Menu.Positionerside'top' | 'right' | 'bottom' | 'left''bottom'Preferred side
Menu.Positioneralign'start' | 'center' | 'end''start'Alignment along the side
Menu.PopupsxStyleXStyles—StyleX style override
Menu.ItemdestructivebooleanfalseRenders the item in a danger style
Menu.ItemdisabledbooleanfalseDisables the item
Menu.ItemsxStyleXStyles—StyleX style override
Menu.SeparatorsxStyleXStyles—StyleX style override
Menu.GroupLabelsxStyleXStyles—StyleX style override
Menu.SubmenuTriggersxStyleXStyles—StyleX style override
Menu.CheckboxItemcheckedboolean—Checkbox checked state
Menu.CheckboxItemonCheckedChange(checked: boolean) => void—Called when checked state changes
Menu.CheckboxItemsxStyleXStyles—StyleX style override
Menu.RadioGroupvaluestring—Selected radio value
Menu.RadioGrouponValueChange(value: string) => void—Called when selection changes
Menu.RadioItemvaluestring—The value of this radio item
Menu.RadioItemsxStyleXStyles—StyleX style override
Menu.GroupchildrenReactNode—Group items
Menu.CheckboxItemIndicatorsxStyleXStyles—StyleX style override
Menu.RadioItemIndicatorsxStyleXStyles—StyleX style override

All sub-components forward additional props to their underlying Base UI elements.

Accessibility

  • Built on Base UI Menu -- full WAI-ARIA menu pattern with role="menu" and role="menuitem".
  • Items receive role="menuitemcheckbox" or role="menuitemradio" when applicable.
  • Focus is managed within the menu. The highlighted item follows keyboard navigation.
  • Dismissed on outside click or Escape.

Keyboard

KeyAction
Enter / SpaceOpens the menu (on trigger) or activates the highlighted item
ArrowDownMoves highlight to the next item
ArrowUpMoves highlight to the previous item
ArrowRightOpens a submenu (on submenu trigger)
ArrowLeftCloses a submenu and returns to the parent menu
EscapeCloses the menu
HomeMoves highlight to the first item
EndMoves 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, letting Menu.Popup render every Menu.Item eagerly is negligible overhead and keeps Base UI's built-in keyboard highlight tracking working with no extra wiring.