Actions Menu Button Example Using element.focus()
Read This First
The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.
This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.
- There may be support gaps in some browser and assistive technology combinations, especially for mobile/touch devices. Testing code based on this example with assistive technologies is essential before considering use in production systems.
- The ARIA and Assistive Technologies Project is developing measurements of assistive technology support for APG examples.
- Robust accessibility can be further optimized by choosing implementation patterns that maximize use of semantic HTML and heeding the warning that No ARIA is better than Bad ARIA.
About This Example
This example demonstrates how the Menu Button Pattern can be used to create a button that opens an actions menu.
In this example, choosing an action from the menu will cause the chosen action to be displayed in the Last Action
edit box.
In this implementation, each item in the menu is made focusable by setting tabindex="-1"
so the JavaScript can use element.focus()
to set focus in response to events that trigger focus movement inside the menu.
An alternative technique for managing focus movement among menu items is demonstrated in the action menu button example that uses aria-activedescendant.
Similar examples include:
- Action Menu Button Example Using aria-activedescendant: A button that opens a menu of actions or commands where focus in the menu is managed using aria-activedescendant.
- Navigation Menu Button: A button that opens a menu of items that behave as links.
Example
Accessibility Features
- A down arrow icon is included to help users understand that the button opens a menu.
-
To support operating system high contrast settings:
- Because transparent borders are visible on some systems with operating system high contrast settings enabled, transparency cannot be used to create a visual difference between the element that is focused an other elements. Instead of using transparency, the focused element has a thicker border and less padding. When an element receives focus, its border changes from 1 to 3 pixels and padding is reduced by 2 pixels. When an element loses focus, its border changes from 3 pixels to 1 and padding is increased by 2 pixels.
- Because background color and text color styles can be overridden by operating system high contrast settings, a border is used to ensure the button has a visible boundary when high contrast mode is enabled.
-
To ensure the arrow icons used to indicate the expanded or collapsed state have sufficient contrast with the background when high contrast settings invert colors, the CSS
currentcolor
value for thefill
andstroke
properties of the SVGpolygon
element is used to synchronize the color with text content. If specific colors are used to specify thefill
andstroke
properties, these colors will remain the same in high contrast mode, which could lead to insufficient contrast between the icon and the background or even make the icon invisible if its color matches the high contrast mode background.
Keyboard Support
Menu Button
Key | Function |
---|---|
Down Arrow Space Enter |
Opens menu and moves focus to first menuitem |
Up Arrow | Opens menu and moves focus to last menuitem |
Menu
Key | Function |
---|---|
Enter |
|
Escape |
|
Up Arrow |
|
Down Arrow |
|
Home | Moves focus to the first menu item. |
End | Moves focus to the last menu item. |
A-Z a-z |
|
Role, Property, State, and Tabindex Attributes
Menu Button
Role | Attribute | Element | Usage |
---|---|---|---|
aria-haspopup="true" |
button
|
|
|
aria-controls="IDREF" |
button
|
|
|
aria-expanded="true" |
button |
|
Menu
Role | Attribute | Element | Usage |
---|---|---|---|
menu
|
ul
|
Identifies the ul element as a menu . |
|
|
aria-labelledby="IDREF" |
ul
|
|
menuitem
|
li
|
|
|
tabindex="-1" |
li
|
|
JavaScript and CSS Source Code
- CSS: menu-button-actions.css
- Javascript: menu-button-actions.js
HTML Source Code