Skip to content
Slotted

Switch

Turns a setting on or off, takes effect at once, and can hold its place while the change is saved.

Email the buyer when a product falls below its reorder point.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { Switch, SwitchDefaultsProvider } from '@slotted/react';

Label and description

The content of the switch is its label: a label element that names the control, so a press on the text toggles the switch too. A description sits under the label, outside it, and joins the control’s accessible description; a press on it changes nothing. labelPlacement="start" puts the label first and the pill at the end of the row, the layout of a settings page; the default, end, puts the pill first.

Paid invoices arrive as PDF attachments.

Unpaid invoices are flagged on the 5th day.

Sizes

size sets the pill, the thumb and the label to one step: sm for dense tables and toolbars, md for most forms, lg for sparse forms. Inside a field the switch takes the field’s size, so one prop sizes the label and the control together.

Indicator

The thumb draws a check when the switch is on, so the state reads without relying on color or on the thumb’s position. indicator="none" removes the check; use it only where text beside the switch states the value, as the description does here. Press the second switch to change the store’s status.

Open: customers can place orders.

States

readOnly keeps the switch focusable and submitted but refuses every press, and sets aria-readonly: use it for a value the reader may see but not change, such as one set by a policy. disabled sets the native disabled, which takes the switch out of the tab order and out of form submission. A disabled switch keeps its position, so a locked setting still shows which way it is set.

Your organization's security policy sets this.

Included in the Business plan.

Available on the Enterprise plan.

Controlled

Pass checked to own the state. A press calls onCheckedChange in React and emits checkedChange in Angular, and the switch moves only when you pass the new value back. Here the first switch pauses the alerts below it, which read disabled from the same state.

Turns off the alerts below until you resume.

Saving a change

A switch that saves at the press shows the asked value at once and returns to the stored one when the write fails. Set loading instead when the server decides the value or when the value cannot be asked again: the switch then holds the stored value and moves when the answer arrives. While loading, the thumb becomes the morphing shape of Loader, the switch keeps its size and refuses a second press, and the control carries aria-busy. loadingDelay holds the shape back for that many milliseconds, so a write that answers sooner draws nothing; the press is refused and aria-busy is set from the start. A screen reader hears messages.loading only if the wait passes two seconds. Press the switch: the save takes 2.5 seconds.

Imports new Shopify orders every five minutes.

Inside a field

A switch inside a field takes the field’s identifier, its description, its error and its states with no wiring. The field label names the setting, and the switch label states what turning it on does. invalid on the field sets aria-invalid on the switch and draws its edge in the danger tone.

Customers see the restock date at checkout.

Error:Add a Nevada tax ID before collecting tax there.

Forms

A switch is a checkbox with the switch role, so it submits like one: its name with the value on when it is on, and nothing when it is off. A form reset returns each switch to its starting state. In Angular, formControlName and ngModel bind a boolean on the switch. Save the form, change a switch, then reset it.

Settings list

A settings card lists one switch per row with the label first, so every pill ends on the same edge however long the labels are. A defaults provider sets labelPlacement, size and indicator once for every switch under it: SwitchDefaultsProvider in React, provideSwitchDefaults in Angular. A switch that states a value of its own keeps it.

Warehouse notifications

Sent to ops@northwind.example.

When a product falls below its reorder point.


When a carrier misses its pickup window.


When a count differs from the system by more than 2%.

Accessibility

  • The pattern is the Switch of the WAI-ARIA Authoring Practices, built as the third of its reference implementations: an input[type=checkbox] with role="switch". The platform supplies the rest: Space toggles it, the label names it and toggles it, name and value are submitted, form.reset() restores it, and a disabled fieldset disables it.
  • The switch writes no aria-checked. The Authoring Practices state that a switch built on a checkbox uses the HTML checked state instead, and writing both would state the value twice in two places that can disagree.
  • Enter does not toggle the switch. The Authoring Practices list Enter as optional, and inside a form Enter submits the form, which a switch bound to Enter would prevent. A reader who expects Enter presses Space.
  • The root carries no role, because ARIA makes the children of a switch presentational: the label and the description would otherwise join the control’s accessible name. The name of a switch does not change with its state, so the pending label is a live region and not part of the name.
  • A required field sets aria-required, never the native required, which would turn on browser constraint validation and change how the form submits; a consumer who wants the native attribute writes required and it reaches the control. disabled is the native attribute. Read-only is aria-readonly plus a refused press, because the native readonly attribute does nothing on a checkbox.
  • The target is at least 24 CSS pixels in both axes at every size and every density, as WCAG 2.2 success criterion 2.5.8 requires. The label supplies the height when there is one, and a switch without a label sets the minimum on its own row.
  • The description sits outside the label, so it describes the switch and does not name it, and a press on it changes nothing.

API

Switch

Props

