Overview
A panel is a <dialog class="nds-panel"> and a button with data-panel-toggle that opens it. The panel holds three optional parts: a header (nds-panel-header), a body (nds-panel-body) and a footer (nds-panel-footer). The panel does not style the content you put in the body.
Pick another component when:
Markup
Variants
Every option goes on the nds-panel element, except Resizable, which has two rows in the header: make both changes. On a lazy panel, write it on the nds-panel inside the <template>.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Panel (default) | — | — | The panel is in the page from the start |
| Structure | Lazy panel | canon #panel-lazy |
— | The panel sits in a <template> and joins the page on the first click. For a large panel that most users never open |
| Resize | Resizable (hint: Buttons make the panel smaller or larger) | canon #panel-resize |
.nds-panel-action |
Minus, plus and close buttons in one group make the panel smaller or larger. For content the user may want to see wider |
| Resize | Resizable (hint: Buttons make the panel smaller or larger) | remove | .nds-panel-action > .nds-btn[data-panel-close] |
The group holds its own close button, so the lone one goes |
| Side | End (default) (hint: Follows the reading direction) | — | — | Slides in from the end edge of the reading direction: the left in Arabic, the right in English |
| Side | Start (hint: Follows the reading direction) | [data-panel-side="start"] |
.nds-panel |
Slides in from the start edge of the reading direction |
| Side | Left (hint: Stays on the left in every language) | [data-panel-side="left"] |
.nds-panel |
Always slides in from the left, in every language |
| Side | Right (hint: Stays on the right in every language) | [data-panel-side="right"] |
.nds-panel |
Always slides in from the right, in every language |
| Side | Top | [data-panel-side="top"] |
.nds-panel |
A full-width sheet below the header |
| Side | Bottom | [data-panel-side="bottom"] |
.nds-panel |
A full-width sheet that rises from the bottom. For actions on a phone |
| Size | MD (default) | — | — | The default size: 420px wide, 60svh high |
| Size | SM | [data-panel-size="sm"] |
.nds-panel |
Three quarters of the default size. For a short list of settings |
| Size | LG | [data-panel-size="lg"] |
.nds-panel |
A third larger than the default |
| Size | XL | [data-panel-size="xl"] |
.nds-panel |
Three quarters larger than the default. For wide content |
| Modal | Modal (hint: Dims the page and keeps focus in the panel) | [data-panel-modal] |
.nds-panel |
Dims the page, stops it from scrolling and keeps focus in the panel. For a panel the user must finish first |
| Static | Static (hint: Escape and a click outside do not close it) | [data-panel-static] |
.nds-panel |
Escape and a click outside do not close the panel. For a panel with a form the user could lose |
| Full width | Full width (hint: Top and bottom sheets) | --panel-content-width: 100% |
.nds-panel:is([data-panel-side="top"], [data-panel-side="bottom"]) |
The sheet’s content spans the full width, not the page’s content width. For a wide table or a row of media |
Behavior
Side
data-panel-side picks the edge the panel slides in from. Start and end follow the reading direction, so an end panel opens on the left in Arabic and on the right in English. Left and right stay on the same edge in every language. A side panel is 420px wide, or the full width of a smaller screen.
Sheet
A top or bottom panel is a sheet: it spans the full width of the screen. The sheet is as tall as its content, up to 60% of the screen, and the body scrolls past that. Its content lines up with the page content at every screen size. Only the two corners that face the page are rounded.
Modal
A panel without data-panel-modal leaves the page usable, so the user can see a filter or a setting change the page. data-panel-modal is for a panel the user must finish first, such as a checkout step. It dims the page behind the panel and stops it from scrolling. Tab and Shift + Tab then stay inside the panel, and a click on the dimmed page closes it.
Static
data-panel-static turns off the two ways a user closes a panel by accident: Escape and a click outside it. The panel closes only from a data-panel-close control or a script. Another panel can still take its place, as Stacking shows.
Stacking
Each kind of panel has its own rule when another panel opens:
- A plain panel replaces an open plain panel. It waits until the old one slides out, so the two never cross.
- A modal panel closes nothing. Its dimmed layer covers the panels under it.
- A panel the user opens over a modal, or over a modal panel, opens as a modal panel too. It covers the one under it, and plain and static panels do not close it.
- A static panel that the page opens with
{ focus: false }waits until no panel is open. A static panel that a toggle or a script opens shows at once, like a plain panel. - When another panel replaces a static panel, the static panel hides. It comes back when the screen has no open panel again.
Only the top panel closes on Escape or a click outside, and only the top modal panel keeps focus. A Modal sits above every panel. While it is open, no panel closes on Escape or a click, and no panel keeps focus.
Resize
A button with data-panel-resize="grow" or "shrink" moves the panel one step along four sizes: sm, md (the default), lg and xl. A side panel changes its width. A sheet changes its height, and a resizable sheet keeps that height even when its content is shorter. The panel never grows past the screen: the plus button gets aria-disabled="true" at the largest size, or sooner when the panel already fills the screen. The minus button gets it at the smallest size. Put the buttons in one nds-btn-group inside nds-panel-action, with the close button last. Put data-panel-size on the panel to start at another size. A --panel-width or --panel-height you set is the md size, and the other sizes scale from it: sm is three quarters of it, lg a third larger and xl three quarters larger.
Lazy Panel
A lazy panel keeps its markup in a <template class="nds-panel-template">, so it adds nothing to the page until the first click. The toggle stays outside the template. On that click, the panel joins the page, and NDS starts the components inside it. Put one panel in each template.
Built-in Features
-
Auto-initialization
Every
.nds-panelon the page starts by itself. The toggle and close buttons work from their attributes, with no script of your own. -
Focus Management
Focus moves to the first close button when the panel opens, or to the panel when it has none, and back when it closes: to the toggle, or to what had focus when a script opened it. A panel destroyed while open, as a framework unmount or React's StrictMode does, sends it back too. Escape and a click outside close the top panel.
-
Panel Stacking
A plain panel replaces the open one after it slides out. A modal panel covers the others and the main navigation, and a static panel comes back when the screen is clear. A panel you open from a modal opens over it.
-
Header-Aware Position
The panel starts below the sticky header, and moves with it as the top bar scrolls in and out. A tall sheet stops at the header. A modal panel covers the header and starts at the top of the screen.
-
Programmatic Control
Open, close and toggle a panel from a script, check whether it is open, and listen to its events.
Best Practices
- Use a panel for content that supports the main task without replacing it: settings, filters, the details of an item, a short form.
- Give every panel an
id, and give its toggle thatidindata-panel-toggle. A panel without anidcannot be opened. - Give the panel an
aria-labelthat names it, and each icon-only button anaria-label. - Make the panel a
<dialog>, with nohiddenattribute. A closed dialog stays hidden before the script loads. Any other element does not open. - Give every button
type="button". In a form, a button without it submits the form. - Prefer the start and end sides, so the panel follows the reading direction. Use left or right only when the panel must stay on one edge in every language.
- Use a bottom sheet for actions on a phone, and a side panel for settings or filters on a large screen.
- Keep the title in
nds-panel-textin the header, with an optionalnds-panel-descriptionbelow it. A Modal with a form or a list puts its title in the header the same way. - Do not look up a lazy panel when the page loads: it is not in the page yet. Listen on
documentfornds:template:ready, which fires on the panel after it joins the page and its components start.
API
Classes
| Class | Element | Effect |
|---|---|---|
nds-panel |
the panel <dialog> |
The sliding surface |
nds-panel-header |
div in .nds-panel |
The top row: an optional featured icon, the text and the header buttons, with a divider below |
nds-panel-text |
div in .nds-panel-header |
Holds the title and the description, and fills the free space in the row. A bare nds-panel-title also fills it |
nds-panel-title |
span in .nds-panel-text |
The panel’s title |
nds-panel-description |
p in .nds-panel-text |
A short line below the title |
nds-panel-action |
div in .nds-panel-header |
Holds the header buttons: the close button, and any beside it, such as a Reset button or the resize group. They sit closer together than the header items |
nds-panel-body |
div in .nds-panel |
The content. It scrolls when the content is taller than the panel |
nds-panel-footer |
div in .nds-panel |
A row of buttons at the bottom, with a divider above. A button that does not fit moves to the next line. It takes the same classes as card actions: nds-end, nds-reverse, nds-col, and nds-col-sm to stack the buttons below 600px |
nds-scroll-more |
div in .nds-panel in place of .nds-panel-body, or on .nds-panel-body itself |
A Scroll More area fills the same space as the body |
nds-panel-template |
<template> |
Holds a lazy panel |
On a phone, the header buttons shrink to 32px.
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-panel-toggle |
any button on the page | Opens or closes the panel whose id is its value. The script sets aria-expanded on it |
data-panel-close |
any element in the panel | Closes the panel. The first one gets focus when the panel opens |
data-panel-resize |
a button in the panel | grow makes the panel one size larger, shrink one size smaller |
data-panel-size |
.nds-panel |
The size: sm, md (the default), lg or xl. The script writes it when the user resizes the panel |
data-panel-side |
.nds-panel |
The edge: end (the default), start, left, right, top or bottom |
data-panel-modal |
.nds-panel |
Opens the panel as a modal dialog. The page behind it, the main navigation too, is dimmed and does not respond, the page does not scroll, and focus stays in the panel |
data-panel-static |
.nds-panel |
Escape and a click outside do not close the panel |
data-state |
.nds-panel |
The script writes open while the panel is open, opening while it slides in and closing while it slides out |
CSS Custom Properties
Set these on the .nds-panel element.
| Property | Default | Controls |
|---|---|---|
--panel-width |
420px |
Width of a start, end, left or right panel, at the md size. The panel is never wider than the screen |
--panel-height |
60svh |
Most height of a top or bottom sheet, at the md size. The sheet also stops at the header |
--panel-content-width |
var(--nds-content-max-width) |
Most width of a sheet’s content. 100% spans the full width, inside the page gutter |
--panel-top |
the bottom of the sticky header, 0 on a modal panel |
Where the panel starts. Any value turns off the header tracking. 0 covers the header too, and needs a --panel-z above the header’s |
--panel-padding |
var(--spacing-lg) |
Space inside the header, the body and the footer |
--panel-gap |
0 |
Space between the header, the body and the footer |
--panel-radius |
0, var(--radius-lg) on a sheet |
Corner radius. A sheet rounds only the two corners that face the page |
--panel-z |
999 |
Stack order of a plain or static panel. The main navigation is 1000. A modal panel is above everything |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_panel.scss.
| Token | Preview | Value |
|---|---|---|
--background-menuRead by background-color | var(--colors-base-white)Dark mode var(--background-default) | |
--background-overlayRead by background-color | var(--colors-alpha-black-50)Dark mode var(--colors-alpha-black-70) | |
--text-defaultRead by color | var(--colors-base-black)Dark mode var(--colors-neutral-200) | |
--text-displayRead by color | var(--colors-neutral-800)Dark mode var(--colors-neutral-100) | |
--text-primary-paragraphRead by color | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--divider-colorRead by border-block-end, border-block-start | var(--colors-alpha-black-10)Dark mode var(--colors-alpha-white-10) |
JavaScript
The NDS.Panel 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.
Each method takes the panel element or its id.
| Method | Effect |
|---|---|
NDS.Panel.open(panel, { focus }) |
Opens the panel, by the Stacking rules. For a lazy panel, pass its id: the element is not in the page yet. Pass { focus: false } when the page opens it on its own, so focus stays where the user is |
NDS.Panel.close(panel) |
Closes the panel |
NDS.Panel.toggle(panel) |
Opens or closes the panel |
NDS.Panel.isOpen(panel) |
Returns true while the panel is open and not closing |
NDS.Panel.init() |
Starts every .nds-panel on the page that has not started yet |
NDS.Panel.create(panel) |
Starts one panel element |
NDS.Panel.destroy(panel) |
Closes the panel at once and removes its listeners |
| Event | Fired on | Detail |
|---|---|---|
nds:panel:opened |
.nds-panel |
{ panel }, after the panel slides in |
nds:panel:closed |
.nds-panel |
{ panel }, after the panel slides out. When one panel replaces another, this fires before the new panel’s opened. A static panel that hides and comes back fires neither event |
nds:panel:resized |
.nds-panel |
{ panel, size }, after a resize button changes the size |
nds:template:ready |
.nds-panel |
{ id }, when a lazy panel joins the page |
The full API is in the banner of _js/nds-panel.js.