Skip to content

Menu Button ​

A menu button is an interactive pattern used for account menus, table row action dropdowns ("⋯"), and lists of actionable commands rather than form values.

Because there is no native HTML element for this pattern, teams must implement the WAI-ARIA menu / menuitem specification, ensuring strict keyboard event handling and focus restoration.

Live Demo ​

Focus the trigger button below and press the Down arrow key (↓) to expand the menu and navigate through items using the Arrow keys:

Keyboard Interaction ​

Keyboard operations and expected behavior
Key InteractionExpected Behavioral Contract
TabFocus the trigger button; closes the menu if previously open, advancing naturally to the next page element
↓ (Down Arrow)Expand the menu and place keyboard focus on the first menu item
↑ / ↓Cycle focus backward and forward between menu items
Home / EndJump focus immediately to the first or last menu item
EscClose the menu and restore focus to the trigger button
Enter / SpaceExecute the focused menu item and close the menu

Pointer Interactions ​

Pointer operations and expected behavior
Pointer ActionExpected Behavioral Contract
Click TriggerToggles the expanded state of the menu
Click Menu ItemExecutes the selected action and closes the menu (matching the outcome of keyboard Enter)
Click Outside MenuDismisses the open menu without executing any action

Structure ​

Options ▾aria-haspopup="true"aria-expandedEditDuplicateDeleterole="menuitem",DOM focus(not activedescendant)

Rules ​

Give the menu button aria-haspopup and an aria-expanded that follows its state MUSTUI-R28
Add aria-haspopup="true" and switch aria-expanded between true and false as the menu opens and closes. Screen readers announce that the button opens a menu, and whether it is open.
Check: MNB-01MNB-01 aria-expanded follows the menu✕ NGStill false when openMenu button whose aria-expanded stays false while the menu is open✓ OKFollows open and closedMenu button whose aria-expanded switches to true when the menu opens
Open the menu and move through items with the arrow keys; run an item with Enter or Space MUSTUI-R29
↓ on the trigger opens the menu and moves to the first item, ↑ ↓ cycle through the items, and Home End go to the first and last. Every item can be chosen with the keyboard alone.
Check: MNB-02MNB-02 Menu keyboard keys✕ NGDown does not open itPressing Down on the trigger does not open the menu✓ OKOpens, first item focusedPressing Down on the trigger opens the menu and focuses the first item
Move real DOM focus to the menu items MUSTUI-R30
Focus each <button role="menuitem"> instead of pointing at it virtually with aria-activedescendant. The focus position is conveyed reliably both on screen and to screen readers.
Check: MNB-03MNB-03 Focus on menu items✕ NGFocus stays on the triggerMenu where focus stays on the trigger while the highlight moves between items✓ OKThe item gets DOM focusMenu where focus moves to the item itself
Close the menu on Esc, Tab and a click outside MUSTUI-R31
Esc closes it and returns focus to the trigger; Tab closes it and moves on to the next element. An open menu never lingers in the way of other actions.
Check: MNB-04MNB-04 Focus after Esc closes✕ NGFocus does not returnAfter Esc closes the menu, no element has focus✓ OKFocus returns to the triggerEsc closes the menu and focus returns to the trigger

Implementation ​

A complete, production-grade reference module:

View Source Code (HTML Markup and JavaScript Module)
html
<button id="menu-trigger" aria-haspopup="true" aria-expanded="false" aria-controls="menu">
  Actions
</button>
<ul id="menu" role="menu" aria-labelledby="menu-trigger" hidden>
  <li role="none"><button type="button" role="menuitem" tabindex="-1">Edit</button></li>
  <li role="none"><button type="button" role="menuitem" tabindex="-1">Duplicate</button></li>
  <li role="none"><button type="button" role="menuitem" tabindex="-1">Delete</button></li>
</ul>
javascript
// modules/menu-button.js
export function initMenuButton(trigger, menu) {
  const items = [...menu.querySelectorAll('[role="menuitem"]')]

  function open(focusIndex = 0) {
    menu.hidden = false
    trigger.setAttribute('aria-expanded', 'true')
    items[focusIndex].focus()
  }

  function close() {
    menu.hidden = true
    trigger.setAttribute('aria-expanded', 'false')
    trigger.focus()
  }

  trigger.addEventListener('click', () => (menu.hidden ? open() : close()))
  trigger.addEventListener('keydown', (event) => {
    if (event.key === 'ArrowDown') {
      event.preventDefault()
      open(0)
    }
  })

  items.forEach((item, index) => {
    item.addEventListener('click', close)
    item.addEventListener('keydown', (event) => {
      const last = items.length - 1
      if (event.key === 'ArrowDown') {
        items[index === last ? 0 : index + 1].focus()
      } else if (event.key === 'ArrowUp') {
        items[index === 0 ? last : index - 1].focus()
      } else if (event.key === 'Home') {
        items[0].focus()
      } else if (event.key === 'End') {
        items[last].focus()
      } else if (event.key === 'Escape') {
        close()
      } else if (event.key === 'Tab') {
        menu.hidden = true
        trigger.setAttribute('aria-expanded', 'false')
        return
      } else {
        return
      }
      event.preventDefault()
    })
  })

  document.addEventListener('click', (event) => {
    if (!menu.hidden && !menu.contains(event.target) && event.target !== trigger) {
      menu.hidden = true
      trigger.setAttribute('aria-expanded', 'false')
    }
  })
}