Skip to the content
human-kit design
33 — Overlays

Menu

A list of actions that opens from a button or a right click.

Installation

pnpm dlx @human-kit/design add menu
npx @human-kit/design add menu
yarn dlx @human-kit/design add menu
bunx @human-kit/design add menu

The 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.

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.

10:30 · Laura Gómez Right click, or press and hold, for the actions.

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 ghost icon trigger at the end of a row, with an aria-label that says whose actions they are: "Appointment actions".
  • Put the destructive row last, after a Menu.Separator, with intent="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 shortcut only when the shortcut really works on the page.

Accessibility

  • The trigger is a <button> with aria-haspopup and aria-expanded. Enter, Space or Down opens the menu on its first row, Up on its last.
  • The panel is role="menu" with role="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 claim aria-haspopup, so it must never be the only way to an action.

API reference

Menu.Root

PropTypeDefault
openboolean

Whether the menu is open. Bindable.

—
defaultOpenboolean

Open at the start, when you give no open.

false
onOpenChange(open: boolean, details: MenuOpenChangeDetails) => void

Called when it opens or closes. details.reason says why, and details.cancel() stops the change.

—
closeOnSelectboolean

Closes the menu when a row is chosen. A row can say otherwise.

true
loopboolean

The arrows go from the last row to the first, and back.

true
typeaheadboolean

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

PropTypeDefault
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 icon sizes make a square for an icon alone.

'default'
elementHTMLButtonElement | null

The rendered element. Bindable.

—

Plus every attribute of a native <button> but type.

Menu.ContextTrigger

PropTypeDefault
disabledboolean

Turns it off: the browser's own menu shows instead.

false
longPressboolean

Opens the menu from a long press with a finger or a pen.

true
longPressDelaynumber

How long the press lasts, in ms.

500
preventTouchCalloutboolean

Stops the iOS text callout and text selection, which the long press needs. Turn it off when the text has to be selectable.

true
tabindexnumber

Give -1 inside a table or a tree that moves the focus itself.

0

Plus every attribute of a <div>. It renders a plain element, not a button, so it can wrap any content.

Menu.Content

PropTypeDefault
placementExtendedPlacement

Where it opens against the trigger: 'bottom-start', 'top-end' and the like. A submenu opens at 'right-start'.

'bottom-start'
offsetnumber

The gap to the trigger, in px. A context menu opens at the pointer, at 0.

4
shouldFlipboolean

Opens on the other side when there is no room.

true
boundaryElementElement | null

The element whose edges it keeps within.

—
shouldCloseOnEscapeboolean

Escape closes it.

true
shouldCloseOnInteractOutsideboolean

A press outside closes it.

true

Plus every attribute of a <div>, but role. It renders in a portal.

Menu.Item

PropTypeDefault
onAction(event?: Event) => void

Called when the row is chosen, by the pointer or the keyboard.

—
iconComponent

The icon before the label.

—
shortcutstring

The keys that do the same, at the far end: "Ctrl D".

—
intent'neutral' | 'danger'

danger for an action that destroys or cannot be undone.

'neutral'
disabledboolean

The row stays in view and cannot be chosen.

false
closeOnSelectboolean

Replaces the menu's closeOnSelect for this row.

—
textValuestring

The text the typeahead matches, when the content is not plain text.

—
valuestring | number

A stable id for the row. One is made up when none is given.

—
elementHTMLElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>, but role.

Menu.Separator

PropTypeDefault
classstring

Classes for the line.

—

A line from edge to edge of the panel. It takes the attributes of a <div>.

Menu.Group

PropTypeDefault
childrenSnippet

The label and the rows.

—

Holds a few rows and the label that names them. It takes the attributes of a <div>.

Menu.GroupLabel

PropTypeDefault
childrenSnippet

The name.

—

The name of the rows under it. It takes the attributes of a <div>.

Menu.SubmenuRoot

PropTypeDefault
openboolean

Whether the submenu is open. Bindable.

—
defaultOpenboolean

Open at the start, when you give no open.

false
onOpenChange(open: boolean, details: MenuOpenChangeDetails) => void

Called when it opens or closes.

—
closeOnSelectboolean

Closes the menus when a row is chosen. Taken from the menu above by default.

—
loopboolean

The arrows wrap around. Taken from the menu above by default.

—
typeaheadboolean

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

PropTypeDefault
iconComponent

The icon before the label.

—
disabledboolean

The row stays in view and does not open.

false
textValuestring

The text the typeahead matches.

—
valuestring | 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.