Create

Form

Switch

Copy page View as Markdown Open in ChatGPT Open in Claude

A control that allows the user to toggle between checked and not checked.

Usage#

app.component.ts
import { WdsSwitchComponent } from '@wds/angular/switch';

Examples#

Default#

The content of the element is the label.

Airplane Mode

Description#

The description slot adds helper text below the label.

Share across devices Focus is shared across devices, and turns off when you leave the app.

Choice card#

variant="card" turns the switch into a card: a click anywhere on it toggles the switch. wds-change bubbles, so one listener on a container can handle a group of switches.

Sync Keep your settings in sync across all your devices. Push notifications Get notified about new messages.

sync: false, push: true

Disabled#

Disabled Disabled, on

Invalid#

invalid shows the error state and sets aria-invalid.

Accept terms and conditions

Size#

Small Default

Forms#

Integration with Angular forms. Pick the API your app uses.

Signal forms#

WdsSwitchField connects the switch to [formField]. The form controls checked, disabled, invalid and required: set them in the schema (here disabled()), not as attributes.

Marketing emails Receive emails about new products, features, and more. Security emails Receive emails about your account security.Submit

Submitted: —

Template-driven#

WdsSwitchValueAccessor makes ngModel work on wds-switch. Inside a <form> each switch needs a name.

Marketing emails Receive emails about new products, features, and more. Security emails Receive emails about your account security.Submit

Submitted: —

Reactive forms#

The same WdsSwitchValueAccessor works with formControl and formControlName. Disable a control in the FormControl, not with the disabled attribute.

Marketing emails Receive emails about new products, features, and more. Security emails Receive emails about your account security.Submit

Submitted: —

API References#

wds-switch#

An on/off switch. A native <input type="checkbox" role="switch">.

Attributes

AttributeTypeDefaultDescription
sizeWdsSwitchSize'default'Size.
checkedbooleanfalseChecked state. Setting it from code does not fire wds-change.
disabledbooleanfalseDisables the field and excludes it from form submission.
invalidbooleanfalseError state: red border and aria-invalid.
requiredbooleanfalseThe field must be checked for the form to be valid (e.g. accepting terms).
namestring''Field name in the form. Reflected to the attribute, which FormData uses (also when a framework sets it as a property).
valuestring'on'Value submitted with the form when the field is checked.
variantWdsToggleVariant'default'card is a clickable card with a border and the control on the right.

Events

EventDescription
wds-changeCustomEvent<WdsToggleChangeDetail>The user toggled the switch.

Methods

MethodDescription
formResetCallback()
formDisabledCallback(disabled: boolean)
checkValidity()Returns true when the value is valid; otherwise fires invalid on the element.
reportValidity()Like checkValidity(), and shows the browser validation bubble when the value is invalid.
setCustomValidity(message: string)Sets a custom validation error (an empty string clears it), like input.setCustomValidity().

Slots

SlotDescription
(default)Label.
descriptionHelper text below the label (linked with aria-describedby).

CSS parts (::part)

PartDescription
rootThe container (the card in the card variant).
controlThe switch track (native <input>).
thumbThe thumb.
labelThe label.
descriptionThe helper text.

CSS custom properties

PropertyDescription
--wds-switch-trackTrack color when off.
--wds-switch-checkedTrack color when on.
--wds-switch-thumbThumb color when off.
--wds-switch-thumb-checkedThumb color when on.

Accessibility#

Screen reader#

wds-switch renders a native <input type="checkbox" role="switch">, so screen readers announce it as a switch with its on or off state.

The content of the element is a native <label> of the input and the description slot is linked with aria-describedby. Without visible text give the switch an aria-label: it is forwarded to the input.

invalid sets aria-invalid="true" and required sets aria-required="true"; disabled disables the native input.

The switch takes part in forms: when on, its value (default on) is submitted under name.

Keyboard support#

KeyFunction
TabMoves focus to the switch. A disabled switch is skipped.
SpaceToggles the switch.
Footer