A modal focuses the user’s attention on a single task or message.
Preview
Usage
Modals disable underlying content and are used to present a short-term task the user needs to perform without losing the context of the underlying page. Users won't be able to interact with the page until they close the modal. By design, clicking outside the DtModal dialog does not close it — this is intentional behavior to prevent accidental dismissal of important tasks. Users must explicitly click the close button or trigger a close action to dismiss the modal.
Although highly versatile, this doesn't mean modal dialogs are fit for all purposes. Modals are purposefully disruptive and should be used thoughtfully and sparingly, specifically in moments where focus is required or an action must be taken.
Do
- To complete a simple task or decision that requires their full attention outside the main workflow.
- Confirming a destructive action that is about to happen.
- Ask for a user’s consent for an action.
Don’t
- When its content or features can be part of the page without complicating the page’s intent.
- When the content or message requires interaction with other parts of the application or screen.
- Form-related error, success, or warning messages. Keep feedback in context to forms.
- Confirming an action took place (instead: use a Toast).
- Revealing more information (instead: place content inline)
- Displaying complex forms or large amounts of information (instead: place content inline)
- Displaying content unrelated to current task (instead: place content inline as a Link or Banner).
Best Practices
- Ideally, users trigger the modal, not the system, and should not be a surprise. Its appearance should reflect user intent to invoke it. Uninvited modals may surprise the user and result in a quick dismissal of the window.
- Treat modals as a last resort. Consider whether there’s another component or UI that might be less disruptive for the user.
- Limit the number of interactions in a modal. Remove anything that does not support the task.
- Avoid multiple steps that require navigation within the modal dialog.
- Avoid complex decision-making that requires additional sources of information unavailable in the modal.
- Use clear header and action labels. Label links and buttons with a verb that avoids ambiguity and clearly indicates what happens when it’s selected. The primary action’s label should complement the modal title.
- Avoid lengthy contents that require scrolling.
- Only one modal can be present at a time.
Accessibility
- Opened modals “trap focus,” meaning keyboard navigation controls are constrained to elements within the modal. Tabbing to the modal's last focusable element, and then pressing tab again would loop the focus back to the first element on the page. Focus doesn't return to the underlying page until the user explicitly dismisses the modal, in which case it would return to the place it was before the dialog opened.
- To ensure maximum compatibility, all
atags must have anhrefattribute. Also, any elements which you don't want to be focusable (but might be focusable by default) must have theirtabindexset to-1. - Focus should always begin on the first actionable element within the dialog. This could be an OK button, or the first field in the form. An X button in the top right corner should be last in the tab order even though it may be visually above the other elements.
- Check out the "Focus management" section of the following MDN Dialog document if you'd like to know more.
- Use
aria-labelledbyon its root element to associate a title to the modal to announce it to assistive technology. The value ofaria-labelledbyshould match theidof its heading element (e.g.h2). - Dismissing the modal returns focus to the originating element that spawned the modal’s display.
Variants and Examples
Base Style
Fixed Header and Footer
This is the default behavior that adds the scroll automatically in the modal content and leaves the header and footer fixed.
Danger
A modal style for destructive or irreversible actions.
Full Screen
To make this modal take up as much of the screen as possible.
Has Banner
When there is a need of more context information regarding the content of the Modal
Custom Header and Content
You're not limited to using plain title and copy text.
In addition to the footer, custom elements can be inserted into the header and body sections of the dialog via slots.
Please note: supplied header or body slots will take the place of any provided "title" or "copy" text, respectively.
Vue API
Slots
Props
Events
Classes
At minimum, modals contain a title and one button. They could also contain body text, brand illustrations, product wireframes, or multiple buttons.