Menu
A list of actions that opens from a button, with keyboard navigation and nested submenus.
// Radix — @radix-ui/react-dropdown-menu
import { Menu, MenuItem, MenuSub, MenuSubContent, MenuSubTrigger, MenuTrigger } from "@shavin/ui";Overflow menu
MenuTrigger with no children renders a three-dot button named "More options". Pass aria-label to say what it acts on. variant="danger" colours a destructive item and adds a rule above it, so put it last.
Nothing chosen yet
import { Menu, MenuTrigger, MenuItem } from "@shavin/ui";
<Menu align="end" trigger={<MenuTrigger aria-label="Actions for Corner Market, $64.18" />}>
<MenuItem onSelect={() => edit()}>Edit</MenuItem>
<MenuItem onSelect={() => split()}>Split</MenuItem>
<MenuItem disabled>Mark as reviewed</MenuItem>
<MenuItem variant="danger" onSelect={() => remove()}>Delete</MenuItem>
</Menu>Your own trigger
trigger can be any element that forwards its ref, such as Button. Menu opens on click, Enter, Space or Arrow Down.
Nothing chosen yet
<Menu
trigger={
<Button variant="outline">
Export
<Icon name="ChevronDown" size="sm" color="inherit" />
</Button>
}
>
<MenuItem onSelect={() => exportAs("csv")}>CSV</MenuItem>
<MenuItem onSelect={() => exportAs("pdf")}>PDF statement</MenuItem>
</Menu>Submenus
Wrap a MenuSubTrigger and a MenuSubContent in MenuSub. Arrow Right opens the submenu, Arrow Left closes it. Keep it to one level where you can.
Nothing chosen yet
<Menu trigger={<Button variant="outline">Move to</Button>}>
<MenuSub>
<MenuSubTrigger>Bank accounts</MenuSubTrigger>
<MenuSubContent>
<MenuItem onSelect={() => move("joint-checking")}>Joint checking</MenuItem>
<MenuItem onSelect={() => move("emergency-savings")}>Emergency savings</MenuItem>
</MenuSubContent>
</MenuSub>
<MenuSub>
<MenuSubTrigger>Cards</MenuSubTrigger>
<MenuSubContent>
<MenuItem onSelect={() => move("everyday-card")}>Everyday card</MenuItem>
<MenuItem onSelect={() => move("travel-card")}>Travel card</MenuItem>
</MenuSubContent>
</MenuSub>
</Menu>Menu, Dropdown or Context menu
Menu runs commands: each item does something when chosen and nothing stays selected. To pick a value that the control then shows, use Select or Dropdown. For the same kind of list opened by right-click or long-press, use ContextMenu. The list is 208px wide and scrolls after about six items.
Keyboard
| Key | Action |
|---|---|
| Enter / Space / Arrow Down | On the trigger: opens the menu and focuses the first item. |
| Arrow Up / Down | Moves between items, skipping disabled ones. |
| Letters | Jumps to the next item that starts with them. |
| Arrow Right / Left | Opens or closes a submenu. |
| Enter / Space | Chooses the focused item and closes the menu. |
| Escape | Closes the menu and returns focus to the trigger. |
Data attributes
| Attribute | Values |
|---|---|
| [data-highlighted] | On the item that has keyboard focus or the pointer. |
| [data-disabled] | On a disabled item. |
| [data-state] | "open" | "closed" on the trigger and on a MenuSubTrigger. |
Props
Menu
| Prop | Type | Default | Description |
|---|---|---|---|
| trigger* | ReactNode | — | Trigger element slot. |
| children* | ReactNode | — | Menu content items. |
| align | "start" | "end" | start | Content alignment (start or end). Default start. |
| onOpenChange | (open: boolean) => void | — | Called when the menu opens or closes (e.g. keep a hover-only trigger visible while open). |
| variant | "default" | "danger" | "default" | — |
* required
MenuItem
| Prop | Type | Default |
|---|---|---|
| asChild | boolean | — |
| disabled | boolean | — |
| onSelect | ((event: Event) => void) | — |
| textValue | string | — |
| variant | "default" | "danger" | null | "default" |
MenuSub
| Prop | Type | Default |
|---|---|---|
| children | React.ReactNode | — |
| defaultOpen | boolean | — |
| onOpenChange | ((open: boolean) => void) | — |
| open | boolean | — |
MenuItem, MenuSub, MenuSubTrigger and MenuSubContent pass their other props to the matching Radix DropdownMenu part, so onSelect, disabled and textValue work as in Radix.