# Menu (/components/menu)

<ReferenceLinks
  links={[
    { label: "Source", href: meta.links.source, iconName: "code" },
    { label: "Stories", href: meta.links.stories },
  ]}
/>

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Entity actions\n    </Button>\n  }\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n    { text: \"View history\", onClick: () => {} },\n    {\n      text: \"Delete entity\",\n      icon: \"trash\",\n      tone: \"error\",\n      onClick: () => {},\n    },\n  ]}\n/>\n<EllipsisMenu\n  items={[\n    { text: \"Rename view\", onClick: () => {} },\n    { text: \"Duplicate view\", onClick: () => {} },\n    { text: \"Delete view\", tone: \"error\", onClick: () => {} },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Entity actions
      </Button>
    }
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
      { text: "View history", onClick: noop },
      {
        text: "Delete entity",
        icon: "trash",
        tone: "error",
        onClick: noop,
      },
    ]}
  />
  <EllipsisMenu
    items={[
      { text: "Rename view", onClick: noop },
      { text: "Duplicate view", onClick: noop },
      { text: "Delete view", tone: "error", onClick: noop },
    ]}
  />
</Stage>

## Installation [#installation]

1. Install the package.

```sh
npm install @hashintel/ds-components
```

2. Import the component.

```tsx
import { Button, Menu } from "@hashintel/ds-components";

export const EntityActionsMenu = () => (
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Entity actions
      </Button>
    }
    items={[
      { text: "Edit entity", onClick: () => {} },
      { text: "Duplicate entity", onClick: () => {} },
    ]}
  />
);
```

### When to use [#when-to-use]

<DoList
  items={[
    "Use Menu to collect secondary actions on an object — edit, duplicate, archive — behind one trigger, where showing them all as buttons would crowd the view.",
    "Use EllipsisMenu for per-row actions in tables, cards, and list rows — it is the standard overflow trigger.",
  ]}
/>

### When not to use [#when-not-to-use]

<DontList
  items={[
    "Don't use Menu to choose a value in a form — use Select, which keeps the chosen value visible in the field.",
    "Don't hide one action behind a menu — show them as Buttons; the extra click buys nothing.",
    "Don't assemble panels or forms out of custom rows — put free-form interactive content in a Popover.",
  ]}
/>

## Anatomy [#anatomy]

Menu is composed of the following parts:

- `trigger` — the element that opens the menu and anchors it: any element
  accepting `aria-expanded`, usually a Button.
- `items` — the menu's content: action (`onClick`), link (`href`), and submenu
  (`subItems`) items, mixed freely with labelled groups and custom rows.
- `header` and `footer` — pinned above and below the items, outside the
  scrollable area, for context or persistent actions.

```tsx
<Menu
  trigger={<Button>Entity actions</Button>} // opens and anchors the menu
  header={<strong>Alice Chen</strong>} // pinned above the items
  items={[
    { text: "Rename", onClick: rename }, // an action
    { text: "View history", href: historyUrl }, // a link
    {
      text: "Move to workspace", // a submenu
      subItems: [{ text: "Acme Corp", onClick: moveEntity }],
    },
    {
      id: "danger",
      label: "Danger", // a labelled group — dividers draw between groups
      items: [{ text: "Delete entity", tone: "error", onClick: confirmDelete }],
    },
  ]}
  footer={<Button variant="link">View all actions</Button>} // pinned below
/>
```

## Examples [#examples]

### Basic [#basic]

Every item takes `text` plus exactly one of `onClick`, `href`, or `subItems`.
A plain-string `text` doubles as the item's `id`.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Entity actions\n    </Button>\n  }\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n    { text: \"Copy entity ID\", onClick: () => {} },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Entity actions
      </Button>
    }
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
      { text: "Copy entity ID", onClick: noop },
    ]}
  />
</Stage>

### Icons and descriptions [#icons-and-descriptions]

