Skip to content

Commit

Permalink
Add support for role='alertdialog'.
Browse files Browse the repository at this point in the history
As described by https://www.w3.org/TR/wai-aria-1.1/#alertdialog, make the dialog modal by not hiding it when pressing the ESCAPE key.

Note that when using role='alertdialog', the `data-a11y-dialog-hide` attribute should be removed from the overlay as well to comply with the specification.
  • Loading branch information
flu0 committed Aug 20, 2018
1 parent 1068349 commit b5c9030
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions a11y-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

// Keep a reference of the node and the actual dialog on the instance
this.container = node;
this.dialog = node.querySelector('dialog, [role="dialog"]');
this.dialog = node.querySelector('dialog, [role="dialog"], [role="alertdialog"]');
this.role = this.dialog.getAttribute('role') || 'dialog';
this.useDialog = (
'show' in document.createElement('dialog') &&
this.dialog.nodeName === 'DIALOG'
Expand Down Expand Up @@ -67,7 +68,7 @@
// Despite using a `<dialog>` element, `role="dialog"` is not necessarily
// implied by all screen-readers (yet)
// See: https://github.com/edenspiekermann/a11y-dialog/commit/6ba711a777aed0dbda0719a18a02f742098c64d9#commitcomment-28694166
this.dialog.setAttribute('role', 'dialog');
this.dialog.setAttribute('role', this.role);

if (!this.useDialog) {
if (this.shown) {
Expand Down Expand Up @@ -291,8 +292,9 @@
*/
A11yDialog.prototype._bindKeypress = function(event) {
// If the dialog is shown and the ESCAPE key is being pressed, prevent any
// further effects from the ESCAPE key and hide the dialog
if (this.shown && event.which === ESCAPE_KEY) {
// further effects from the ESCAPE key and hide the dialog, unless its role
// is 'alertdialog', which should be modal
if (this.shown && event.which === ESCAPE_KEY && this.role !== 'alertdialog') {
event.preventDefault();
this.hide();
}
Expand Down

0 comments on commit b5c9030

Please sign in to comment.