Web Application Menus
Web application menus have a similar basis to navigation menus. They often consist of a horizontal menu bar and use fly-out functionality. There are some WAI-ARIA roles that are helping users with assistive technology to operate menus similar to desktop software menus. When using the menu WAI-ARIA attributes, the keyboard interaction should be similar to desktop software as well: the tab key is used to iterate through the top-level items only, the up and down arrows are used to navigate the sub menus. This keyboard behavior doesn’t come with the WAI-ARIA attributes, but needs to be added using scripting. A detailed explanation on the WAI-ARIA attributes and keyboard behavior can be found in the WAI-ARIA Authoring Practices document (draft).
In addition to the aria-expanded
and aria-haspopup
attributes, the following roles are used in the example:
aria-menubar
: Represents a usually horizontal menu bar.aria-menu
: Represents a set of links or commands in a menu bar, it is used for the fly-out menus.aria-menuitem
: Represents an individual menu item.
The markup has no links, it is a bare nested list with some WAI-ARIA roles. As we will add keyboard and mouse interaction on our own, this is enough.
First, we collect all top-level menu items in a variable (appsMenuItems
) as well as all submenu items (subMenuItems
). An object is defined with the key codes of the keys that need to be handled. This makes the code much more readable. Two variables keep track of the focus in top-level items (currentIndex
) and in submenus (subIndex
).
To make the menu work for keyboard users, a tabindex
attribute with the value -1
is added to the menu items. This enables scripts to set the focus on the element. The first menu item (“File” in this example) is assigned a tabindex
value of 0
which adds it to the tab order and lets the user access the menu. The currentIndex
variable is initialized as soon as this first item gets focus.
All top-level menu items close open submenus when they receive focus and reset the subIndex
variable. When individual items are clicked, the visibility of the submenu is toggled by changing the aria-expanded
value. If a key is pressed, the appropriate action is carried out.
Key | Action |
---|---|
tab ⇥ | Select the next top-level menu item |
right → | |
shift ⇧ + tab ⇥ | Select the previous top-level menu item |
left ← | |
return/enter ↵ | Open the submenu, select first submenu item. |
space | |
down ↓ | |
up ↑ | Open the submenu, select last submenu item. |
esc | Leave the menu |
Submenu items do behave differently when interacting with them on the keyboard, see the following table for details:
Key | Action |
---|---|
tab ⇥ | Close the submenu, select the next top-level menu item |
right → | |
shift ⇧ + tab ⇥ | Close the submenu, select the previous top-level menu item |
left ← | |
return/enter ↵ | Carry out function of this item. (In this example: bring up a dialog box with the text of the chosen menu item.) |
space | |
down ↓ | Select next submenu item |
up ↑ | Select previous submenu item |
esc | Close the submenu, select the current top-level menu item |