`icon` reinforces the label, `description` adds one line of secondary text,
and `suffix` renders trailing content such as a count. Don't overuse icons in menus —
icons should differentiate important menu items, not decorate every row.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Flow actions\n    </Button>\n  }\n  items={[\n    {\n      text: \"Run flow\",\n      icon: \"play\",\n      description: \"Uses the last saved inputs\",\n      onClick: () => {},\n    },\n    {\n      text: \"Schedule run\",\n      description: \"Repeat on a cron schedule\",\n      onClick: () => {},\n    },\n    {\n      text: \"View run history\",\n      suffix: <span>24</span>,\n      onClick: () => {},\n    },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Flow actions
      </Button>
    }
    items={[
      {
        text: "Run flow",
        icon: "play",
        description: "Uses the last saved inputs",
        onClick: noop,
      },
      {
        text: "Schedule run",
        description: "Repeat on a cron schedule",
        onClick: noop,
      },
      {
        text: "View run history",
        suffix: <span>24</span>,
        onClick: noop,
      },
    ]}
  />
</Stage>

### Grouped items [#grouped-items]

A group is `{ id, label, items }`; groups draw their own separators and an
uppercase label. Group related actions once a menu passes roughly six items.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Type actions\n    </Button>\n  }\n  items={[\n    {\n      id: \"view\",\n      label: \"View\",\n      items: [\n        { text: \"Open entity type\", onClick: () => {} },\n        { text: \"View dependents\", onClick: () => {} },\n      ],\n    },\n    {\n      id: \"manage\",\n      label: \"Manage\",\n      items: [\n        { text: \"Create new version\", onClick: () => {} },\n        { text: \"Duplicate type\", onClick: () => {} },\n      ],\n    },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Type actions
      </Button>
    }
    items={[
      {
        id: "view",
        label: "View",
        items: [
          { text: "Open entity type", onClick: noop },
          { text: "View dependents", onClick: noop },
        ],
      },
      {
        id: "manage",
        label: "Manage",
        items: [
          { text: "Create new version", onClick: noop },
          { text: "Duplicate type", onClick: noop },
        ],
      },
    ]}
  />
</Stage>

### Disabled and loading items [#disabled-and-loading-items]

Disabled and loading items stay visible but can't be activated, and arrow
keys skip them. `loading` adds a spinner — use it while an item's
availability is still being determined.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"download\">\n      Export\n    </Button>\n  }\n  items={[\n    { text: \"Export as CSV\", onClick: () => {} },\n    { text: \"Export as JSON\", onClick: () => {} },\n    { text: \"Export with history\", disabled: true, onClick: () => {} },\n    { text: \"Checking snapshot size\", loading: true, onClick: () => {} },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="download">
        Export
      </Button>
    }
    items={[
      { text: "Export as CSV", onClick: noop },
      { text: "Export as JSON", onClick: noop },
      { text: "Export with history", disabled: true, onClick: noop },
      { text: "Checking snapshot size", loading: true, onClick: noop },
    ]}
  />
</Stage>

### Destructive items [#destructive-items]

Set `tone="error"` on destructive actions, and place them last in a group of
their own so they can't be hit by overshooting an adjacent item.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Entity actions\n    </Button>\n  }\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n    {\n      text: \"Delete entity\",\n      icon: \"trash\",\n      tone: \"error\",\n      onClick: () => {},\n    },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Entity actions
      </Button>
    }
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
      {
        text: "Delete entity",
        icon: "trash",
        tone: "error",
        onClick: noop,
      },
    ]}
  />
</Stage>

### Link items [#link-items]

