Switch

Binary on and off control for immediate settings.

Also known as: toggle switch, on off switch, binary switch, settings switch

Installation

Install with the CLI, or copy the component source into your project.

Add switch (npm)

npx --yes -p @default-file/ui df-ui add switch

Default

Checked by default.

Sizes

Small and default sizes.

Disabled

Non-interactive states.

Properties

Binary on and off control for immediate settings. Prefer Toggle Group for option clusters.

Switch

PropTypeDefaultNotes
checkedboolean-Controlled checked state.
defaultCheckedbooleanfalseUncontrolled initial checked state.
onCheckedChange(checked: boolean) => void-Called when the switch toggles.
size"sm" | "default""default"Control size.
disabledboolean-Non-interactive switch.
classNamestring-Additional styles.

Tokens

Theme defaults live on :root. Override these globally or on a local host.

TokenTypeDefaultNotes
--df-switch-thumb-gapCSS lengthcalc(1 * var(--spacing-unit))Inset between track edge and thumb. Source of truth for enclosed vertical slack.
--df-switch-thumb-sizeCSS lengthvar(--df-affordance-size-sm)Default thumb diameter.
--df-switch-thumb-travelCSS lengthvar(--df-switch-thumb-size)Default checked translate distance. Matches the thumb diameter.
--df-switch-track-heightCSS lengthcalc(var(--df-switch-thumb-size) + 2 * var(--df-switch-thumb-gap))Default track height. Derived as thumb size plus twice the thumb gap (40x24 with the default gap).
--df-switch-track-widthCSS lengthcalc(var(--df-switch-track-height) + var(--df-switch-thumb-travel))Default track width. Derived as track height plus thumb travel.
--df-switch-thumb-size-smCSS lengthvar(--df-affordance-size-xs)Small thumb diameter.
--df-switch-thumb-travel-smCSS lengthvar(--df-switch-thumb-size-sm)Small checked translate distance. Matches the small thumb diameter.
--df-switch-track-height-smCSS lengthcalc(var(--df-switch-thumb-size-sm) + 2 * var(--df-switch-thumb-gap))Small track height. Derived as small thumb size plus twice the thumb gap (32x20 with the default gap).
--df-switch-track-width-smCSS lengthcalc(var(--df-switch-track-height-sm) + var(--df-switch-thumb-travel-sm))Small track width. Derived as small track height plus small thumb travel.
--primaryCSS colorvar(--df-neutral-850, var(--df-neutral-900))Checked track fill.
--inputCSS colorvar(--df-neutral-100)Unchecked track fill.
--backgroundCSS colorvar(--df-neutral-0)Thumb fill.