Accessible Dialog/Modal
The native HTML <dialog> element makes it easy to create an accessible modal. It automatically manages keyboard navigation and sets focus, eliminating the need for aria-modal adjustments. Plus, you can animate it with CSS or enhance it with JavaScript.
Try it with your Keyboard or Mouse.
What makes a dialog or modal element accessible?
When the modal is open, elements behind it should become inactive.
The focus should shift to an element within the modal upon opening.
When the modal closes, focus should return to the original activating element.
Focus should be trapped within the modal while it is open, preventing users from tabbing to elements outside the modal.
The modal should be closable by pressing the Escape key.
The modal should not be part of the tab order when it is closed.
It’s also important to respect user preferences. For users who prefer reduced motion, adjust animations accordingly. CSS provides a media query for this. Mozilla docs.
A modal should be opened with a <button>, not an <a>. The <button> element is intended for actions, while <a> is for links. Additionally, always set type="button" on a button. Without it, the default type is submit, which is only necessary within forms.
You can also use it as a Menu if it’s only an overlay.