This repo is a Custom Element for creating accessible dialogs/modals It is heavily inspired by Polymer's paper-dialog and the A11y Dialog fork from Edenspiekermann.
Click the following buttons to see different types of modals
NOTE: the auto positioning will only work reliably
on a dialog with fixed height & width. It will also function differently based on the
existance of the arrow-direction
attribute.
The following dialog will always fly out to the side of the screen with the most space
The following dialog will always such that the arrow is pointing at the center of the passed element (in this case the button)
This dialog will be automatically positioned relative to the button.
NOTE: This will only work on a dialog with fixed width
This is a simple dialog! Look at me go!
This is a simple modal! I stole all the focus yeah!
This is a dialog with another dialog inside! Click below to open the nested dialog
This is a nested dialog! Here I am!
This is a modal with another dialog inside! Click below to open the nested dialog
This is a nested dialog! Here I am!
This dialog should have an arrow pointing left. Note that positioning is up to the containing element.
This dialog will be automatically positioned relative to the button.
NOTE: This will only work on a dialog with fixed width
There are no non-native dependencies in this Web Component. If your browser supports the following two things you are good. If not you will need to make sure you load the Polyfills first
Because this is a Web Component you can instantiate it using one of two methods:
HTML
<dialog-el>
<!-- Your dialog/modal content -->
</dialog-el>
JS
var dialogEl = document.createElement('dialog-el');
document.body.appendChild(dialogEl);
dialog.show
This is the means of displaying the dialog/modal. Simply select the element and call this method to display the dialog/modal.
Example:
var dialog = document.querySelector('dialog-el');
dialog.show();
This function returns a Promise that you can use to perform operations after the dialog/modal has displayed.
NOTE: The show
function, if called on an already open dialog, will throw an error
dialog.close
This is the means of closing an open dialog/modal. Simply select the element and call this method to close it.
Example:
var dialog = document.querySelector('dialog-el');
dialog.close();
This function returns a Promise that you can use to perform operations after the dialog/modal has closed.
NOTE: The close
function, if called on an already closed dialog, will throw an error
modal
If set the dialog will render as a fixed position modal instead of an absolute positioned div.
arrow-direction
Based on the value of this property (left, right, top, bottom), it sets an arrow pointer on the side of the dialog. For example, arrow-direction="left"
adds an arrow to the left side of the dialog. Note that this does not work with the modal
property.
dialog-opened
Fired whenever the dialog is opened.
dialog-closed
Fired whenever the dialog is closed.
Use these to override default styles
Property Name | Description | Default Value |
--dialog-el-padding |
Used as the padding for the dialog | 15px |
--dialog-el-background |
Used for the background property of the dialog |
#FFFFFF |
These defaults effectively wrap your provided local DOM in a 15px white border. Override them to change the appearance.