Menu
A dropdown menu with items, radio groups, checkboxes and separators.
Demo
Usage
import { Menu, type MenuItem } from '@tuttn/slant'
// Parts, for rich content.
<Menu label="Sort and filter" trigger="Sort" onSelect={id => apply(id)}>
<Menu.Group label="Sort by">
<Menu.RadioItem id="recent" checked={sort === 'recent'}>Recent</Menu.RadioItem>
<Menu.RadioItem id="priority" checked={sort === 'priority'}>Priority</Menu.RadioItem>
</Menu.Group>
<Menu.Separator />
<Menu.CheckboxItem id="archived" checked={archived}>Show archived</Menu.CheckboxItem>
<Menu.Sub label="Export as">
<Menu.Item id="csv">CSV</Menu.Item>
<Menu.Item id="pdf">PDF</Menu.Item>
</Menu.Sub>
<Menu.Item id="duplicate" shortcut="⌘D" keyshortcuts="Meta+D">Duplicate list</Menu.Item>
<Menu.Item id="delete" disabled>Delete list</Menu.Item>
</Menu>
// The same as data: a run of entries sharing a group is one labelled group,
// and an entry with items is a submenu.
const items: MenuItem[] = [
{ id: 'recent', label: 'Recent', kind: 'radio', checked: sort === 'recent', group: 'Sort by' },
{ separator: true },
{ id: 'export', label: 'Export as', items: [{ id: 'csv', label: 'CSV' }, { id: 'pdf', label: 'PDF' }] },
{ id: 'delete', label: 'Delete list', disabled: true },
]
<Menu label="Sort and filter" trigger="Sort" items={items} onSelect={id => apply(id)} />
// It keeps its own open state; pass open and onOpenChange to control it.
<Menu label="More" trigger="More" placement="bottom-end" open={open} onOpenChange={setOpen} items={more} onSelect={run} />
// A long menu: maxHeight caps the list, which then scrolls, and a sticky
// group's label stays at its top while the group's items pass under it.
<Menu label="Open file" trigger="Open" maxHeight={320} onSelect={open}>
<Menu.Group label="Recent" sticky>{recent.map(file => <Menu.Item key={file} id={file}>{file}</Menu.Item>)}</Menu.Group>
<Menu.Group label="All files" sticky>{files.map(file => <Menu.Item key={file} id={file}>{file}</Menu.Item>)}</Menu.Group>
</Menu>
// As data, stickyGroup: true on any entry of a run makes its group sticky.Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | The menu's accessible name. |
trigger | ReactNode | — | Rendered inside the trigger button, before the chevron. |
items | MenuItem[] | — | The entries as data, shorthand for the parts: `{ id, label, kind?, checked?, disabled?, group?, stickyGroup?, textValue?, shortcut?, keyshortcuts?, items? }` or `{ separator: true }`. A run of entries sharing a `group` is wrapped in a group with that name, sticky when one of them sets `stickyGroup`; an entry with `items` is a submenu. |
children | ReactNode | — | The compound form, after any `items`: Menu.Item, Menu.CheckboxItem, Menu.RadioItem, Menu.Group, Menu.Sub and Menu.Separator. |
onSelect | (id: string) => void | — | Called with the chosen item's id. Checkbox items keep the menu open; the rest close it. |
open | boolean | — | Whether the list is open. Pass it with onOpenChange to control the menu; leave it out and the menu keeps its own state. |
defaultOpen | boolean | false | Whether the list starts open, when uncontrolled. |
onOpenChange | (open: boolean) => void | — | Called with the new state whenever the menu opens or closes. |
placement | MenuPlacement | 'bottom-start' | The side of the trigger the list hangs from, and the edge it lines up with. Start and end follow the text direction, and the list flips to the other side when this one lacks room. |
triggerProps | Pick<ButtonProps, 'variant' | 'size'> | secondary, md | The trigger button's look, as Button takes it: a quieter `variant="ghost"`, a smaller `size="sm"`. |
maxHeight | number | no cap, except that a list with a sticky group stops at the viewport's height | The list's greatest height in pixels; past it, the list scrolls, and sticky group headings stay at its top. |
Menu.Item | { id: string; disabled?; textValue?; shortcut?; keyshortcuts? } | — | A plain item (menuitem); its children are the content. Choosing it calls onSelect(id) and closes the menu. |
Menu.CheckboxItem | { id: string; checked?; disabled?; textValue?; shortcut?; keyshortcuts? } | — | An on/off item (menuitemcheckbox). Choosing it calls onSelect(id) and leaves the menu open. |
Menu.RadioItem | { id: string; checked?; disabled?; textValue?; shortcut?; keyshortcuts? } | — | One choice of several (menuitemradio), usually inside a Menu.Group. Opening the menu from the button lands on the checked one. |
shortcut | string | — | On any item: a hint shown at its inline end (⌘D). Hidden from assistive technology, and it binds nothing: handle the key yourself. |
keyshortcuts | string | — | On any item: the key you bind, as aria-keyshortcuts spells it (Meta+D), so screen readers announce it. |
Menu.Group | { label: string; sticky? } | — | Wraps items in a group whose accessible name is label. sticky also shows label as a heading that stays at the top of a scrolling menu while the group’s items pass under it. |
Menu.Sub | { label: ReactNode; disabled?; textValue? } | — | An item (menuitem with aria-haspopup) that opens a submenu of its children beside it. label is the item’s content and names the submenu. Subs nest. The ref and attributes go on the item. |
Menu.Separator | {} | — | A rule between groups (separator). |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the wrapper around the button. Each part forwards its ref and attributes to its own element. |
Keyboard
| ↓ / Enter / Space | on the button, open the menu on the checked radio item, or the first |
| ↑ | on the button, opens the menu on the last item |
| ↓ / ↑ | move to the next or previous item, wrapping and skipping disabled items and group headings |
| Home / End | jump to the first or last item |
| Letters | jump to the next item starting with the letters typed within half a second |
| Enter / Space | choose the item: onSelect gets its id, and the menu closes with focus back on the button (a checkbox item leaves it open) |
| → (← in right-to-left) | on a Menu.Sub item, opens its submenu on the first item |
| ← (→ in right-to-left) | in a submenu, closes it and returns to its item |
| Esc | closes the innermost list, returning focus to the button or the submenu’s item |
| Tab / Shift+Tab | close the whole menu and move on from the button, as if the list were not there |
Accessibility
- The button has
aria-haspopup="menu",aria-expandedand, while open,aria-controls. The list is arole="menu"named bylabel, with one roving tab stop. - Items are
menuitem,menuitemcheckboxormenuitemradiowitharia-checked; disabled ones keep their place witharia-disabledand the arrows pass over them. - A Menu.Group is a
role="group"named by itslabel. A sticky group’s visible heading isaria-hidden, because the group already carries the same name: a screen reader hears it once, on entering the group. - Shortcut hints are hidden from assistive technology;
keyshortcutssetsaria-keyshortcuts, which is announced.
Keys and focus
ArrowDown, Enter or Space on the button opens the menu on the checked radio item, or the first; ArrowUp opens it on the last. Inside, the arrows move and wrap past disabled items, Home and End jump, typing jumps to the next item starting with those letters, and Enter or Space chooses. Escape closes it and returns focus to the button; Tab closes it and moves on to the next control as if the list were not there. A press outside, or focus moving elsewhere, closes it too.
Submenus
A Menu.Sub item shows a chevron and opens its submenu beside it, at the inline end, on ArrowRight (ArrowLeft in right-to-left), Enter, Space or a click, landing on its first item; resting the pointer on it opens the submenu after a moment and leaves focus on the item. ArrowLeft (ArrowRight in right-to-left) or Escape closes only that submenu and returns to its item, and moving to another item of the parent list closes it too. Choosing anywhere in the tree closes all of it and returns focus to the button; Tab closes all of it and moves on; a press outside every open list closes all of it. A submenu with no room at the inline end opens on the other side. Shortcut hints are only hints: bind the keys yourself and name them with keyshortcuts.
Long menus and sticky groups
maxHeight caps the list in pixels; past it, the list scrolls inside itself. A Menu.Group with sticky (or a run of entries where one sets stickyGroup) shows its label as a small heading above its items, on the menu’s own surface with a hairline under it. While the list scrolls, the heading stays at its top and the group’s items pass under it, until the group ends and the next group’s heading takes its place. An item the arrows reach scrolls clear of the heading. A list holding a sticky group scrolls even without maxHeight, stopping at the viewport’s height. Groups without sticky show no heading, as before: their label only names them.
Placement
The list renders in a portal at the end of the body, carrying its root’s theme, direction and density, so an overflow: hidden box or a stacking context around the menu cannot clip it. It hangs from the side placement names and flips to the other side when that one lacks room. Without open the menu keeps its own state, starting from defaultOpen.