Dialogs
Dialogs are a little bit trickier to make accessible, but with a few key principles, they can be made keyboard navigable and usable with screen readers.
Semantic dialog
Start VoiceOver if you are on your Mac and press enter on the Show Dialog button. You will notice that:
- VoiceOver announces that a dialog is opened and the name of the dialog because we have a
roleof 'dialog' with a label associated usingaria-labelledby. - When the dialog opens, focus lands in the first focusable element in the dialog.
- Tabbing keeps focus within the dialog. Note that you have to write code to restrict the tab sequence to the dialog.
- Closing the dialog places focus back on the source button.
- Pressing the
esckey closes the dialog and places the focus back on the source button.- Note that for widgets like typeaheads, pressing
escshould close the typeahead and not the dialog. - Note that if you have textual content in the dialog besides labels, you should associate it with an
aria-describedbyto the right input field so it gets announced when you tab to the input field.
- Note that for widgets like typeaheads, pressing
Live editor
Result
Shipping Address
Exercise: Unsemantic dialog
The dialog below is unsemantic:
- The fact that a dialog opened up is not announced to VoiceOver.
- The heading that labels the dialog is not announced when the dialog is opened. To fix these issues:
- Change the
<b>to a semantich2tag. - To the div with
id"container2", add aroleof 'dialog'. - To the div with
id"container2", addaria-labelledby='dialog-title'. Verify with VoiceOver.
Live editor
Result
Shipping Address