Skip to main content
Login

Sign in to access your account

Switch - National Design System

A switch turns one setting on or off, and the change applies at once

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

Options
Structure
Size
Validation
More
Preview
<div class="nds-form-container nds-switch"> <div class="nds-form-header" data-feedback-target> <label for="switch-1"> <span class="nds-label">Enable notifications</span> <span class="nds-info">Receive push notifications for important updates</span> </label> </div> <div class="nds-form-control"> <div class="nds-switch-toggle"> <input type="checkbox" id="switch-1" name="notifications" value="enabled" class="nds-switch-input"> <div class="nds-switch-track"> <div class="nds-switch-thumb"></div> </div> </div> </div> </div>

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 fires nds:switch:change.

  • Disabled Sync

    disabled on the input and data-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-sm in dense forms and tables, and nds-lg on touch-first screens.
  • Use nds-neutral when the setting is not a brand action.
  • Use nds-reverse for 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.

TokenPreviewValue
--switch-primary-checkedvar(--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.

TokenPreviewValue
--background-default
Read by --switch-track-bg

var(--colors-base-white)
Dark mode var(--colors-neutral-800)
--background-surface-oncolor
Read by --switch-thumb-bg
var(--colors-base-white)
--background-primaryvar(--colors-primary-600)
--focus-outline
Read by outline

var(--colors-base-black)
Dark mode var(--colors-base-white)
--focus-inner
Read by box-shadow

var(--colors-base-white)
Dark mode var(--colors-base-black)
--controls-neutral-hovered
Read by --switch-track-border, --switch-thumb-bg

var(--colors-neutral-600)
Dark mode var(--colors-neutral-400)
--controls-neutral-pressed
Read by --switch-track-border, --switch-thumb-bg

var(--colors-neutral-600)
Dark mode var(--colors-neutral-400)
--controls-ripple-effect
Read by box-shadow, background-color

var(--colors-neutral-200)
Dark mode var(--colors-alpha-black-50)
--controls-disabled
Read by --switch-track-border, --switch-thumb-bg, --switch-track-bg

var(--colors-neutral-400)
Dark mode var(--colors-alpha-white-30)
--controls-icon-disabled
Read 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.

document.querySelector('.nds-switch-toggle').addEventListener('nds:switch:change', function (e) { console.log(e.detail.checked, e.detail.value); }); var input = document.querySelector('.nds-switch-input'); input.checked = !input.checked; input.dispatchEvent(new Event('change'));
Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks