Modals
A modal is a pop-up that displays a message on top of other page content. A modal generally forces a user to complete a task or acknowledge a message. It prevents the user from interacting with the page until they close the modal. A modal can cover the whole page or part of it.
Use modals as a last resort. They have usability issues and disrupt the user. Use them only if no other component can do the action required.
Examples of modals:
- A confirmation that a user has filled out a form and is ready to submit it
- A privacy policy
- A notification of a time limit to complete a task
- A system error
When to use
- As an alert or a required consent to process a form
- When the user needs to give more information or clarify information
- To notify a user that they have completed a process
- To tell a user they are running out of time to do a task
When not to use
- For validation: Instead, use a validation component. They are less disruptive. For more information, visit the validation component.
- For confirmation: Instead, create a confirmation page. Or create an alert banner. For more information on alerts, visit the Alert banner component.
- For step-by-step processes or questions: Instead, create individual pages.
- For replacing the main page or landing page of a site: Instead, create an individual page.
Use the <dialog>
element
The <dialog>
element is a built-in modal dialog for HTML5. It offers features for dialog behavior, such as showing, hiding, and modal blocking. The <dialog>
element includes JavaScript APIs .show()
, .showModal()
, and .close()
for displaying and controlling the dialog. This makes it easy to show, hide, and interact with dialog content. Use the appropriate .showModal()
or .show()
method to display dialogs.
When using HTMLDialogElement.showModal()
, the first nested element receives focus.
A .showModal()
method will let users press the Esc
key to close the modal. You do not need to use an ARIA role=”dialog”
if you are using a <dialog>
element.
Alert vs. alertdialog roles
Both roles help specify the type of modal message to users on assistive devices. Use <div>
elements within the <dialog>
to use the appropriate ARIA roles and attributes.
role = “alert”
This is a type of live region for important, immediate messages. This alert doesn’t make the user close the modal. This type of modal should not receive focus. This role has an implicit assertive value and will interrupt a user’s current task.
role= “alertdialog”
This is a type of live region for urgent messages that a user must respond to. Examples of this role include error messages or confirming a destructive action. The tab sequence must not leave the modal. Users shouldn't be able to do anything else until they select an action within the modal.
Use the aria-describedby
attribute to give greater context to the modal's content. The ID of the attribute must match the ID of the element referencing the content. It helps screen readers announce the descriptive content of the modal. The aria-labelledby
attribute gives the modal an accessible name. It does this by connecting to the modal title or heading.
Keyboard accessibility
Make all modal content and controls keyboard accessible. Users should be able to open, close, and navigate the modal using the keyboard alone.
Close buttons
Include an obvious way a user can exit the modal. The .showModal()
method allows a user to use the esc
key to exit. But you should still have a close button or an “X” icon that is keyboard accessible.
Make content and interactions clear and simple. Avoid too much information. Tell the user any issues they need to respond to. Give guidance on how to resolve those issues.
Make the content relevant to the user’s current task or context.
Make the modal easy to control. Make it easy for users to open and close the modal.
Make it clear what caused the modal to appear. Users should understand why the modal has displayed and how to respond.