# Menu

A list of actions under a button, with sections, icons, shortcuts and disabled items that say why.

- Family: overlay
- Import: `import { Menu, MenuItem, MenuSection, MenuSubmenu, MenuSeparator } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/menu
- JSON: https://design.senvenli.com/docs/components/menu.json

## Example: Actions for a row

An icon-only trigger with a name; the destructive action comes last.

```tsx
import { Button } from "@senvenli/react/action";
import { Menu, toast } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Menu
      aria-label="Actions for order ORD-1040"
      trigger={<Button variant="subtle" iconOnly iconBefore="ellipsis" aria-label="More actions" />}
      onAction={(id) => toast(`Action: ${id}`)}
      items={[
        { id: "edit", label: "Edit", icon: "pencil" },
        { id: "duplicate", label: "Duplicate", icon: "copy" },
        { id: "archive", label: "Archive", icon: "archive" },
        { id: "delete", label: "Delete", icon: "trash-2", tone: "danger" },
      ]}
    />
  );
}
```

## Examples

### Sections, shortcuts and a disabled item

Titled groups; a disabled item says why.

```tsx
import { Button } from "@senvenli/react/action";
import { Menu } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Menu
      placement="bottom start"
      trigger={<Button iconAfter="chevron-down">Invoice</Button>}
      items={[
        {
          id: "edit",
          title: "Edit",
          items: [
            { id: "rename", label: "Rename", icon: "pencil", shortcut: "R" },
            { id: "duplicate", label: "Duplicate", icon: "copy", shortcut: "D" },
          ],
        },
        {
          id: "share",
          title: "Share",
          items: [
            { id: "download", label: "Download PDF", icon: "download" },
            { id: "send", label: "Send invoice (payment pending)", icon: "send", isDisabled: true },
          ],
        },
      ]}
    />
  );
}
```

### Holding a choice

A selection mode adds checkmarks: a view option that stays set.

```tsx
import { Button } from "@senvenli/react/action";
import { Menu } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Menu
      aria-label="Row density"
      placement="bottom start"
      trigger={<Button iconBefore="rows-3">Density</Button>}
      selectionMode="single"
      defaultSelectedKeys={["comfortable"]}
      items={[
        { id: "compact", label: "Compact" },
        { id: "comfortable", label: "Comfortable" },
        { id: "spacious", label: "Spacious" },
      ]}
    />
  );
}
```

### Composed from parts

`MenuItem`, `MenuSection`, `MenuSubmenu` and `MenuSeparator` as children; an item's label can hold more than text.

```tsx
import { Button } from "@senvenli/react/action";
import { Avatar } from "@senvenli/react/display";
import { Menu, MenuItem, MenuSection, MenuSeparator, MenuSubmenu } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Menu trigger={<Button iconBefore="user">Account</Button>} aria-label="Account">
      <MenuItem id="profile" textValue="Sofia Marin">
        <span className="flex items-center gap-ds-100">
          <Avatar name="Sofia Marin" src="/avatars/05.webp" size="small" />
          <span className="flex flex-col">
            <span className="text-ds-text">Sofia Marin</span>
            <span className="text-body-small text-ds-text-subtle">sofia@example.com</span>
          </span>
        </span>
      </MenuItem>
      <MenuSeparator />
      <MenuSection title="Workspace">
        <MenuItem id="settings" icon="settings" shortcut="⌘,">
          Settings
        </MenuItem>
        <MenuSubmenu id="switch" label="Switch workspace" icon="arrow-left-right">
          <MenuItem id="acme">Acme</MenuItem>
          <MenuItem id="globex">Globex</MenuItem>
        </MenuSubmenu>
      </MenuSection>
      <MenuSeparator />
      <MenuItem id="sign-out" icon="log-out" tone="danger">
        Sign out
      </MenuItem>
    </Menu>
  );
}
```

### With a submenu

An item with children opens a second level on hover and the arrow key.

```tsx
import { Button } from "@senvenli/react/action";
import { Menu } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Menu
      placement="bottom start"
      trigger={<Button iconAfter="chevron-down">Project</Button>}
      items={[
        { id: "open", label: "Open", icon: "folder-open" },
        {
          id: "move",
          label: "Move to",
          icon: "folder-input",
          children: [
            { id: "move-design", label: "Design" },
            { id: "move-engineering", label: "Engineering" },
            { id: "move-archive", label: "Archive" },
          ],
        },
        { id: "export", label: "Export", icon: "download" },
      ]}
    />
  );
}
```

## Guidelines

### When to use it

- Several actions for one object that don't all fit as buttons.

### Use something else when

- [context-menu](https://design.senvenli.com/docs/components/context-menu.md): The actions belong to what is under the pointer.
- [select](https://design.senvenli.com/docs/components/select.md): People pick a value, not run an action.
- [button-group](https://design.senvenli.com/docs/components/button-group.md): Two or three actions that fit as visible buttons.

### Do and avoid

- Items are verbs; a disabled item says why.

## Accessibility

Keyboard:

- Arrow keys: Moves between items.
- Typing: Jumps to an item.
- Home, End: Goes to the first or last item.

Built in:

- Announced as a menu button and menu.

You provide:

- A name for an icon-only menu button.
- With children: a textValue on a MenuItem whose label is not plain text, so typing can jump to it; the destructive item last, after a MenuSeparator.

## API reference

### Menu

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| trigger | `ReactElement<unknown, string \| JSXElementConstructor<any>>` |  | yes | The pressable that opens it: the row "…" button, the account button. |
| items | `readonly MenuEntry[]` |  | no | Items, or sections of items with a title. Leave it out to compose the menu as children instead. |
| children | `React.ReactNode` |  | no | With no `items`: `MenuItem`, `MenuSection`, `MenuSubmenu` and `MenuSeparator`, in the order given (put the destructive item last yourself; with `items` the menu moves it there). |
| onAction | `(id: Key) => void` |  | no |  |
| disabledKeys | `Iterable<Key>` |  | no |  |
| placement | `Placement` |  | no | "bottom end" by default: under the trigger, aligned to its inline end, as row menus sit. |
| selectionMode | `"none" \| "single" \| "multiple"` |  | no | For menus that also hold a choice (a view option): checkmarks on the selected items. |
| selectedKeys | `Iterable<Key>` |  | no |  |
| defaultSelectedKeys | `Iterable<Key>` |  | no |  |
| onSelectionChange | `(keys: Selection) => void` |  | no |  |
| isOpen | `boolean` |  | no |  |
| onOpenChange | `(isOpen: boolean) => void` |  | no |  |
| aria-label | `string` |  | no | Names the menu when the trigger's own name is not enough. |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### MenuItem

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `Key` |  | yes |  |
| children | `React.ReactNode` |  | yes | The label: a verb ("Edit"). It may hold more than text (an avatar, two lines); then give `textValue`. |
| textValue | `string` |  | no | The words typing jumps to; the label itself when it is plain text. |
| icon | `IconName` |  | no |  |
| shortcut | `string` |  | no | Shown in mono at the end ("E"). |
| isDisabled | `boolean` |  | no |  |
| tone | `"danger"` |  | no | `danger` for the destructive action; put it last, after a `MenuSeparator`. |
| href | `string` |  | no | Makes the item a link; it goes through the host router. |
| lang | `string` |  | no | The language of the label, when it differs from the page. |

### MenuSection

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `Key` |  | no |  |
| title | `string` |  | yes | The section's title ("Share"); it names the group. |
| children | `React.ReactNode` |  | yes | `MenuItem`s and `MenuSubmenu`s. |

### MenuSubmenu

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `Key` |  | yes |  |
| lang | `string` |  | no | The language of the label, when it differs from the page. |
| isDisabled | `boolean` |  | no |  |
| icon | `IconName` |  | no |  |
| shortcut | `string` |  | no | Shown in mono at the end ("E"). |
| label | `string` |  | yes | The item that opens the submenu ("Move to"). |
| children | `React.ReactNode` |  | yes | The submenu's `MenuItem`s. |

### Tokens it reads

`--ds-background-neutral-bold`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-blanket`, `--ds-border`, `--ds-border-focused`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-icon-danger`, `--ds-icon-subtle`, `--ds-radius-large`, `--ds-radius-small`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-inverse`, `--ds-text-subtle`, `--ds-text-subtlest`, `--font-ds-mono`

## Related

- [popover](https://design.senvenli.com/docs/components/popover.md)
- [button](https://design.senvenli.com/docs/components/button.md)
