Alert Dialog Example
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
The below example of a confirmation prompt demonstrates the Alert Dialog Pattern. It also includes an example of the Alert Pattern to make comparing the experiences provided by the two patterns easy.
To use this example:
-
Activate the "discard" button to trigger a confirmation dialog that has the
alertdialog
role.- Activating the "yes" button in the confirmation dialog removes the contents of both the "Notes" text area and local storage of the notes.
- Activating the "no" button or pressing escape closes the dialog.
- The "discard" button is disabled if the notes text area does not contain any text.
-
Activate the "save" button to trigger an alert when the contents of the "Notes" text area is saved to
local storage.
- A successful save triggers a short alert to notify the user that the notes have been saved.
- The "save" button is disabled if the user's local storage value is the same as the "Notes" field.
- Modify the "notes" text area as needed to enable and disable the discard and save functions.
Similar examples include:
- Alert: a basic alert.
- Modal Dialog Example: An example demonstrating multiple layers of modal dialogs with both small and large amounts of content.
- Date Picker Dialog example: Demonstrates a dialog containing a calendar grid for choosing a date.
Example
Confirmation
Are you sure you want to discard all of your notes?
Accessibility Features
- The accessible name of the alert dialog is set to its heading ("Confirmation").
- The dialog's prompt ("Are you sure...?") is referenced via
aria-describedby
to ensure that the user is immediately aware of the prompt. -
Focus is automatically set to the first focusable element inside the dialog, which is the "No"
button
. This is the least destructive action, so focusing "No" helps prevent users from accidentally confirming the destructive "Discard" action, which cannot be undone. -
When the buttons are disabled,
aria-disabled
is used instead of the HTMLdisabled
attribute so the buttons will remain in the page Tab sequence. This makes it easier for screen reader users to discover the buttons and discern how the interface works.
Keyboard Support
Key | Function |
---|---|
Tab |
|
Shift + Tab |
|
Escape | Closes the dialog. |
Command + S | (Mac only) Save the contents of the notes textarea when focused. |
Control + S | (Windows only) Save the contents of the notes textarea when focused. |
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
alertdialog |
div |
Identifies the element that serves as the alert dialog container. | |
aria-labelledby="ID_REFERENCE" |
div |
Gives the alert dialog an accessible name by referring to the element that provides the alert dialog title. | |
aria-describedby="ID_REFERENCE" |
div |
Gives the alert dialog an accessible description by referring to the alert dialog content that describes the primary message or purpose of the alert dialog. | |
aria-modal="true" |
div |
Tells assistive technologies that the windows underneath the current alert dialog are not available for interaction (inert). | |
alert |
div |
Identifies the element that serves as the alert notification. | |
aria-disabled="true" |
button |
Tells assistive technology users the button cannot be activated. |
Notes on aria-modal
and aria-hidden
-
The
aria-modal
property was introduced in ARIA 1.1. As a relatively new property, screen reader users may experience varying degrees of support for it. - Applying the
aria-modal
property to thedialog
element replaces the technique of usingaria-hidden
on the background for informing assistive technologies that content outside a dialog is inert. -
In legacy dialog implementations where
aria-hidden
is used to make content outside a dialog inert for assistive technology users, it is important that:aria-hidden
is set totrue
on each element containing a portion of the inert layer.- The dialog element is not a descendant of any element that has
aria-hidden
set totrue
.
Javascript and CSS Source Code
- CSS: alertdialog.css
- Javascript: alertdialog.js, utils.js
HTML Source Code
To copy the following HTML code, please open it in CodePen.