Skip to content
GetUrUI

Switch

.md

Toggle switch whose thumb slides on a stiff spring and stretches while pressed.

switchtogglesettingsformspring

Installation

$npx shadcn@latest add @geturui/switch
Download source (.zip)

Usage

usage.tsx
1import { Switch } from "@/components/ui/switch";
2
3export function EmailSettings() {
4  return (
5    <Switch
6      label="Email me about releases"
7      defaultChecked
8      onCheckedChange={(on) => console.log("releases", on)}
9    />
10  );
11}

Props

PropTypeDefaultDescription
checkedboolean—On state when controlled.
defaultCheckedbooleanfalseInitial state when uncontrolled.
onCheckedChange(checked: boolean) => void—Called with the new state.
labelReactNode—Visible label; wraps the switch in a <label> so the text toggles it too.
disabledbooleanfalseDims the switch and blocks input.
aria-labelstring—Accessible name when there is no visible label.
classNamestring—Classes for the label wrapper, or the switch when there is no label.

Accessibility

  • A native <button role="switch"> with aria-checked, toggled with Space or Enter.
  • With label, the text is a real <label>, so clicking it toggles and it becomes the accessible name.
  • Without a visible label, pass aria-label or wrap it in your own <label>.

Related