Overview
A content switcher shows one view at a time, picked from a short row of joined buttons. It is a tab set with the class nds-content-switcher on its root: an nds-tab-list of nds-tab buttons, and an nds-tab-content area with one nds-tab-panel for each button. The open button has a solid fill.
Pick another component when:
Markup
Every request you submitted, in all states.
Requests that the authority is reviewing. You do not need to do anything yet.
Requests with a decision. Each record shows the decision and its date.
Requests closed more than a year ago, kept for reference.
Variants
The first button is open in the markup: its aria-selected is true, and every other panel has hidden. Keep that state in your markup, so the right panel shows before the script runs. To open another button first, give it aria-selected="true" and tabindex="0", and remove hidden from its panel. Then set the first button and panel like the others.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Size | SM | .nds-sm |
.nds-content-switcher |
32px buttons with a small label, for cards and toolbars |
| Size | MD (default) | — | — | 40px buttons, for page content. It needs no class |
| Size | LG | .nds-lg |
.nds-content-switcher |
48px buttons, for a larger touch target |
| On color | On color | .nds-oncolor |
.nds-content-switcher |
For a switcher on a deep primary or dark background. The open button turns brand primary, as in dark mode. The panel text turns white. Inside an on-color area (data-theme="dark oncolor"), the switcher already takes this look: skip the class |
| Center | Center | .nds-center |
.nds-tab-list |
Centers the row of buttons above the panels |
| Loading | Loading | .nds-loading |
.nds-content-switcher |
Gray bars in place of the labels and the panel content while the data loads. On .nds-tab-list it bars the labels only |
Built-in Features
-
Auto-initialization
Every content switcher on the page starts by itself. Switching and the keyboard need no call.
-
Keyboard Navigation
The arrow keys move between buttons, and left and right follow the reading direction. Home and End jump to the first and the last button. Enter or Space opens the focused button.
-
Three Sizes
32, 40 and 48px buttons. The label size, the padding and the corners follow the size.
-
Loading Skeleton
Gray bars stand in for the labels and the open panel until the script starts, so the row is never bare.
-
High Contrast and Reduced Motion
The open button gets a border in high-contrast mode, and the transitions stop when the user prefers reduced motion.
-
Change Events
Every switch fires
nds:tabs:changewith the new and the previous button and panel. -
Programmatic Control
Open a view, read the open panel or remove the behavior through
NDS.Tabsor the element'sndsTabsproperty.
Best Practices
- Use a content switcher for two to four short views of the same data, such as a date range, a status filter or a chart period.
- Pick it over tabs when people must see the open view at a glance. A solid fill is easier to see than an underline on a busy page.
- Keep labels to one or two words. The row does not scroll, so long labels push it past its container.
- Keep the same buttons at all times. For choices that come and go, use a Filter.
- Put the view people open most first. It opens when the page loads.
- Give every button a panel. A switcher with no panels does not start. For a row of view buttons with no panels, use a button group and handle the clicks yourself.
- Keep the buttons and the panels in the same order and the same number. Button one opens panel one, by their order in the markup.
- Give
nds-tab-listanaria-label, and pair each button and panel witharia-controlsandaria-labelledby.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
aria-selected="true" |
.nds-tab |
Marks the button that opens when the page loads. With none, the first button opens |
data-state="loading" |
.nds-content-switcher or .nds-tab-list |
The same as nds-loading, for a script that already sets states |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--btn-group-radius |
var(--radius-md) |
Corner radius of the two ends of the row, the same at every size. Set it on .nds-tab-list |
--btn-size |
40px |
Button height, for a size between the three sizes. Set it on .nds-tab |
--tab-button-gap |
var(--spacing-xs) |
Gap between a button’s icon and its label |
--tab-button-padding-block |
0 |
Padding above and below a button’s label |
--tab-button-padding-inline |
the button padding | Padding at the start and the end of a button. It follows the size |
--tab-panel-padding |
0 at the sides, var(--spacing-2xl) above and below |
Padding of a panel on both axes. A panel with nds-card keeps var(--spacing-2xl) at the sides |
--tab-panel-padding-inline, --tab-panel-padding-block |
--tab-panel-padding |
Padding of a panel on one axis |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_content-switcher.scss.
| Token | Preview | Value |
|---|---|---|
--text-oncolor-primaryRead by --btn-color, color | var(--colors-base-white) | |
--divider-colorRead by border-inline-start-color | var(--colors-alpha-black-10)Dark mode var(--colors-alpha-white-10) | |
--button-background-neutral-defaultRead by --btn-bg, --btn-bg-hover, --btn-bg-active | var(--colors-neutral-950) | |
--button-background-primary-defaultRead by --btn-bg, --btn-bg-hover, --btn-bg-active | var(--colors-primary-600) | |
--button-background-oncolor-disabledRead by --btn-bg-disabled, --btn-indicator-active | var(--colors-alpha-white-20) | |
--button-background-transparent-hoveredRead by --btn-bg, --btn-bg-hover | var(--colors-alpha-white-20) | |
--button-background-transparent-pressedRead by --btn-bg-active | var(--colors-alpha-white-40) | |
--button-background-transparent-selectedRead by --btn-bg-selected | var(--colors-alpha-white-30) |
JavaScript
The Tabs controller, NDS.Tabs, runs the content switcher. The instance lives on the element as ndsTabs. It is there once the component starts: code that runs at page load waits for NDS.init.ready.
| Method | Effect |
|---|---|
NDS.Tabs.init() |
Starts every switcher and tab set on the page that has not started yet |
NDS.Tabs.create(element) |
Starts one switcher and returns its instance, or the instance it already has. Returns null when the switcher has no tab list, tabs or panels |
NDS.Tabs.destroy(element) |
The same as instance.destroy() |
instance.goTo(index) |
Opens a view by its position, from 0 |
instance.getActiveTabIndex() |
Returns the position of the open button |
instance.getActiveTab(), instance.getActivePanel() |
Return the open button and its panel |
instance.destroy() |
Removes the listeners. The markup stays as it is |
| Event | Fired on | Detail |
|---|---|---|
nds:tabs:change |
.nds-content-switcher, and it bubbles |
tabIndex, tab, panel, previousTab, previousPanel |
The full API is in the banner of _js/nds-tabs.js.