Switch is a native on/off control. The knob moves on a spring curve and stretches while pressed, like on iOS. It is a checkbox with role="switch", so it works in forms without JavaScript and submits its value by name.
Browser Support
Feature
Baseline
Your browser
appearance
Customizes the native checkbox appearance while retaining its browser behavior.
Your browser:
linear() easing
Spring easing for the knob. Browsers without it fall back to cubic-bezier.
Your browser:
:has()
Focus ring around the switch.
Your browser:
Preview
Installation
Usage
import { Switch } from "@/components/ui/switch"
import { Label } from "@/components/ui/label"
<Switch id="airplane" name="airplane" defaultChecked />
<Label htmlFor="airplane">Airplane mode</Label>Examples
Sizes
<Switch size="sm" />
<Switch />
<Switch size="lg" />With a label
<Switch id="airplane" />
<Label htmlFor="airplane">Airplane mode</Label>Disabled
<Switch disabled />
<Switch disabled defaultChecked />Toast on change
<Switch
aria-label="Notifications"
onChange={(e) =>
customToast.success(
`Notifications: ${e.currentTarget.checked ? "on" : "off"}`,
)
}
/>In a form
<form>
<Switch name="notifications" value="yes" defaultChecked />
<Switch name="sound" />
<Button type="submit">Save</Button>
</form>
// FormData: { notifications: "yes" }Settings list
<li className="flex items-center justify-between">
<Label htmlFor="sound">Sound</Label>
<Switch id="sound" defaultChecked />
</li>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "sm" | "default" | "lg" | "default" | Control size. |
| checked / defaultChecked | boolean | - | Controlled or initial state. |
| disabled | boolean | - | Disables the switch. |
| name | string | - | Field name in the form. |
| value | string | - | Value submitted when on. Defaults to "on". |
| className | string | - | Classes for the wrapper element. |
| ...props | InputHTMLAttributes<HTMLInputElement> | - | Other native input attributes, including onChange and aria attributes. |