Van11y accessible modal window system, using ARIA
This script will provide you an accessible modal window using ARIA.
The demo is here: https://van11y.net/downloads/modal/demo/index.html
Website is here: https://van11y.net/accessible-modal/
La page existe aussi en français : https://van11y.net/fr/modale-accessible/
How it works
Basically, when you activate one:
- the script wraps all the page into a
div id="js-modal-page"
; - adds the
noscroll
class on thebody
element (to remove scroll with CSS if needed); - then inserts a
dialog
element at the end of your page; - puts the focus into it and traps focus in the modal window;
- When you exit it, the focus is given back to the element that opened it.
You can close it using Esc, or by using Enter or Space if you’re on the close button.
Mouse users can click outside the modal window to close it (this option can be disabled if needed).
If you never activate a modal window, it won’t be anywhere in the code.
How to use it
Download the script
You may use npm command: npm i van11y-accessible-modal-window-aria
.
You may also use bower: bower install van11y-accessible-modal-window-aria
.
Option and attributes
First, put class="js-modal"
on a button or a link to activate the script. Then, here are all attributes:
- Attribute
data-modal-prefix-class
: will namespace all the modal CSS element classnames. - Attribute
data-modal-text
: the text of your modal window (will be put into ap
tag). - Attribute
data-modal-content-id
: theid
of (hidden) content in your page that will be put into your modal window (ifdata-modal-text
is not present). - Attribute
data-modal-title
: the main title of the modal window. - Attribute
data-modal-close-text
: the text of the close button in your modal window. - Attribute
data-modal-close-title
: the title attribute of the close button in your modal window. - Attribute
data-modal-background-click="disabled"
: disable the possibility to click outside the modal window to close it. - Attribute
data-modal-close-img
: a path to a valid image for the close button. - Attribute
data-modal-focus-toid
: theid
of the element in the modal you want to give the focus to, when loading the modal (closing button if not specified). - Attribute
data-modal-describedby-id
: addsaria-describedby=<the value of this attribute>
to thedialog
tag.
If you need to close it, add class="js-modal-close"
on an element in the modal content, it will trigger a click on close button.
Remember there are some demos, it will be easier to understand: Demo of accessible modal window
The script is launched when the page is loaded. If you need to execute it on AJAX-inserted content, you may use for example on <div id="newContent">your modal launcher source</div>
:
van11yAccessibleModalWindowAria(document.getElementById('newContent')[, addListeners]);
addListeners
is a facultative boolean (by default set to true
) to add modal listeners (should be set up only the first time in most of the cases).
Minimal styling classes
Here is the minimal set of styles needed to make it work (without data-modal-prefix-class attribute):
/* needed for old browsers *//* removes scroll when modal is opened *//* overlay covers everything *//* modal */
Styling classes example
Here are the styles (unprefixed) used for the demo, I’ve used data-modal-prefix-class="simple"
and data-modal-prefix-class="simple-animated"
to namespace elements, so each one will start with .simple-
/.simple-animated-
:
/* removes scroll when modal is opened *//* overlay covers everything */ @ 100% }/* modal */ @ 100% } /* it can be easily adapted in media-queries for tablets/mobile */ /* for this example: tablets */{ } /* for this example: mobile */{ }
Other style example
Here are the styles (unprefixed) used for the third example of the demo, I’ve used data-modal-prefix-class="simple-left"
to namespace elements, so each one will start with .simple-left-
:
/* needed for old browsers *//* removes scroll when modal is opened */ /* another modal styling example *//* tooltip modal for it’s easy button */