checkedboolean
The state, owned by the consumer for the life of the component when it is set. Unset, the element owns the state and a native form reset restores it.
defaultCheckedbooleanDefault false
The state an uncontrolled switch starts from, read once and written to the element as the native attribute.
childrenReactNode
The label, beside the pill. It renders a label element pointing at the control through for, so it names the switch and a press on it toggles the switch.
descriptionReactNode
Help text under the label and outside it. It reaches the control through aria-describedby, so it describes the switch and does not name it.
size'sm' | 'md' | 'lg'Default md
On Switch, SwitchDefaultsProviderThe control step. A field around the switch decides it first, then the defaults provider. The pill, the thumb, the check and the label read the control scales, which the theme restates per density.
indicator'mark' | 'none'Default mark
On Switch, SwitchDefaultsProviderWhat the thumb carries. mark draws a check when the switch is on and nothing when it is off. none draws nothing, and is correct only where text beside the switch states the value.
labelPlacement'end' | 'start'Default end
On Switch, SwitchDefaultsProviderWhich side of the label the pill sits on. start is the settings row: the label takes the free width and the pill sits at the end of the row.
variant'plain' | 'control'Default plain
On Switch, SwitchDefaultsProvidercontrol stands the switch in a box one control tall, so in a row of fields aligned to their end the pill sits on the line of their controls. --slotted-switch-control-block-size names another step than the switch size
loadingbooleanDefault false
The value is not settled yet. The switch refuses every press, keeps its size, sets aria-busy, draws the morphing shape of Loader in the place of the thumb, and announces the pending label politely once the wait passes two seconds.
loadingDelaynumberDefault 0
On Switch, SwitchDefaultsProviderMilliseconds before the shape appears, so a write that answers sooner draws nothing. The switch refuses a press and sets aria-busy from the start of the wait, and keeps its mark and its cursor until the delay has passed.
messagesPartial<SwitchMessages>
Words that replace the active language’s, key by key: loading is what a screen reader hears while the switch is pending. The words reach a live region, not the control’s name, so the name never changes with the state.
disabledbooleanDefault from the field, or false
Unset rather than false, so the switch follows the field around it. A stated value wins. It is the native attribute, which takes the switch out of the tab order and out of form submission.
readOnlybooleanDefault from the field, or false
Unset rather than false. The native readonly attribute does nothing on a checkbox, so the switch sets aria-readonly and refuses every press. It stays focusable, announced and submitted.
invalidbooleanDefault from the field, or false
Unset rather than false, so the switch follows the field around it. Writes aria-invalid on the control and data-invalid on the root, which draws the edge and the focus ring in the danger tone.
requiredboolean
The element’s own attribute, passed through unchanged; the switch never writes it. A required field sets aria-required instead, because the native attribute turns on browser constraint validation.
name / value / formstringDefault — / on / —
The element’s own attributes. The form submits name with value when the switch is on, and nothing when it is off, as it does for a checkbox.

Events

NamePayloadWhen
onCheckedChange(checked: boolean, request: ChangeRequest<boolean>)Before the switch moves. request.value is the new state and request.previous the state it replaces. request.reason is "pointer" for a press, "keyboard" for Space or "programmatic" for element.click(). request.source is the originating event, and request.cancel() refuses the move before anything commits.
onClick, onChange, onFocus, onBlurthe element’s own eventsWhenever the element fires them. The switch runs a consumer’s handler before its own, and a click handler that calls preventDefault() refuses the move before onCheckedChange is called.

Composition

render(props: SwitchRootProps) => ReactNode
Replaces the root element and hands it every attribute. The props are typed against HTMLAttributes<HTMLElement>, so they can be spread onto a list item or a table cell. The control, the pill and the label are fixed: the control holds the state and the identifier, and the other parts are the switch’s own drawing.
refRef<HTMLInputElement>
Reaches the control. focus() and click() are the element’s own, and a click() reaches onCheckedChange with the reason "programmatic".
className / stylestring / CSSProperties
Reach the root, the element a consumer styles. Every other native prop reaches the control.
SwitchDefaultsProvider{ size, indicator, labelPlacement, loadingDelay, variant }
On a subtreeSets size, indicator and labelPlacement once for every switch under it. A field outranks the provider’s size, so a switch inside a field takes the field’s step.
useField()() => FieldContextValue | undefined
On inside a FieldWhat the switch reads from a field around it: the identifier, the description, and the disabled, read-only, invalid and required states. The switch needs no wiring inside a field.

Parts

