Overview
An accordion is a stack of items. Each item has a heading with a button, and a panel under it that the button opens and closes. The panels open in place and push the content below them down.
Pick another component when:
Markup
An overview of the service and how to begin your application.
The documents and the eligibility criteria you need before you apply.
The fee schedule and the expected processing time for each request.
Variants
The first item is open in the markup: its button has aria-expanded="true" and data-state="open", and its nds-accordion-collapse has data-state="open". Keep all three, so the item shows open before the script runs. To open another item first, move the three to it. To start with all items closed, remove both data-state="open" and set aria-expanded="false". Stroke and Shadow need Card. Minimal and Card do not combine.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Text (default) | — | — | A title on each button. For most lists, such as questions and answers |
| Structure | Leading icons | canon #accordion-icons |
— | An icon before each title, for topics that people find faster by picture. Give every item an icon, or none |
| Size | LG (default) | — | — | 56px headings, for page content. It needs no class |
| Size | MD | .nds-md |
.nds-accordion |
48px headings, for a side column or a card |
| Size | SM | .nds-sm |
.nds-accordion |
40px headings, for a dense list in a panel or a filter |
| Minimal | Minimal | .nds-minimal |
.nds-accordion:not(.nds-card) |
Each heading is a short button in neutral link colors, as wide as its title, with no lines between items and no side padding. The headings take the button sizes: 40px, 32px with MD, 24px with SM. For a short list inside other content, such as a step or a card. Not with Card |
| Card | Card | .nds-card |
.nds-accordion:not(.nds-minimal) |
A card background and rounded corners around the whole list. The first item loses its top line |
| Stroke | Stroke | .nds-stroke |
.nds-accordion.nds-card |
A border around the card |
| Shadow | Shadow | .nds-shadow |
.nds-accordion.nds-card |
A shadow under the card, to lift it off a colored background |
| Always open | Always open (hint: Several items stay open at once) | [data-state~="always-open"] |
.nds-accordion |
Several items stay open at once. Without it, opening one item closes the others |
| Loading | Loading | .nds-loading |
.nds-accordion |
Gray bars in place of the titles, the icons and the open panel while the content loads |
Behavior
One Open Item
By default, one item is open at a time. When a person opens an item, the open one closes, so the list stays short. Pick it for questions and answers, where people read one answer and move on.
When a long item above closes, the page moves up. If the heading of the item that opened is then above the screen, the page scrolls back to it.
Always Open
data-state="always-open" on .nds-accordion lets each item open and close on its own. Items stay open until the person closes them. Pick it when people compare two answers, or keep one panel open while they work in another.
Built-in Features
-
Auto-initialization
Every accordion on the page starts by itself. Opening, closing and the keyboard need no call.
-
Animations
Panels slide open and closed. The motion stops when the user prefers reduced motion, and an item that is open at load does not animate.
-
Keyboard Navigation
The up and down arrows move between the headings and wrap at the ends. Home and End jump to the first and the last heading. Enter or Space opens or closes the focused item.
-
Loading Skeleton
Gray bars stand in for the titles and the open panel until the script starts. In a list that a filter controls, they stay until the filter has applied the choices in the URL.
-
Nested Accordions
A panel can hold another accordion. Each accordion runs its own items and ignores the items of the one inside it.
-
Print
Every panel prints open, so no content is lost on paper.
-
Screen Readers and High Contrast
The script keeps
aria-expandedon each button in step with its panel. In high-contrast mode, the lines between items get stronger and the focus ring gets wider. -
Open and Close Events
An event fires when a panel has finished opening or closing, for analytics or for content that loads on open.
Best Practices
- Use an accordion when people scan the headings and open only what they need: questions and answers, services grouped by topic, settings.
- Keep titles short. People decide to open an item from its title alone.
- Put the item people open most first, and open it at load when most visitors need it.
- Pick the heading level that fits the page outline. The markup above uses
h3, under anh2section title. - Keep one button and one
nds-accordion-collapsein each item. The script pairs them by their order in the markup. - Give each button
aria-controlswith theidof its collapse. - Do not add
role="region",aria-labeloraria-labelledbytonds-accordion-collapse. On a long list, every panel becomes a landmark. On a plaindiv, the two labels are not allowed, so an accessibility audit fails. - Use
nds-minimalfor one or two items inside other content, such as the requirements of a step. On a page section, keep the full-width headings: they are easier to find and to tap. - Do not hide content that people must see to finish a task, such as a required step or a warning.
- After you add items to a live accordion, call
NDS.Accordion.init(). The new items start working, and the open items stay open.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
aria-expanded="true" and data-state="open" |
.nds-accordion-btn |
Opens the item at load. The script reads aria-expanded. data-state turns the arrow before the script runs |
data-state="open" |
.nds-accordion-collapse |
Shows the panel at load, before the script runs. Set it with the two attributes above |
data-state="always-open" |
.nds-accordion |
Several items can stay open at once. Without it, opening one item closes the others. Set it yourself |
data-state="loading" |
.nds-accordion |
The same as nds-loading, for a script that already sets states |
CSS Custom Properties
Set them on .nds-accordion, in its style attribute.
| Property | Default | Controls |
|---|---|---|
--accordion-header-height |
56px, 48px with nds-md, 40px with nds-sm |
Minimum height of each heading button. Not used with nds-minimal |
--accordion-header-padding |
var(--spacing-xl), var(--spacing-lg) with nds-md, var(--spacing-md) with nds-sm |
Padding above and below the title in each heading button. With nds-minimal, padding above and below each panel instead |
--accordion-scroll-offset |
120px |
Space between the main navigation and an opened item’s heading, when the page scrolls back to it |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_accordion.scss.
| Token | Preview | Value |
|---|---|---|
--text-defaultRead by color | var(--colors-base-black)Dark mode var(--colors-neutral-200) | |
--text-primaryRead by border-color | var(--colors-primary-600)Dark mode var(--colors-primary-400) | |
--divider-colorRead by border-top | var(--colors-alpha-black-10)Dark mode var(--colors-alpha-white-10) | |
--button-background-secondary-hoveredRead by --btn-bg-hover | var(--colors-neutral-100)Dark mode var(--colors-neutral-700) | |
--button-background-secondary-pressedRead by --btn-bg-active | var(--colors-neutral-200)Dark mode var(--colors-neutral-600) | |
--button-background-transparent-defaultRead by --btn-bg, --btn-bg-selected | var(--colors-alpha-white-0) | |
--link-neutralRead by --btn-color | var(--colors-neutral-700)Dark mode var(--colors-neutral-200) | |
--link-neutral-hoveredRead by --btn-color-hover | var(--colors-neutral-500)Dark mode var(--colors-neutral-400) | |
--link-neutral-pressedRead by color | var(--colors-neutral-400)Dark mode var(--colors-neutral-500) |
JavaScript
The NDS.Accordion 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.
The instance lives on .nds-accordion as ndsAccordion. It is there once the component starts: code that runs at page load waits for NDS.init.ready. Item positions start at 0.
| Method | Effect |
|---|---|
NDS.Accordion.init() |
Starts every accordion on the page that has not started yet. On an accordion that already started, it picks up the new items |
NDS.Accordion.create(element) |
Starts one accordion and returns its instance, or the instance it already has. Returns null when it has no buttons |
NDS.Accordion.destroy(container) |
The same as instance.destroy(). The open items stay open |
instance.openItem(index), instance.closeItem(index), instance.toggleItem(index) |
Open, close or toggle an item by its position |
instance.openAll() |
Opens every item, also in an accordion without always-open |
instance.closeAll() |
Closes every open item |
instance.getOpenItems() |
Returns the open items, each as { index, button, collapse, isOpen } |
instance.refresh() |
Picks up the items put into this accordion after it started |
instance.destroy() |
Removes the listeners. The markup stays as it is, and the open items stay open |
| Event | Fired on | Detail |
|---|---|---|
nds:accordion:opened |
.nds-accordion, after the panel opens, and it bubbles |
index, button, collapse, accordion |
nds:accordion:closed |
.nds-accordion, after the panel closes, and it bubbles |
index, button, collapse, accordion |
The full API is in the banner of _js/nds-accordion.js.