Switch

A toggle switch for boolean settings with default and compact size variants.

Installation

$
npx shadcn@latest add https://ui.mattdowney.com/r/switch.json

Usage

import { Switch } from "@/components/ui/switch"

Examples

Default

For a setting that takes effect the moment it is flipped. A switch has no Save step, so use a checkbox in a form when the change needs confirming first.

Small

The 16x24px track, for dense rows and menus. Step the label down with it (text-xs here) or the control looks undersized against its own text.

API Reference

PropTypeDefaultDescription
size"sm" | "default""default"Track size. default is 20×32px; sm is a compact 16×24px.
checkedbooleanNoneControlled on/off state. Pair with onCheckedChange.
defaultCheckedbooleanfalseInitial state when uncontrolled.
onCheckedChange(checked: boolean) => voidNoneCalled with the new state whenever the switch is toggled.
disabledbooleanfalsePrevents interaction and dims the control.
namestringNoneName submitted with the owning form's data.
valuestring"on"Value submitted with the form when the switch is on.
size"sm" | "default" = "default"Track size. default is 20×32px; sm is a compact 16×24px.
checkedbooleanControlled on/off state. Pair with onCheckedChange.
defaultCheckedboolean = falseInitial state when uncontrolled.
onCheckedChange(checked: boolean) => voidCalled with the new state whenever the switch is toggled.
disabledboolean = falsePrevents interaction and dims the control.
namestringName submitted with the owning form's data.
valuestring = "on"Value submitted with the form when the switch is on.