Overview
A switch turns one setting on or off. The change takes effect at once. It is a checkbox input drawn as a track and a thumb, inside a form field with a label and optional info text. Related switches go in a group under one legend.
Pick another component when:
- the change applies only after the user submits a form: Checkbox
- the user picks one of three or more options: Radio buttons
Markup
Variants
Checked goes on one switch: the builder uses #switch-1 or #switch1, but on a page it goes on the switch you mean.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Single (default) | — | — | One setting on its own |
| Structure | Group | canon #switch-group |
— | Related settings under one legend |
| Size | SM | .nds-sm |
.nds-switch |
36×18px track, 12px thumb. Dense forms and tables |
| Size | MD (default) | — | — | 48×24px track, 16px thumb. Most forms |
| Size | LG | .nds-lg |
.nds-switch |
56×28px track, 20px thumb. Touch-first screens |
| Color | Neutral (demo: + checked) | .nds-neutral |
.nds-switch-toggle |
The on state uses the neutral color, not the primary color. Use it when the setting is not a brand action. The color shows only when the switch is on, so the demo also turns on Checked |
| Checked | Checked (id: checked) | [checked] |
#switch-1 |
The switch is on when the page loads |
| Checked | Checked (id: checked) | [checked] |
#switch1 |
The same, on the first switch of a group |
| Validation (any) | Required (hint: Press Validate with every switch off) | [data-required] |
.nds-switch-group |
At least one switch must be on before the form submits. Group only |
| Validation (any) | At least 2 (hint: Press Validate with one switch on) | [data-min-checked="2"] |
.nds-switch-group |
At least 2 switches must be on. Group only. Say the rule in the legend |
| Validation (any) | At most 2 (hint: Turn on all three switches, then press Validate) | [data-max-checked="2"] |
.nds-switch-group |
No more than 2 switches may be on. Group only. Say the rule in the legend |
| Disabled | Disabled | [disabled] |
.nds-switch-input |
The user cannot change the setting now |
| Label first | Label first (hint: The label before the switch. Single only) | .nds-reverse |
.nds-switch:not(.nds-switch-group > *) |
The label before the switch. At the end of a line, such as a section action, the switch stays in place when the label changes. Single only: a switch in a group ignores it |
| Stacked | Stacked (hint: The label above the switch. Single only) | .nds-stacked |
.nds-switch:not(.nds-switch-group > *) |
The label above the switch, not beside it. For a narrow column, such as a page header action. Single only: a switch in a group ignores it |
| Layout | Row | .nds-horizontal |
.nds-switch-group |
Group only. The switches sit side by side and wrap |
| Field states | Label, info, feedback, required | — | — | Shared by every form field. See Forms |
Built-in Features
-
Form Integration
Every switch starts on its own. No init call is needed.
-
Toggle Input
A click on the track, on a label inside the control or on the label in the field header toggles the switch. Space and Enter toggle it from the keyboard. Every change of the input, including
NDS.setValue(), checks the field again and firesnds:switch:change. -
Disabled Sync
disabledon the input anddata-state="disabled"on the container stay in sync, in both directions. -
Visual Feedback
A ripple grows around the track on hover. The thumb widens while the track is pressed, and slides to the other end when the switch toggles.
-
Mobile Spacing
On phones, the gap between a switch and its label is smaller, and so is the gap between rows in a group.
Best Practices
- Write the label as the setting, not the action: “Email alerts”, not “Turn on email alerts”.
- Apply the change as soon as the switch moves. Do not add a Save button for a switch.
- Use
nds-smin dense forms and tables, andnds-lgon touch-first screens. - Use
nds-neutralwhen the setting is not a brand action. - Use
nds-reversefor a switch at the end of a line, such as a section action, so the switch stays in place when its label changes.NDS.init.audit()flags one without it. - For the label, info text, feedback and the required mark, see Forms. They work the same on every field.
- If the change fails, turn the switch back and show feedback on the field.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-state~="disabled" |
.nds-switch |
Not-allowed cursor and muted track and thumb. Also disables the input |
data-state~="disabled" |
.nds-switch-group |
Disables every input in the group, and turns off pointer events. The group’s title takes the disabled color |
data-required |
.nds-switch-group |
At least one switch must be on before the form submits. It adds the required mark to the legend |
data-min-checked, data-max-checked |
.nds-switch-group |
The fewest and the most switches that may be on. The same rules as a checkbox group |
data-error-message |
.nds-switch-group |
Replaces the message when no switch is on. The data-min-checked and data-max-checked messages stay |
data-feedback-target |
.nds-form-header |
Receives the feedback message of the field |
CSS Custom Properties
Set these on .nds-switch. The size classes set the first five.
| Property | Default | Controls |
|---|---|---|
--switch-width |
48px |
Track width |
--switch-height |
24px |
Track height |
--switch-thumb-size |
16px |
Thumb diameter |
--switch-thumb-offset |
4px |
Gap between the thumb and the track edge |
--nds-input-size |
20px |
Label line height |
--switch-track-bg |
var(--background-default) |
Track background when off |
--switch-track-border |
var(--switch-neutral-default) |
Track border when off |
--switch-thumb-bg |
var(--switch-neutral-default) |
Thumb color when off |
Tokens
Set a token at :root, or on a wrapper to reach every switch inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the switch group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--switch-primary-checked | var(--controls-primary-checked) | |
--switch-primary-hovered | var(--controls-primary-hovered)Dark mode var(--colors-primary-700) | |
--switch-primary-pressed | var(--controls-primary-pressed)Dark mode var(--colors-primary-800) | |
--switch-neutral-default | var(--colors-neutral-950)Dark mode var(--colors-neutral-400) | |
--switch-neutral-checked | var(--controls-neutral-checked)Dark mode var(--colors-neutral-600) | |
--switch-neutral-hovered | var(--controls-neutral-hovered)Dark mode var(--colors-neutral-400) | |
--switch-neutral-pressed | var(--controls-neutral-pressed)Dark mode var(--colors-neutral-400) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_switch.scss.
| Token | Preview | Value |
|---|---|---|
--background-defaultRead by --switch-track-bg | var(--colors-base-white)Dark mode var(--colors-neutral-800) | |
--background-surface-oncolorRead by --switch-thumb-bg | var(--colors-base-white) | |
--background-primary | var(--colors-primary-600) | |
--focus-outlineRead by outline | var(--colors-base-black)Dark mode var(--colors-base-white) | |
--focus-innerRead by box-shadow | var(--colors-base-white)Dark mode var(--colors-base-black) | |
--controls-neutral-hoveredRead by --switch-track-border, --switch-thumb-bg | var(--colors-neutral-600)Dark mode var(--colors-neutral-400) | |
--controls-neutral-pressedRead by --switch-track-border, --switch-thumb-bg | var(--colors-neutral-600)Dark mode var(--colors-neutral-400) | |
--controls-ripple-effectRead by box-shadow, background-color | var(--colors-neutral-200)Dark mode var(--colors-alpha-black-50) | |
--controls-disabledRead by --switch-track-border, --switch-thumb-bg, --switch-track-bg | var(--colors-neutral-400)Dark mode var(--colors-alpha-white-30) | |
--controls-icon-disabledRead by --switch-thumb-bg | var(--colors-base-white)Dark mode var(--colors-neutral-400) |
JavaScript
| Method | Effect |
|---|---|
NDS.Switch.getValue(el) |
Returns true when the switch is on and false when it is off, or '' when the switch is not found. el is the .nds-switch, its input, its id or a selector |
NDS.Switch.setValue(el, value) |
Turns the switch on or off as a user does, and returns true. It returns false, and changes nothing, when value is not true or false |
NDS.Switch.clear(el) |
Turns the switch off |
| Event | Fired on | Detail |
|---|---|---|
nds:switch:change |
.nds-switch-toggle (bubbles) |
{ checked, value, input } |
The full API is in the banner of _js/nds-switch.js.