# Menu button

A button that opens a menu of actions.

_Diagram: A menu button is the Actions button at the end of a table row, opening a list of actions under it._

## Live example

### Actions menu

```html
<dui-menu>
  <button type="button" popovertarget="row-actions">Actions</button>
  <div id="row-actions" popover data-popover>
    <button type="button">Edit</button>
    <button type="button">Duplicate</button>
    <button type="button">Archive</button>
    <hr>
    <button type="button">Delete</button>
  </div>
</dui-menu>
<p role="status" id="menu-result"></p>
```

### Links are not a menu

```html
<button type="button" popovertarget="account-links">Account</button>
<div id="account-links" popover data-popover>
  <nav aria-label="Account" data-nav>
    <ul>
      <li><a href="#links-title">Profile</a></li>
      <li><a href="#links-title">Billing</a></li>
      <li><a href="#links-title">Sign out</a></li>
    </ul>
  </nav>
</div>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `<dui-menu>` |  | wraps the button and its popover |
| `popovertarget="menu-id"` | the `<button>` (native) |  |
| `popover data-popover` | the menu element |  |
| `<button> or <a href> children` |  | the actions; `<hr>` separates groups |

Added by the component: `aria-haspopup`, `aria-expanded`, `role="menu"`, `role="menuitem"`, `tabindex="-1"`.

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Enter</kbd> / <kbd>Space</kbd> / <kbd>Down Arrow</kbd> | on the button, opens the menu and focuses the first item |
| <kbd>Up Arrow</kbd> | on the button, opens the menu and focuses the last item |
| <kbd>Down Arrow</kbd> / <kbd>Up Arrow</kbd> | next or previous item, wrapping |
| <kbd>Home</kbd> / <kbd>End</kbd> | first or last item |
| <kbd>A letter</kbd> | next item whose label starts with it |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | activates the item and closes the menu |
| <kbd>Esc</kbd> | closes the menu and returns focus to the button |
| <kbd>Tab</kbd> | closes the menu and moves on |

## Accessibility

- `<dui-menu>` adds the ARIA and arrow keys. Without JavaScript, every action is a Tab stop.
- For links to other pages, use a disclosure: `role="menu"` means arrow keys and no Tab stops ([APG note](https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/examples/disclosure-navigation/)).

## Specifications

- [APG Menu Button pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menu-button/)
- [APG Menu and Menubar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menubar/)
- [MDN `menu` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/menu_role)
- [MDN `aria-haspopup`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-haspopup)
- [WHATWG popover](https://html.spec.whatwg.org/multipage/popover.html)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/menu-button.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/menu-button.css), [`examples/molecules/menu-button.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/menu-button.html)
- Demos: Dashboard view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/index.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/DashboardPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/dashboard), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/index.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/DashboardPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/DashboardPage.svelte)); Account settings view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/settings.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/SettingsPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/settings), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/settings.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/SettingsPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/SettingsPage.svelte)); Orders view ([Plain HTML](https://github.com/odonckers/domestic-ui/blob/main/demos/vanilla/orders.html), [React](https://github.com/odonckers/domestic-ui/blob/main/demos/react/src/OrdersPage.tsx), [Angular](https://github.com/odonckers/domestic-ui/tree/main/demos/angular/src/app/orders), [Astro](https://github.com/odonckers/domestic-ui/blob/main/demos/astro/src/pages/orders.astro), [Vue](https://github.com/odonckers/domestic-ui/blob/main/demos/vue/src/OrdersPage.vue), [Svelte](https://github.com/odonckers/domestic-ui/blob/main/demos/svelte/src/OrdersPage.svelte))

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Pull-down buttons](https://developer.apple.com/design/human-interface-guidelines/pull-down-buttons)
- [Material Design 3: Menus](https://m3.material.io/components/menus/overview)
- [Vercel Geist: Menu](https://vercel.com/geist/menu)
