Switch
An accessible toggle switch supporting controlled, uncontrolled, and labeled modes
Quick Preview
Structure
Anatomy of the Switch โ a track with a sliding thumb and an optional label.
States
Click any switch to toggle it โ all state transitions are live.
Switch โ all states
Click to toggle; disabled switches cannot be interacted with
Off
Default ยท unchecked
On
Checked ยท click to try
Disabled off
Cannot interact
Disabled on
Locked in on state
Sizes
Three size tokens โ sm md (default) lg.
Sizes โ sm / md / lg
Toggle each switch to compare thumb and track dimensions
Small โ sm
w-8 h-4 ยท compact
Medium โ md
w-11 h-6 ยท default
Large โ lg
w-14 h-7 ยท spacious
Installation
// Copy the Switch component fromsrc/components/ui/Switch.tsx
Usage
'use client'import { Switch } from '@/components/ui/Switch'// Uncontrolled<Switch label="Dark mode" />// Controlledconst [enabled, setEnabled] = useState(false)<Switch checked={enabled} onChange={setEnabled} label="Dark mode" />
Examples
With Description
Sizes
Settings List
Prompt Preview
Copy a prompt that recreates this UI
Paste this into your AI coding assistant to generate code that closely matches the reference, including color, size, shape, typography, spacing, and polish.
Copy-ready AI prompt
Starts from the current visual reference and project constraints.
Tweak only product-specific copy or data after the first generation pass.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | - | Controlled checked state |
defaultChecked | boolean | - | Initial state for uncontrolled mode |
onChange | (checked: boolean) => void | - | Callback fired on toggle |
label | string | - | Label text next to the switch |
description | string | - | Secondary description below label |
size | "sm" | "md" | "lg" | "md" | Switch size |
disabled | boolean | - | Disable the switch |
Features
- โControlled and uncontrolled modes
- โrole="switch" + aria-checked for accessibility
- โLabel and description slots
- โThree sizes (sm, md, lg)
- โPrimary color when checked
- โfocus-visible ring
- โClient Component