Switch
A toggle control for switching between on and off states.
Installation
npx @smicolon/cli add switchRun init once per project before adding components. See Installation.
Preview
Variants
Two styles, as in Figma. solid, the default, fills the track, with the primary colour when on. outline keeps the track on the page background, draws it with a 1px line, and fills the thumb instead.
solidoutlineSizes
Three sizes. md is the default. The track is 32, 40 or 48px wide, and the label stays the same size.
solidoutlineLayout
control-first, the default, puts the track before the label and lines the description up under the label. label-first puts the label at the start and the track at the end of a full-width row, for settings lists.
control-firstlabel-firstStates
Focus is a real CSS state rather than a prop. Tab to a switch to see the ring. Disabled dims the whole switch, label and description included.
solidoutlineAccessibility
- Renders a native
<input type="checkbox" role="switch">, so Space toggles it and it submits with a form without extra code. descriptionis linked witharia-describedby; the track is hidden from assistive technology.- Without a
label, pass anaria-label. - Direction is inherited from
dir, never a prop. The thumb always moves towards the end of the line when the switch turns on.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "solid" | "outline" | "solid" | A filled track or an outlined one. |
layout | "control-first" | "label-first" | "control-first" | Track before the label, or after it. |
size | "sm" | "md" | "lg" | "md" | Switch size. |
checked | boolean | — | On state. Use it with onChange. |
defaultChecked | boolean | false | Starting state when you do not control it. |
disabled | boolean | false | Disables the switch and dims it. |
label | string | — | Label text. |
description | string | — | Secondary line below the label. |
name | string | — | Name submitted with the form. |
onChange | (event) => void | — | Called when the switch changes. |
className | string | — | Extra classes for the component. |
Usage
import { Switch } from "@/components/ui/switch"
// Basic switch
<Switch aria-label="Airplane mode" />
// With label
<Switch label="Airplane mode" defaultChecked />
// With description
<Switch
label="Dark mode"
description="Toggle dark mode on or off"
/>
// Outline style
<Switch variant="outline" label="Auto-sync" />
// Label first, track at the end
<Switch layout="label-first" label="Notifications" />
// Different sizes
<Switch size="sm" label="Small" />
<Switch size="md" label="Medium" />
<Switch size="lg" label="Large" />
// Controlled
const [enabled, setEnabled] = useState(true)
<Switch
label="Notifications"
checked={enabled}
onChange={(e) => setEnabled(e.target.checked)}
/>State: On