Overview
A modal is a <dialog> card with the nds-modal class. A button with data-modal-target opens it. The modal keeps the card’s parts: a header with a featured icon and a close button, the content, and the actions.
Pick another component when:
Markup
Variants
Every option goes on the nds-modal element. On a lazy modal, write it on the nds-modal inside the <template>. A Status option sets data-status: the feedback chip in the header takes its icon and color from it, as in a status card.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Dialog (default) | — | — | A question or a short task, with a close button and two actions |
| Structure | Header title (hint: The title in the header, over content that scrolls) | canon #modal-header-title |
— | A modal whose content is more than its title and description, of any kind. The title and description sit in the header, as in a Card’s header title, and the content scrolls in a Scroll More. A featured icon before the title is optional; a one-line title centers on it |
| Structure | Scroll more (hint: Long content that fades at its edge, with a Show more button) | canon #modal-scroll-more |
— | A modal whose content runs longer than the screen, such as terms to read. The content scrolls in a Scroll More: its edge fades while more is below, and Show more scrolls on. The header and the actions stay in view |
| Structure | Status (demo: + size-sm) | canon #modal-status |
— | The result of an action, centered, with a status chip and one action. It has no close button. It is a status card in a dialog |
| Structure | Lazy modal | canon #modal-lazy |
— | The modal sits in a <template> and joins the page on the first click. For a large modal that most users never open |
| Size | MD (default) | — | — | 600px wide. For a message with a short form |
| Size | SM (id: size-sm) | .nds-sm |
.nds-modal |
400px wide. For a status or a confirmation with one or two buttons |
| Size | LG | .nds-lg |
.nds-modal |
800px wide. For long content, such as terms of service or a data preview |
| Size | Full | .nds-full |
.nds-modal |
The width of the screen, less 64px on each side. For a large preview, such as a document or an image |
| Status | Success (default) | — | .nds-modal.nds-center |
The action worked |
| Status | Error | [data-status="error"] |
.nds-modal.nds-center |
The action failed |
| Status | Warning | [data-status="warning"] |
.nds-modal.nds-center |
The action worked, with a risk the user should know about |
| Status | Info | [data-status="info"] |
.nds-modal.nds-center |
Neutral news about the action |
| Static | Static (hint: Escape and an overlay click do not close it) | [data-modal-static] |
.nds-modal |
Escape and a click on the overlay do not close the modal. For a choice the user must make |
Behavior
Static
data-modal-static turns off the two ways a user closes a modal by accident: Escape and a click on the overlay. The modal then closes only from a data-modal-close control or a script. Use it when the user must pick one of the actions. Keep a Cancel action in the modal, so the user always has a way out.
Panels Below
A modal opens above any open panel and leaves it open. When the modal closes, the panel is where the user left it.
Lazy Modal
A lazy modal keeps its markup in a <template class="nds-modal-template">, so it adds nothing to the page until the first click. The trigger stays outside the template. On that click, the modal joins the page, and NDS starts the components inside it. Put one modal in each template. Page search and search engines do not see content in a template, so do not use it for content people search for.
Built-in Features
-
Auto-initialization
Triggers and close controls work from their attributes, with no script of your own. A trigger added to the page later works too.
-
Focus Management
Focus moves to the field with
autofocuswhen the modal opens, else to the first close control, or to the modal when it has neither. The modal itself shows no focus ring. The page behind the modal is inert: Tab, a click and a screen reader cannot reach it. Focus goes back to the trigger when it closes. -
Keyboard Control
Escape closes the modal, wherever focus is. When the modal itself has focus, Enter presses the first primary button in
.nds-card-actions. -
Backdrop Overlay
A dimmed overlay covers the page, and a click on it closes the modal. Set
--backdrop-blurto blur the page behind it. -
Body Scroll Lock
The page does not scroll while the modal is open.
-
Pinned Header and Actions
A modal is at most 80% of the screen height. Past that, only its content scrolls, so the header and the actions stay in view.
-
Bottom Sheet on Phones
On a phone, the modal slides up from the bottom, spans the full width and rounds only its top corners. Its action buttons stack, each at full width.
-
Animations
On a larger screen, the modal fades in and out.
-
One Modal at a Time
Opening a modal closes the open one first. The new modal keeps its own settings, such as
data-modal-static. -
Programmatic Control
Open and close a modal from a script, check whether one is open, and listen to its events.
Best Practices
- Use a modal only when the user must act before going on: confirm a submission, accept terms, or approve a delete.
- Do not use a modal for a success message the user does not have to act on. Use an Alert or a toast, which the Alert page also covers.
- Do not open a modal from a modal. Change the flow so one modal holds the decision.
- Write a title that names the action, and keep the content to one message or one task.
- Put the title and description in the header when the modal holds anything more. Keep them in the content when they are the whole message, such as a confirm or a status.
- When the content holds more than the title and description, of any kind, put it in a Scroll More, as the Header title structure does. When the content fits, Scroll More shows nothing. Do not judge by the length you see: on a phone the modal is a bottom sheet, and content can grow after it opens.
NDS.init.audit()flags a modal without it. - With the title in the header, the icon is optional. Leave it out when the title is long, so the title gets the full width of the header.
- Give the modal a clear primary action and a way out, such as Confirm and Cancel.
- Give every modal an
id, and give its trigger thatidindata-modal-target. - Make the modal a
<dialog>. A closed dialog stays hidden, also before the script loads.NDS.init.audit()flags adiv.nds-modal. - Give the modal an
aria-labelledbythat points to its title. Give the close button anaria-label. - Use
role="alertdialog"instead for an urgent message the user did not ask for, such as a session that is about to end. - Give every button
type="button". In a form, a button without it submits the form. - Put
.nds-card-actionsafter.nds-card-content, not inside it. A form’s buttons go there too, never in a.nds-form-actionsinside the content, where they scroll away.NDS.init.audit()flags both. - Keep
nds-col-smon.nds-card-actions. On a phone the modal is a bottom sheet, and the buttons stack in it, each filling the width. - For a form, wrap
.nds-card-contentand.nds-card-actionsin one<form class="nds-form">. A form withoutnds-formbreaks the layout: the content runs out of the modal and the actions leave the screen. It also gets no submit validation.NDS.init.audit()flags it. - A Dropmenu, a select, a Multiselect, an Autocomplete or a Filter works in a modal with no extra attribute. Its menu opens in the browser’s top layer, so the modal’s edge does not cut it off.
- Do not look up a lazy modal when the page loads: it is not in the page yet. Listen on
documentfornds:template:ready, which fires on the modal after it joins the page and its components start.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-modal |
<dialog class="nds-card"> |
Turns the card into a dialog over the page |
nds-md |
.nds-modal |
600px wide, the same as no size class |
nds-modal-close |
a button in the modal | Closes the modal, like data-modal-close, and removes the button’s padding |
nds-modal-template |
<template> |
Holds a lazy modal |
The card parts, colors and nds-center are on the Cards page.
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-modal-target |
any button on the page | Opens the modal whose id is its value |
data-modal-close |
any element in the modal | Closes the modal |
data-modal-static |
.nds-modal |
Escape and a click on the overlay do not close the modal |
data-status |
.nds-modal |
The status color: success, error, warning, info, critical or neutral. See Cards |
data-state |
.nds-modal |
The script writes open while the modal is open and closing while it animates out. Do not set it yourself |
CSS Custom Properties
Set these on :root. They style the overlay behind every modal and modal panel.
| Property | Default | Controls |
|---|---|---|
--backdrop-bg |
var(--background-overlay) |
The overlay color |
--backdrop-blur |
none |
A backdrop-filter for the page behind the overlay, such as blur(4px) |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_modal.scss.
| Token | Preview | Value |
|---|---|---|
--background-overlayRead by background-color | var(--colors-alpha-black-50)Dark mode var(--colors-alpha-black-70) |
JavaScript
The NDS.Modal script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.Modal.open(modal) |
Opens the modal. It takes the element or its id. An open modal closes first. For a lazy modal, pass its id: the element is not in the page yet |
NDS.Modal.close() |
Closes the open modal. It takes no argument |
NDS.Modal.isOpen() |
Returns true while a modal is open |
NDS.Modal.init() |
Starts the trigger and close handlers on document. NDS calls it when the page loads, and a second call does nothing |
NDS.Modal.destroy(root) |
Closes an open modal inside root at once, before you remove its markup. Without it, the page does not scroll. NDS.init.destroy(root) calls it for you |
| Event | Fired on | Detail |
|---|---|---|
nds:modal:opened |
.nds-modal |
None. Fires after the modal shows |
nds:modal:closed |
.nds-modal |
None. Fires after the close animation ends, not on the click |
nds:template:ready |
.nds-modal |
{ id }, when a lazy modal joins the page |
The full API is in the banner of _js/nds-modal.js.