<!-- index: /slotted/llms.txt -->
# menu

`data-ui="menu"` · List of actions opened from a trigger.

## Import

```tsx
import { Menu, MenuCheckboxItem, MenuContent, MenuEmpty, MenuGroup, MenuHeader, MenuItem, MenuItemDescription, MenuItemLabel, MenuRadioGroup, MenuRadioItem, MenuSeparator, MenuTrigger } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Button } from '@slotted/react/button';
import { Glyph } from '@slotted/react/icon';
import { Menu, MenuContent, MenuItem, MenuSeparator, MenuTrigger } from '@slotted/react/menu';

export default function MenuBasic() {
  return (
    <Menu>
      <MenuTrigger
        render={(props) => (
          <Button {...props} end={<Glyph name="chevron-down" />} variant="outline">
            Order actions
          </Button>
        )}
      />
      <MenuContent>
        <MenuItem>View order</MenuItem>
        <MenuItem shortcut="Mod+E">Edit order</MenuItem>
        <MenuItem shortcut="Mod+D">Duplicate</MenuItem>
        <MenuItem shortcut="Mod+P">Print packing slip</MenuItem>
        <MenuSeparator />
        <MenuItem tone="danger">Cancel order…</MenuItem>
      </MenuContent>
    </Menu>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |
| `backdrop` | div | no |
| `content` | div | yes |
| `grip` | span | no |
| `step` | div | no |
| `back` | button | no |
| `title` | div | no |
| `header` | div | no |
| `group` | div | no |
| `group-label` | div | no |
| `item` | button, or a when the item is a link | yes |
| `indicator` | span | no |
| `icon` | span | no |
| `label` | span | yes |
| `description` | span | no |
| `meta` | span | no |
| `shortcut` | span | no |
| `separator` | hr | no |
| `empty` | div | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean \| undefined` | — | Menu | Whether the menu is open. Passing it makes the menu controlled for its lifetime; update it from onOpenChange. |
| `defaultOpen` | `boolean` | false | Menu | Whether an uncontrolled menu starts open. Read once. |
| `onOpenChange` | `(open, request) => void` | — | Menu | Called before the menu opens or closes, with the reason and the event. The request can be cancelled. |
| `value` | `string` | — | Menu, MenuRadioGroup, MenuRadioItem | On a Menu inside a Menubar, the name the bar knows it by; a menu on its own needs none. On a MenuRadioGroup, the chosen value, which makes the group controlled. On a MenuRadioItem, the value the item stands for. |
| `checked` | `boolean \| undefined` | — | MenuCheckboxItem | Whether the option is on. Passing it makes the item controlled. |
| `defaultChecked` | `boolean` | false | MenuCheckboxItem | Whether an uncontrolled option starts on. |
| `onCheckedChange` | `(checked, request) => void` | — | MenuCheckboxItem | Called before the option changes, with pointer or keyboard as the reason. |
| `defaultValue` | `string` | — | MenuRadioGroup | The value an uncontrolled group starts with. |
| `onValueChange` | `(value, request) => void` | — | MenuRadioGroup | Called before the chosen value changes, with pointer or keyboard as the reason. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | Menu | The width of the surface and the height of its items. |
| `width` | `'content' \| 'anchor'` | content | Menu | content: the surface takes its own measure; anchor: it is exactly as wide as its trigger, capped by the available width. A submenu is always content. |
| `side` | `'top' \| 'bottom' \| 'inline-start' \| 'inline-end' \| 'left' \| 'right'` | bottom | Menu | The edge of the trigger the surface opens from. inline-start and inline-end follow the reading direction. |
| `align` | `'start' \| 'center' \| 'end'` | start | Menu | Where the surface sits along that edge. |
| `anchor` | `PositionerAnchor \| null` | — | Menu | What the surface is positioned against instead of the trigger, such as a table row or a point. Focus still returns to the trigger. |
| `finalFocus` | `Element \| RefObject \| string` | the trigger | Menu | Where focus goes when the menu closes, for a trigger that is gone by then, such as the actions button of a deleted row. |
| `mount` | `'active' \| 'lazy' \| 'eager'` | active | Menu | When the surface is in the document: only while open, from the first open on, or from the start. |
| `strategy` | `'absolute' \| 'fixed'` | absolute | Menu | The CSS position the layer is placed with. |
| `closeOnSelect` | `boolean` | true | Menu, MenuItem | Whether running an item closes the menu. Checkbox and radio items keep it open unless they set it themselves. |
| `loop` | `boolean` | true | Menu | Whether the arrow keys wrap from the last item to the first. |
| `typeahead` | `boolean` | true | Menu | Whether typing moves focus to the item whose label starts with the typed text. |
| `openOnHover` | `boolean` | true | Menu | Whether a submenu opens when the pointer rests on its item. |
| `closeOnEscape` | `boolean` | true | Menu | Whether Escape closes the menu that holds focus. |
| `closeOnOutsidePress` | `boolean` | true | Menu | Whether a press outside the menu closes every open menu. |
| `flip` | `boolean` | true | Menu | Whether the surface moves to the opposite side when it would leave the window. |
| `shift` | `boolean` | true | Menu | Whether the surface slides along its edge to stay in the window. |
| `hideWhenDetached` | `boolean` | true | Menu | Whether the surface hides once its trigger has scrolled out of view. |
| `portal` | `boolean` | true | Menu | Whether the layer is rendered at the end of the document. |
| `presentation` | `'popover' \| 'auto'` | popover | Menu, MenuDefaultsProvider | auto draws the menu as a bottom sheet under (max-width: 480px) and (pointer: coarse): modal, as ActionSheet draws its actions, every item at least --slotted-target-min tall, dismissed by its grip, its scrim, Escape and the platform’s way back, with focus given back to the trigger. A submenu opens as a second step of the same sheet. A menu on a bar stays anchored. |
| `messages` | `Partial<MenuMessages>` | — | Menu | Words laid over the catalog of the language in force: back, the name of the control that returns from a submenu step to the first step of a sheet. |
| `disabled` | `boolean` | false | Menu, MenuItem | A disabled menu never opens, and its trigger keeps aria-expanded="false". A disabled item stays focusable and runs nothing. |
| `loading` | `boolean` | false | MenuContent | Whether the items are on their way. The surface keeps its width, sets aria-busy and shows the MenuEmpty message. |
| `sideOffset` | `number` | 0 | Menu | Extra distance from the trigger, in pixels, beyond the layer's own gap. |
| `alignOffset` | `number` | 0 | Menu | Distance along the alignment edge, in pixels. |
| `collisionPadding` | `number` | 8 | Menu | The distance, in pixels, the surface keeps from the edge of the window. |
| `submenuOpenDelay` | `number` | 150 | Menu | How long, in milliseconds, the pointer rests on an item before its submenu opens. |
| `submenuCloseDelay` | `number` | 300 | Menu | How long, in milliseconds, a submenu stays open while the pointer travels toward it without reaching it. |
| `typeaheadTimeout` | `number` | 500 | Menu | How long, in milliseconds, typed letters keep adding to one search. |
| `icon` | `ReactNode` | — | MenuItem, MenuTrigger | The icon in the first column of the item. |
| `description` | `ReactNode` | — | MenuItem, MenuCheckboxItem, MenuRadioItem | The line under the label, referenced with aria-describedby; the label alone names the item. |
| `meta` | `ReactNode` | — | MenuItem, MenuTrigger | A count, a status or the value a submenu holds, in its own column before the shortcut or the chevron. |
| `shortcut` | `string` | — | MenuItem | A shortcut written as Mod+K, printed for the platform and announced with aria-keyshortcuts. The menu does not bind the key. |
| `textValue` | `string` | — | MenuItem | The text typeahead matches, when the label does not start with the word a reader would type. |
| `tone` | `'accent' \| 'secondary' \| 'success' \| 'warning' \| 'danger'` | — | MenuItem | The theme tone of the label, the icon and the highlight. A destructive command takes danger. |
| `label` | `ReactNode` | — | MenuGroup | The name of the group, rendered as its group-label part. |
| `render` | `(props) => ReactNode` | — | MenuTrigger, MenuContent, MenuItem | Renders your own element with the wiring, the state attributes and the handlers spread on it. On MenuItem, return an a for a link item. |
| `MenuDefaultsProvider` | `component` | — | Application | Sets any axis once for every menu under it, and container sets where the layers are rendered. A value set on one menu still wins. |
| `render` | `prop` | — | MenuTrigger, MenuContent, MenuItem | Replaces the rendered element with your own, keeping the wiring, the state attributes and the handlers. |
| `MenuHeader` | `component` | — | MenuContent | The block above the commands. Its children are the media, a MenuItemLabel and a MenuItemDescription, and the surface is described by it. |
| `MenuItemMeta, MenuItemIcon, MenuItemLabel, MenuItemDescription, MenuItemShortcut` | `component` | — | MenuItem children | The parts of an item, for an item written from its parts instead of from props. |
| `MenuGroupLabel` | `component` | — | MenuGroup | The group-label part, for a label written as a child instead of with label. |
| `MenuEmpty` | `component` | — | MenuContent | The message shown while loading is set or when the menu holds no items. |
| `useMenu` | `hook` | — | Inside a Menu | Reads the menu around a part: whether it is open, its resolved axes and toggle(). |
| `ref` | `prop` | — | Menu | A handle with open(), close() and focus(), for a shortcut your application binds outside the menu. |
| `native props` | `attributes` | — | Every part | Any attribute the underlying element takes reaches it. Your own onClick, onKeyDown or onPointerEnter runs before the menu's handler, and preventDefault() in it skips the menu's. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onOpenChange` | `(open: boolean, request: MenuChangeRequest)` | Before the menu opens or closes. request.reason is pointer, keyboard, escape, outsidePress, select, dismiss or programmatic, request.source is the event, and request.cancel() refuses the change. |
| `onCheckedChange` | `(checked: boolean, request: MenuCheckedChangeRequest)` | Before a checkbox item changes. request.reason is pointer or keyboard. |
| `onValueChange` | `(value: string, request: MenuValueChangeRequest)` | Before a radio group changes its value. request.reason is pointer or keyboard. |
| `onSelect` | `(event: MenuSelectEvent)` | When an item runs. event.reason is pointer or keyboard, and event.preventDefault() keeps the menu open for this press only. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `width` | `content`, `anchor` | `content` |
| `side` | `bottom`, `top`, `inline-start`, `inline-end`, `left`, `right` | `bottom` |
| `align` | `start`, `center`, `end` | `start` |
| `mount` | `active`, `lazy`, `eager` | `active` |
| `strategy` | `absolute`, `fixed` | `absolute` |
| `closeOnSelect` | `true`, `false` | `true` |
| `loop` | `true`, `false` | `true` |
| `typeahead` | `true`, `false` | `true` |
| `openOnHover` | `true`, `false` | `true` |
| `closeOnEscape` | `true`, `false` | `true` |
| `closeOnOutsidePress` | `true`, `false` | `true` |
| `flip` | `true`, `false` | `true` |
| `shift` | `true`, `false` | `true` |
| `hideWhenDetached` | `true`, `false` | `true` |
| `portal` | `true`, `false` | `true` |
| `disabled` | `true`, `false` | `false` |

## Rules

- An item is a native button, so Enter and Space run it and the pointer behaves as the browser's own. role="menuitem" replaces role="button", so a screen reader announces a menu item. A link item is an a that keeps its href and takes the same role.
- Menu supplies the keyboard of the WAI-ARIA menu pattern: the arrow keys move and wrap, Home and End reach the ends, letters move by typeahead, the inline-end arrow opens a submenu and the inline-start arrow closes it, Escape closes the menu that holds focus, and Tab closes every menu and moves focus past the trigger.
- A disabled item stays focusable and carries aria-disabled instead of the native disabled attribute, so a keyboard user can still find a command that is unavailable. A natively disabled button cannot take focus.
- A header is not an item and never takes focus, because role="menu" owns only items, groups and separators. The surface references it with aria-describedby, joined to any description you give, so a screen reader announces who the menu belongs to once, as it opens.
- The surface is named by its trigger, or by its own aria-label. A checkbox or radio item announces its state through aria-checked, a group is named by its label, and a shortcut is announced through aria-keyshortcuts while the printed keys are hidden, so the item's name stays the command.

## Tokens

`--slotted-border-strong`, `--slotted-border-subtle`, `--slotted-border-width-thin`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-lg`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-padding-inline-lg`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-foreground`, `--slotted-drawer-border-color`, `--slotted-drawer-radius`, `--slotted-drawer-resizer-line-color`, `--slotted-drawer-scrim`, `--slotted-drawer-scrim-backdrop-filter`, `--slotted-drawer-slide-duration`, `--slotted-drawer-slide-easing`, `--slotted-drawer-slide-exit-duration`, `--slotted-drawer-slide-exit-easing`, `--slotted-drawer-surface`, `--slotted-duration-long`, `--slotted-duration-medium`, `--slotted-duration-short`, `--slotted-easing-enter`, `--slotted-easing-exit`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-width`, `--slotted-font-family-heading`, `--slotted-font-family-sans`, `--slotted-font-size-xs`, `--slotted-font-weight-semibold`, `--slotted-line-height-md`, `--slotted-line-height-sm`, `--slotted-menu-backdrop-filter`, `--slotted-menu-border-color`, `--slotted-menu-gap`, `--slotted-menu-group-label-height`, `--slotted-menu-highlight`, `--slotted-menu-highlight-active`, `--slotted-menu-item-radius`, `--slotted-menu-measure`, `--slotted-menu-measure-lg`, `--slotted-menu-measure-sm`, `--slotted-menu-min-measure`, `--slotted-menu-padding-block`, `--slotted-menu-padding-inline`, `--slotted-menu-radius`, `--slotted-menu-separator-color`, `--slotted-menu-shadow`, `--slotted-menu-surface`, `--slotted-menu-text`, `--slotted-menu-text-muted`, `--slotted-overlay-max-block-size`, `--slotted-radius-lg`, `--slotted-scrim`, `--slotted-shadow-lg`, `--slotted-shadow-md`, `--slotted-space-1`, `--slotted-space-11`, `--slotted-space-2`, `--slotted-space-5`, `--slotted-space-6`, `--slotted-surface-raised`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-subtle-active`, `--slotted-tone-accent-surface`, `--slotted-tone-accent-text`, `--slotted-tone-danger-subtle-active`, `--slotted-tone-danger-surface`, `--slotted-tone-danger-text`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`, `--slotted-tone-secondary-surface`, `--slotted-tone-secondary-text`, `--slotted-tone-success-subtle-active`, `--slotted-tone-success-surface`, `--slotted-tone-success-text`, `--slotted-tone-warning-subtle-active`, `--slotted-tone-warning-surface`, `--slotted-tone-warning-text`, `--slotted-z-index-popover`

## Examples

See [menu.examples.md](./menu.examples.md).