PartElementRequiredPurpose
rootspan, or the element render returnsYesThe box. It carries data-ui="switch", the three axes, and data-checked, data-disabled, data-readonly, data-invalid and data-loading. It has no role: ARIA makes the children of a switch presentational, so a root carrying the role would fold the label and the description into the control’s accessible name. className and style reach this element.
controlinput[type=checkbox][role=switch]YesThe control. It holds the state, the identifier and every native attribute a consumer writes, such as name, value, form, required, autoFocus and tabIndex. It is transparent and lies over the pill, so a press on the pill is a press on the control. ref reaches this element.
trackspanYesThe pill. It paints the ground, the edge and the focus ring, and it is the box the thumb travels inside.
thumbspanYesThe part that moves, and the ground the check is drawn on. It travels on the inline axis, so a right-to-left document mirrors the movement.
labellabelNoThe label, from children. It points at the control through for, so it names the switch and a press on it toggles the switch. A switch written without children takes its name from aria-label or from the field’s label.
descriptionpNoHelp text under the label, from description. It sits outside the label, so it does not join the accessible name and a press on it changes nothing. Its identifier reaches the control through aria-describedby.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every switch inside it.

Switch tokens

TokenDefault
--slotted-switch-active-backgroundvar(--slotted-tone-secondary-solid-active, ButtonFace)
--slotted-switch-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-switch-checked-active-backgroundvar(--slotted-tone-accent-solid-active, Highlight)
--slotted-switch-checked-backgroundvar(--slotted-tone-accent-solid, Highlight)
--slotted-switch-checked-border-colorvar(--slotted-tone-accent-solid, Highlight)
--slotted-switch-checked-hover-backgroundvar(--slotted-tone-accent-solid-hover, Highlight)
--slotted-switch-checked-thumb-border-colorvar(--slotted-tone-accent-solid, Highlight)
--slotted-switch-colorvar(--slotted-text, CanvasText)
--slotted-switch-control-block-sizevar(--_control-height)
--slotted-switch-description-colorvar(--slotted-text-muted, GrayText)
--slotted-switch-description-font-sizevar(--slotted-font-size-xs, 0.75rem)
--slotted-switch-disabled-backgroundvar(--slotted-disabled-background, ButtonFace)
--slotted-switch-disabled-border-colorvar(--slotted-disabled-border)
--slotted-switch-disabled-colorvar(--slotted-disabled-foreground, GrayText)
--slotted-switch-disabled-mark-colorvar(--slotted-disabled-background)
--slotted-switch-disabled-thumb-backgroundvar(--slotted-disabled-foreground)
--slotted-switch-hover-backgroundvar(--slotted-tone-secondary-solid-hover, ButtonFace)
--slotted-switch-invalid-border-colorvar(--slotted-tone-danger-border)
--slotted-switch-invalid-edge-widthvar(--slotted-control-invalid-edge-width, 1px)
--slotted-switch-invalid-focus-ring-colorvar(--slotted-focus-ring-color-invalid, Highlight)
--slotted-switch-line-heightvar(--slotted-line-height-md, 1.5)
--slotted-switch-mark-colorvar(--slotted-tone-accent-solid)
--slotted-switch-pending-colorvar(--slotted-text-muted)
--slotted-switch-radiusvar(--slotted-radius-full, 9999px)
--slotted-switch-readonly-backgroundvar(--slotted-surface, Canvas)
--slotted-switch-readonly-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-switch-thumb-backgroundvar(--slotted-tone-accent-on-solid)
--slotted-switch-thumb-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-switch-thumb-shadowvar(--slotted-control-shadow, none)
--slotted-switch-track-backgroundvar(--slotted-tone-secondary-solid, ButtonFace)
--slotted-switch-track-inline-sizecalc(var(--_thumb-size) * 2 + var(--_gutter) * 3 + var(--_border) * 2)

Theme tokens it reads

TokenDefault
--slotted-control-border-colorSet per scheme by the theme
--slotted-control-border-widthvar(--slotted-border-width-thin)
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-invalid-edge-widthvar(--slotted-border-width-thin)
--slotted-control-label-gap-lgcomfortable 12px · compact 8px · dense 8px
--slotted-control-label-gap-mdcomfortable 12px · compact 8px · dense 6px
--slotted-control-label-gap-smcomfortable 8px · compact 6px · dense 6px
--slotted-control-shadowSet per scheme by the theme
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-loop-slow1.6s
--slotted-easing-emphasizedcubic-bezier(0.3, 0, 0, 1.3)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-color-invalidSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-size-xs0.75rem
--slotted-line-height-md1.5
--slotted-line-height-xs1.4
--slotted-radius-full9999px
--slotted-space-10.125rem
--slotted-space-30.375rem
--slotted-surfaceSet per scheme by the theme
--slotted-target-min24px
--slotted-textSet per scheme by the theme
--slotted-text-mutedSet per scheme by the theme
--slotted-tone-accent-on-solidSet per scheme by the theme
--slotted-tone-accent-solidSet per scheme by the theme
--slotted-tone-accent-solid-activeSet per scheme by the theme
--slotted-tone-accent-solid-hoverSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-secondary-solidSet per scheme by the theme
--slotted-tone-secondary-solid-activeSet per scheme by the theme
--slotted-tone-secondary-solid-hoverSet per scheme by the theme