Items with `href` render real anchors, so they support open-in-new-tab and
middle-click. Add `target="_blank"` for external destinations.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Resources\n    </Button>\n  }\n  items={[\n    { text: \"Entity type schema\", href: \"#\" },\n    { text: \"Property definitions\", href: \"#\" },\n    {\n      text: \"HASH docs\",\n      icon: \"externalLink\",\n      href: \"https://hash.ai/docs\",\n      target: \"_blank\",\n    },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Resources
      </Button>
    }
    items={[
      { text: "Entity type schema", href: "#" },
      { text: "Property definitions", href: "#" },
      {
        text: "HASH docs",
        icon: "externalLink",
        href: "https://hash.ai/docs",
        target: "_blank",
      },
    ]}
  />
</Stage>

### Submenus [#submenus]

Items with `subItems` open a nested menu to the side and show a chevron. Avoid 
more than one level of nesting — deeper trees are hard to traverse with some input
devices.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Entity actions\n    </Button>\n  }\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    {\n      text: \"Export as\",\n      subItems: [\n        { text: \"CSV\", onClick: () => {} },\n        { text: \"JSON\", onClick: () => {} },\n      ],\n    },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Entity actions
      </Button>
    }
    items={[
      { text: "Edit entity", onClick: noop },
      {
        text: "Export as",
        subItems: [
          { text: "CSV", onClick: noop },
          { text: "JSON", onClick: noop },
        ],
      },
    ]}
  />
</Stage>

### Header and footer [#header-and-footer]

`header` and `footer` pin above and below the items, outside the scrollable
area. They come undecorated — add your own divider if the design needs one.
Set `swapHeaderFooterOnFlip` to keep the header on the edge nearest the
trigger when the menu opens upward.

<Stage code={"<Menu\n  swapHeaderFooterOnFlip\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Recent runs\n    </Button>\n  }\n  header={<strong>Research flow</strong>}\n  footer={\n    <Button size=\"xs\" variant=\"link\" href=\"#\">\n      View all runs\n    </Button>\n  }\n  items={[\n    { text: \"Run #128 — succeeded\", onClick: () => {} },\n    { text: \"Run #127 — succeeded\", onClick: () => {} },\n    { text: \"Run #126 — failed\", onClick: () => {} },\n  ]}\n/>"}>
  <Menu
    swapHeaderFooterOnFlip
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Recent runs
      </Button>
    }
    header={<strong>Research flow</strong>}
    footer={
      <Button size="xs" variant="link" href="#">
        View all runs
      </Button>
    }
    items={[
      { text: "Run #128 — succeeded", onClick: noop },
      { text: "Run #127 — succeeded", onClick: noop },
      { text: "Run #126 — failed", onClick: noop },
    ]}
  />
</Stage>

### Custom rows [#custom-rows]

An entry of `{ custom }` renders arbitrary content on its own row. Arrow keys
will skip it — but focusable children inside it stay reachable with Tab.

<Stage code={"<Menu\n  trigger={\n    <Button variant=\"subtle\" iconName=\"chevronDown\" iconPosition=\"right\">\n      Workspace\n    </Button>\n  }\n  items={[\n    {\n      custom: (\n        <span>\n          <strong>Acme Corp</strong> — 3 of 5 seats used\n        </span>\n      ),\n    },\n    { text: \"Invite teammate\", onClick: () => {} },\n    { text: \"Workspace settings\", onClick: () => {} },\n  ]}\n/>"}>
  <Menu
    trigger={
      <Button variant="subtle" iconName="chevronDown" iconPosition="right">
        Workspace
      </Button>
    }
    items={[
      {
        custom: (
          <span>
            <strong>Acme Corp</strong> — 3 of 5 seats used
          </span>
        ),
      },
      { text: "Invite teammate", onClick: noop },
      { text: "Workspace settings", onClick: noop },
    ]}
  />
</Stage>

### Placement [#placement]

`position` sets the preferred side and alignment relative to the trigger,
defaulting to `bottom-start`. It is a preferred position, and another placement is
automatically chosen when the menu won't fit.

<Stage code={"<Menu\n  position=\"bottom-start\"\n  trigger={<Button variant=\"subtle\">bottom-start</Button>}\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n  ]}\n/>\n<Menu\n  position=\"bottom-end\"\n  trigger={<Button variant=\"subtle\">bottom-end</Button>}\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n  ]}\n/>\n<Menu\n  position=\"top-start\"\n  trigger={<Button variant=\"subtle\">top-start</Button>}\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n  ]}\n/>"}>
  <Menu
    position="bottom-start"
    trigger={<Button variant="subtle">bottom-start</Button>}
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
    ]}
  />
  <Menu
    position="bottom-end"
    trigger={<Button variant="subtle">bottom-end</Button>}
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
    ]}
  />
  <Menu
    position="top-start"
    trigger={<Button variant="subtle">top-start</Button>}
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
    ]}
  />
</Stage>

### EllipsisMenu [#ellipsismenu]

EllipsisMenu is Menu with a ready-made icon-button trigger — the standard
overflow affordance for table rows and cards.

