5.2: Toggle Buttons (Activity Example)
The remainder of the instruction here is hands-on. You’ll be taking inaccessible widgets, like the example of toggle buttons described here, and making them accessible by adding appropriate WAI-ARIA and keyboard operability. The toggle buttons widget demonstrated here is provided as an example for the ten widgets you will be working on over the next three units, describing the Activity Elements you will find in each exercise.
Roles, states, and properties for toggle buttons
- role=”button”
- tabindex=”0″
- aria-label=”[button name]”
- aria-pressed=”[true|false]”
Activity Element: Each widget will have an inaccessible JSFiddle version provided, like the one below. You can examine the JavaScript and HTML to observe how the widget was created. Under the Result tab, view and try out the widget to see how it functions. CSS is also provided, though you will not be working with CSS as part of the activities. In the JSFiddle here, the accessibility elements are included but commented out so you can see how the code snippets below have been applied. In the activities that follow, the accessibility elements will not be present. Your task will be to apply the code snippets yourself to make the inaccessible version provided in the activity file accessible.
At the top right, you may choose to “Edit in JSFiddle” and test the code snippets that will be provided below, to understand how they add accessibility to the widget. You can start by uncommenting the accessibility elements for the toggle buttons, and testing the resulting version with ChromeVox.
The following JSFiddle presents a typical toggle button. Review the JavaScript and HTML markup. Test the buttons present under the Result tab with ChromeVox to understand how it functions without any accessibility features added (if it functions at all). You can work in JSFiddle itself by clicking “Edit in JSFiddle” and copying the accessibility/WAI-ARIA code described below to fix the accessibility of the toggle buttons, before completing the Activity on the page that follows (there is no activity that follows in this example case). Skip JSFiddle
Add a
tabindex
to each button to make them keyboard focusable, define the
role="button"
, and add a label with
aria-label="[button name]"
and set the default state to “not pressed” with
aria-pressed="false"
.
Add in equivalent keyboard access where mouse access is provided, referencing the
onActivate()
function, described below, with jQuery
.on('keydown')
.
Set
aria-pressed="[true|false]"
for buttons when activated or deactivated to announce the button’s state to screen readers.
Adding Keyboard Operability
Keyboard access for the buttons is fairly simple, with no special key press events needing to be defined.
Keyboard Interaction for Toggle Buttons
When the button has focus:
- Space: Activates the button.
- Enter: Activates the button.
-
Following button activation, focus is set depending on the type of action the button performs. For example:
- If activating the button opens a dialog, the focus moves inside the dialog (see dialog pattern ).
- If activating the button closes a dialog, focus typically returns to the button that opened the dialog unless the function performed in the dialog context logically leads to a different element. For example, activating a cancel button in a dialog returns focus to the button that opened the dialog. However, if the dialog were confirming the action of deleting the page from which it was opened, the focus would logically move to a new context.
- If activating the button does not dismiss the current context, then focus typically remains on the button after activation, e.g., an Apply or Recalculate button.
- If the button action indicates a context change, such as move to next step in a wizard or add another search criteria, then it is often appropriate to move focus to the starting point for that action.
- If the button is activated with a shortcut key, the focus usually remains in the context from which the shortcut key was activated. For example, if Alt + U were assigned to an “Up” button that moves the currently focused item in a list one position higher in the list, pressing Alt + U when the focus is in the list would not move the focus from the list.
Source: W3C WAI-ARIA 1.1 Authoring Practices
No added keyboard interaction is required for the toggle buttons beyond the standard Space bar and Enter key defined in the ik_utils.js file. Reference to these key events is added to the
onActivate()
function.
Accessible Toggle Buttons in Action
The buttons are accessed initially with the Tab key, and the Tab key is used to move between buttons. The Space bar or Enter keys are used to activate and deactivate buttons. Aim to have the widget you edit in the associated activity function like that presented in the video (there is no associated activity for this example).
Video: Accessible Toggle Buttons