A11y Dialog
This repository is a fork from accessible-modal-dialog by Greg Kraus. We at Edenspiekermann are big fans of the original version, although we discovered we could improve it and make it even better. On top of that, the original script depends on jQuery, which happened to be a problem for us.
The original repository being apparently unmaintained, we decided to fork it and release our own version of the accessible modal dialog. All credits to the original author.
You can try the live demo.
What’s new in Edenspiekermann’s version?
- No more dependency (not even jQuery);
- Possibility to have several different dialog windows on the page;
- DOM API for dialog openers (
data-a11y-dialog-show="dialog-id"
) and closers (data-a11y-dialog-hide
); - JS API to manually show and hide dialog windows as well as knowing their status (
dialog.show()
, dialog.hide()
, dialog.shown
); - Addition of
[tabindex]:not([value="-1"])
to focusable elements; - No more
display
manipulation in JS, the hiding mechanism is entirely up to the CSS layer (using [aria-hidden]
selectors); - Full test coverage with CasperJS and CodeShip;
- Cleaner code resulting in only 650 bytes (0.65Kb!) once gzipped.
Note: the script should run seamlessly in Internet Explorer 9 and above.
Install
npm install a11y-dialog --save
Or you could also copy/paste the script in your project directly, but you will be disconnected from this repository, making it hard for your to get updates.
Usage
You will find a concrete demo in the example folder of this repository, but basically here is the gist:
HTML
Here is the basic markup, which can be enhanced. Pay extra attention to the comments.
<main id="main" aria-label="Content">
</main>
<div id="my-accessible-dialog" aria-hidden="true">
<div tabindex="-1" data-a11y-dialog-hide></div>
<div role="dialog">
<div role="document">
<button type="button" data-a11y-dialog-hide aria-label="Close this dialog window">
×
</button>
</div>
</div>
</div>
CSS
You will have to implement some styles for the dialog to “work” (visually speaking). The script itself does not take care of any styling whatsoever, not even the display
property. It basically mostly toggles the aria-hidden
attribute on the main element and the dialog itself. You can use this to show and hide the dialog:
.dialog[aria-hidden="true"] {
display: none;
}
JavaScript
var dialogEl = document.getElementById('my-awesome-dialog');
var dialog = new A11yDialog(dialogEl);
The script assumes the main document of the page has a main
id. If it is not the case, you can pass the main node as second argument to the A11yDialog
constructor:
var dialog = new A11yDialog(dialogEl, mainEl);
Toggling the dialog window
There are 2 ways of toggling the dialog. Either through the DOM API, or directly with JavaScript. Both ways are inter-operable so feel free to use both if you need it.
The following button will open the dialog with the my-awesome-dialog
id when interacted with.
<button type="button" data-a11y-dialog-show="my-awesome-dialog">Open the dialog</button>
The following button will close the dialog in which it lives when interacted with.
<button type="button" data-a11y-dialog-hide aria-label="Close the dialog">×</button>
The following button will close the dialog with the my-awesome-dialog
id when interacted with. Given that the only focusable elements when the dialog is open are the focusable children of the dialog itself, it seems rather unlikely that you will ever need this but in case you do, well you can.
<button type="button" data-a11y-dialog-hide="my-awesome-dialog" aria-label="Close the dialog">×</button>
Regarding the JS API, it simply consists on show()
and hide()
methods on the dialog instance.
dialog.show();
dialog.hide();
Tests
CasperJS is being used to run browser tests. CasperJS has some dependencies that have to be installed manually. Be sure to satisfy them before running the tests.
npm test
Deploy example
The example page is deployed through GitHub Pages.
npm run deploy