Open an Actions Menu
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Build a menu button following the WAI-ARIA menu button pattern. A button "Actions" has aria-haspopup="menu" (or "true", which means the same) and aria-expanded "false" while the menu is closed and "true" while it's open. The menu is a role="menu" named after the button (aria-labelledby pointing at the button's id) with three role="menuitem" items, "Edit", "Duplicate" and "Delete", rendered only while it's open (or hidden with the hidden attribute). Move real DOM focus into the menu: give the items tabIndex={-1} and call focus() on them. Clicking the button, or pressing Enter or Space on it, opens the menu with focus on the first item; the Down arrow on the button does the same, and the Up arrow opens it with focus on the last item. In the menu, the Down and Up arrows move focus to the next or previous item, wrapping round, and Home and End to the first and last. Escape closes the menu and puts focus back on the button. Choosing an item, by click or Enter, closes the menu, puts focus back on the button and updates a line below that reads "Last action: none" at first, for example to "Last action: Duplicate". Closing the menu when focus moves elsewhere is up to you; if you do, look at document.activeElement once focus has moved rather than at the blur event's relatedTarget, which browsers don't always set.
Your code must pass
- The button is set up for a menu, closed at first
- Clicking the button opens the menu at the first item
- Down and Up arrows on the button open it at the first and last item
- Arrow keys, Home and End move focus through the items, wrapping round
- Escape closes the menu and returns focus to the button
- Choosing an item closes the menu, returns focus and runs it