<Stage code={"<EllipsisMenu\n  items={[\n    { text: \"Edit entity\", onClick: () => {} },\n    { text: \"Duplicate entity\", onClick: () => {} },\n    { text: \"Delete entity\", tone: \"error\", onClick: () => {} },\n  ]}\n/>\n<EllipsisMenu\n  iconName=\"bell\"\n  variant=\"solid\"\n  size=\"xs\"\n  items={[\n    { text: \"Mute notifications\", onClick: () => {} },\n    { text: \"Notification settings\", onClick: () => {} },\n  ]}\n/>\n<EllipsisMenu disabled items={[{ text: \"Edit entity\", onClick: () => {} }]} />"}>
  <EllipsisMenu
    items={[
      { text: "Edit entity", onClick: noop },
      { text: "Duplicate entity", onClick: noop },
      { text: "Delete entity", tone: "error", onClick: noop },
    ]}
  />
  <EllipsisMenu
    iconName="bell"
    variant="solid"
    size="xs"
    items={[
      { text: "Mute notifications", onClick: noop },
      { text: "Notification settings", onClick: noop },
    ]}
  />
  <EllipsisMenu disabled items={[{ text: "Edit entity", onClick: noop }]} />
</Stage>

## Behavior [#behavior]

- The menu's content mounts renders in a portal above the page, preventing most z-index + overflow issues.
- While open, the trigger carries `aria-expanded` and a Button trigger also
  shows its pressed visual.
- Selecting an item closes the menu unless that item sets `keepOpenOnSelect`.
- With an empty `items` array, Menu renders just the trigger with no menu
  behind it; EllipsisMenu disables its trigger instead.
- Arrow keys wrap from the last item to the first (and back) on discrete
  presses, but a held key stops at the boundary instead of cycling.

## Content [#content]

- Label the trigger with the scope of the actions — "Entity actions", not
  "Menu" or "Options".
- Write item labels verb-first, 1–3 words, sentence case: "Edit entity",
  "Duplicate", "Copy entity ID". No punctuation.
- Name the object when the menu mixes targets — "Delete property" beats
  "Delete" when nearby items act on the entity. But do not name the object
  when there are no mixed targets as the label then becomes redundant
- Keep `description` to one short clause of consequence or context, not a
  restatement of the label.
- Group labels should be 1–2 word nouns: "View", "Manage".
- Order items by expected frequency of use, and destructive actions last.

## Accessibility [#accessibility]

Menu implements the
[WAI-ARIA menu button pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/).

### What Menu provides [#what-menu-provides]

- The trigger is wired with `aria-haspopup` and `aria-expanded`; closing the
  menu returns focus to it.
- Disabled and loading items are exposed as disabled and skipped by arrow-key
  navigation.
- Item `icon`s are `aria-hidden`

### What you must do [#what-you-must-do]

- Make every label carry its full meaning without the icon — icons are not
  announced to screen readers.
- Provide an explicit `id` when `text` is JSX; the id is also what `onClick`
  and `onKeyDown` report.

### Keyboard [#keyboard]

| Key                     | Action                                                                                                                                                                                                                                 |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Tab` / `Shift+Tab`     | When the menu has a focusable `header`, `footer`, or custom row, moves between those rows and the item list. Past either edge the menu closes — `Tab` hands focus to the element after the trigger, `Shift+Tab` to the trigger itself. |

## API reference [#api-reference]

### Menu [#menu]

<PropsTable of={Menu} defaults={{ position: '"bottom-start"' }} />

### MenuItem [#menuitem]

The shape of `items` and `subItems` entries. Each takes `text` plus exactly
one of `onClick`, `href`, or `subItems` — or a lone `custom` node for a
presentational row.

<PropsTable of={MenuItemDoc} defaults={{ selectedStyle: '"highlight"' }} />

### MenuItemGroup [#menuitemgroup]

A labelled group of items, usable wherever items are: `{ id, label, items }`.

<PropsTable of={MenuItemGroupDoc} />

### EllipsisMenu [#ellipsismenu-1]

Accepts every Menu prop except `trigger`, plus the trigger-styling props
below.

<PropsTable
  of={EllipsisMenu}
  defaults={{
    iconName: '"ellipsis"',
    variant: '"subtle"',
    position: '"bottom-end"',
  }}
/>
