mt-a11y-dialog is a lightweight yet flexible script to create accessible dialog windows. Forked from edenspiekermann
✔︎ No dependencies
✔︎ Closing dialog on overlay click and ESC
✔︎ Toggling aria-* attributes
✔︎ Trapping and restoring focus
✔︎ Firing events
✔︎ DOM and JS APIs
✔︎ Fast and tiny
Installation
npm install mt-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:
Required Markup
All you require is a trigger, most likely a button since we want to be accessible, and then a script of type "text/template" which contains the contents of the dialog. Your trigger will need a "data-content" attribute which contains the string that the script tag has as its "data-js" attribute.
<button data-js="trigger-newsletter-signup" data-content="newsletter-signup-content">Open the dialog window</button>
<script data-js="newsletter-signup-content" type="text/template">
	HTML CONTENT
</script>
Styling layer
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 dialog itself and its counterpart containers. You can use this to show and hide the dialog:
.dialog[aria-hidden='true'] {
  display: none;
}
The example directory here has a styled modal you can use in your code for inspiration. You also have control over all the css classes used in the dialog as is explained further on.
Show/Hide Effects
To keep this library very light, we only provide a few low level utilities for animations, but with a bit of css you can do whatever you like.
50 milliseconds after the aria-hidden attribute is set to false on the root node a class of a11y-dialog--open is added.
This class is then removed on close, and if the effect type is set to css the aria-hidden attribute is set to true only after the effectSpeed time in milliseconds has passed, allowing whatever css animations you want to apply to run. You just have to synchronize your css animation timings you use with the effect speed.
The fade effect is handled for you in javascript and requires no css.
JavaScript instantiation
const dialog = new A11yDialog({ trigger: '.some-selector or an element node' });
Here are all the options you can pass to the dialog on init, and the defaults that the system uses:
appendTarget: '', 
bodyLock: true, 
closeButtonAriaLabel: 'Close this dialog window', 
closeButtonClasses: 'a11y-dialog__close-button', 
contentClasses: 'a11y-dialog__content', 
effect: 'none', 
effectSpeed: 300, 
effectEasing: 'ease-in-out', 
overlayClasses: 'a11y-dialog__overlay', 
overlayClickCloses: true, 
trigger: null, 
wrapperClasses: 'a11y-dialog', 
JS API
Regarding the JS API, it simply consists of show() and hide() methods on the dialog instance.
dialog.show();
dialog.hide();
For advanced usages, there are create() and destroy() methods. These are responsible for attaching click event listeners to dialog openers and closers. Note that the create() method is automatically called on instantiation so there is no need to call it again directly.
dialog.destroy();
dialog.create();
Events
When shown, hidden and destroyed, the instance will emit certain events. It is possible to subscribe to these with the on() method which will receive the dialog DOM element and the event object (if any).
The event object can be used to know which trigger (opener / closer) has been used in case of a show or hide event.
dialog.on('render', function (dialogEl, event) {
  
  
});
dialog.on('show', function (dialogEl, event) {
  
  
});
dialog.on('hide', function (dialogEl, event) {
  
  
});
dialog.on('destroy', function (dialogEl) {
  
});
dialog.on('create', function (dialogEl) {
  
  
  
  
});
You can unregister these handlers with the off() method.
dialog.on('show', doSomething);
dialog.off('show', doSomething);
Disclaimer & credits
This repository is a fork from edenspiekermann & faction23