Menus
Menus, like dialogs, rely on a few key principles to render them usable with the keyboard or screen reader.
Accessible menu​
The example below shows how to apply ARIA attributes to an unordered list element to give it the semantics screen readers need to announce it as a menu. It also demonstrates the JavaScript functionality you need to create the appropriate keyboard interaction.
- You can move focus to the button by pressing tab.
- Pressing enter with focus on the button opens the menu and moves focus to the first menu item.
- Up and down arrows navigate the menu.
- escape closes the menu, returning focus to the button.
- If actions were implemented, pressing enter on a menu item would perform the action.
Live editor
Result
Exercise: Inaccessible menu​
Update the example below to include the necessary markup for making this menu accessible. You should address the following issues.
- The
atag with the IDmenuTriggerhas an associated popup. Therefore, it should be marked up witharia-haspopupandaria-ownsto make this relationship explicit. - The
atag with the IDmenuTriggershould be marked up as a button with the rolebutton. - The
atag with the IDmenuTriggershould be tabbable. Add an explicittabindex="0"to it. - The
ultag with the IDactionsMenushould be marked up as a menu with the rolemenu. - The
atags inside the ul with the IDactionsMenuare menu items. Mark them up with the rolemenuitem. - The
atags inside the ul with the IDactionsMenushould not be tabbable. Add an explicittabindex="-1"to each of them.
Live editor
Result