Modal
Modals are implemented using the <dialog> element in combination with the Invoker Commands API, for basic open/close behavior, follow these steps:
- Give your
<dialog>a uniqueidattribute - Add a
aria-labelledbyattribute pointing to the dialog title id andaria-describedbypointing to the message text id. - Create an opening button with
command="show-modal" commandfor="your-modal-id"(alternatively, you can use JS todocument.querySelector('#your-modal-id').showModal()) - The modal will display with a backdrop and trap focus automatically
- Close the modal through the use of a button with
command="close"(alternatively, you can use JS todocument.querySelector('#your-modal-id').close())
Check out the Dialog element documentation for more examples of how to structure your modal content.
Basic Modal
<div style="min-height: 200px;">
<button command="show-modal" commandfor="dialog-modal" class="contrast">Show Modal</button>
<dialog id="dialog-modal" aria-labelledby="modal-title" aria-describedby="modal-description">
<button aria-label="Close" commandfor="dialog-modal" class="icon-close float-end" command="close"></button>
<h3 id="modal-title">📅 Thank You for Registering!</h3>
<br>
<article id="modal-description">
<p>
We're excited to have you join us for our
upcoming event.
</p>
</article>
</dialog>
</div>Remark: When it comes to modals, ARIA dictates that the HTML element should have
aria-modal="true"androle="dialog"attributes, but in the case of the<dialog>element, the browser automatically applies therole="dialog"and if you open the dialog using the Invoker Commands API or the.showModal()method, it automatically appliesaria-modal="true".So you can be sure that if you follow the steps above you have all you need to create an accessible modal dialog.
Dialog alerts
Dialog alerts are modal windows that require user interaction before they can be dismissed. They are often used for critical messages or actions that require confirmation from the user.
- Use
<dialog role="alertdialog">for critical decisions. - Provide
aria-labelledbypointing to the dialog title andaria-describedbypointing to the message text. - Ensure the dialog contains at least one focusable element (e.g., action buttons).
- The user must interact with the dialog to dismiss it; do not close on backdrop click.
Warning: Be mindful that the role
role="alertdialog"tells screen readers to treat the dialog as a high-priority alert, immediately interrupting the current reading to announce its contents, so it's important to consider the use of this role versus simply using a less intrusive Modal.
<div style="min-height: 250px;">
<button command="show-modal" commandfor="dialog-alert" class="contrast">Show Dialog Alert</button>
<dialog id="dialog-alert" role="alertdialog" aria-labelledby="modal-title" aria-describedby="modal-description">
<header>
<h2 id="modal-title">Are you sure?</h2>
<button aria-label="Close" class="icon-close align-self-start" commandfor="dialog-alert" command="close"></button>
</header>
<div id="modal-description">
<p>Are you sure you want to proceed with this action?</p>
</div>
<footer>
<button class="secondary" commandfor="dialog-alert" command="close">No</button>
<button commandfor="dialog-alert" command="close">Yes</button>
</footer>
</dialog>
</div>Remark: When it comes to these types of dialogs that interrupt the user’s workflow, ARIA dictates that the HTML element should have
aria-modal="true"androle="alertdialog"attributes, but in the case of the<dialog>element, the browser automatically applies therole="dialog"so you only need to change the role toalertdialog.