Menu
A list of actions that opens from a button or a right click.
Installation
pnpm dlx @human-kit/design add menunpx @human-kit/design add menuyarn dlx @human-kit/design add menubunx @human-kit/design add menuThe command copies the files to src/lib/components/ui/menu. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Menu from '$lib/components/ui/menu';
</script>
<Menu.Root>
<Menu.Trigger>Actions</Menu.Trigger>
<Menu.Content>
<Menu.Item onAction={edit}>Edit</Menu.Item>
<Menu.Item onAction={duplicate}>Duplicate</Menu.Item>
</Menu.Content>
</Menu.Root> Examples
Groups
Menu.Group with a Menu.GroupLabel names the rows under it. Split the groups with a Menu.Separator.
Submenu
A Menu.SubmenuRoot holds a Menu.SubmenuTrigger and its own Menu.Content, which opens to the side on hover, on a press, or with the right arrow, and closes with the left one. Keep it to one level: a list inside a list inside a list is lost to the pointer.
Context menu
Menu.ContextTrigger takes the place of the trigger to open the menu from a surface: a right click, a long press on touch, or Shift F10 from the keyboard. Offer the same actions somewhere visible too.
Guidelines
- Use a menu for actions on one thing: edit, duplicate, delete. To choose a value that stays shown, use a
Select.Root. - Keep the action that is used most out of the menu, as its own button beside it. A menu hides what is in it.
- Use a
ghosticon trigger at the end of a row, with anaria-labelthat says whose actions they are: "Appointment actions". - Put the destructive row last, after a
Menu.Separator, withintent="danger". Leaving, such as signing out, is not danger. - Group rows by what they do, and name a group only when the rows alone do not say it. Keep submenus to one level.
- Show a
shortcutonly when the shortcut really works on the page.
Accessibility
- The trigger is a
<button>witharia-haspopupandaria-expanded. Enter, Space or Down opens the menu on its first row, Up on its last. - The panel is
role="menu"withrole="menuitem"rows. The arrows move between rows and wrap around, and a typed letter jumps to a row. - Escape closes the menu and puts the focus back on the trigger. Tab or a click outside closes the whole chain. In a submenu, Left closes that level only.
- A disabled row stays in view and is skipped. Prefer it to hiding the row, so the menu does not change shape.
- A context trigger is a tab stop with
aria-keyshortcuts="Shift+F10". It cannot claimaria-haspopup, so it must never be the only way to an action.
API reference
Menu.Root
| Prop | Type | Default |
|---|---|---|
open | boolean Whether the menu is open. Bindable. | — |
defaultOpen | boolean Open at the start, when you give no | false |
onOpenChange | (open: boolean, details: MenuOpenChangeDetails) => void Called when it opens or closes. | — |
closeOnSelect | boolean Closes the menu when a row is chosen. A row can say otherwise. | true |
loop | boolean The arrows go from the last row to the first, and back. | true |
typeahead | boolean A typed letter moves to the row whose text starts with it. | true |
It renders nothing of its own: it holds the open state for the trigger and the panel.
Menu.Trigger
| Prop | Type | Default |
|---|---|---|
variant | 'default' | 'outline' | 'filled' | 'ghost' | 'link' The look of a button. It stays pressed while its menu is open. | 'outline' |
size | 'sm' | 'default' | 'lg' | 'icon' | 'icon-lg' | 'icon-sm' | 'icon-xs' | 'icon-2xs' The size of a button. The | 'default' |
element | HTMLButtonElement | null The rendered element. Bindable. | — |
Plus every attribute of a native <button> but type.
Menu.ContextTrigger
| Prop | Type | Default |
|---|---|---|
disabled | boolean Turns it off: the browser's own menu shows instead. | false |
longPress | boolean Opens the menu from a long press with a finger or a pen. | true |
longPressDelay | number How long the press lasts, in ms. | 500 |
preventTouchCallout | boolean Stops the iOS text callout and text selection, which the long press needs. Turn it off when the text has to be selectable. | true |
tabindex | number Give | 0 |
Plus every attribute of a <div>. It renders a plain element, not a button, so it can wrap any content.
Menu.Content
| Prop | Type | Default |
|---|---|---|
placement | ExtendedPlacement Where it opens against the trigger: | 'bottom-start' |
offset | number The gap to the trigger, in px. A context menu opens at the pointer, at | 4 |
shouldFlip | boolean Opens on the other side when there is no room. | true |
boundaryElement | Element | null The element whose edges it keeps within. | — |
shouldCloseOnEscape | boolean Escape closes it. | true |
shouldCloseOnInteractOutside | boolean A press outside closes it. | true |
Plus every attribute of a <div>, but role. It renders in a portal.
Menu.Item
| Prop | Type | Default |
|---|---|---|
onAction | (event?: Event) => void Called when the row is chosen, by the pointer or the keyboard. | — |
icon | Component The icon before the label. | — |
shortcut | string The keys that do the same, at the far end: "Ctrl D". | — |
intent | 'neutral' | 'danger'
| 'neutral' |
disabled | boolean The row stays in view and cannot be chosen. | false |
closeOnSelect | boolean Replaces the menu's | — |
textValue | string The text the typeahead matches, when the content is not plain text. | — |
value | string | number A stable id for the row. One is made up when none is given. | — |
element | HTMLElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>, but role.
Menu.Separator
| Prop | Type | Default |
|---|---|---|
class | string Classes for the line. | — |
A line from edge to edge of the panel. It takes the attributes of a <div>.
Menu.Group
| Prop | Type | Default |
|---|---|---|
children | Snippet The label and the rows. | — |
Holds a few rows and the label that names them. It takes the attributes of a <div>.
Menu.GroupLabel
| Prop | Type | Default |
|---|---|---|
children | Snippet The name. | — |
The name of the rows under it. It takes the attributes of a <div>.
Menu.SubmenuRoot
| Prop | Type | Default |
|---|---|---|
open | boolean Whether the submenu is open. Bindable. | — |
defaultOpen | boolean Open at the start, when you give no | false |
onOpenChange | (open: boolean, details: MenuOpenChangeDetails) => void Called when it opens or closes. | — |
closeOnSelect | boolean Closes the menus when a row is chosen. Taken from the menu above by default. | — |
loop | boolean The arrows wrap around. Taken from the menu above by default. | — |
typeahead | boolean A typed letter jumps to a row. Taken from the menu above by default. | — |
Holds a Menu.SubmenuTrigger and its own Menu.Content, inside a parent Menu.Content.
Menu.SubmenuTrigger
| Prop | Type | Default |
|---|---|---|
icon | Component The icon before the label. | — |
disabled | boolean The row stays in view and does not open. | false |
textValue | string The text the typeahead matches. | — |
value | string | number A stable id for the row. One is made up when none is given. | — |
A row with a chevron. Plus every attribute of a <div>, but role.