Skip to content

Switch

Download .md

A switch turns one setting on or off, and the change applies right away, without a save button.

  • Use it for settings that apply at once, such as email notifications.
  • Use a Checkbox for a choice that is saved with a form, or for agreeing to terms.
  • Write the label as the setting, not as a question: “Kirim notifikasi email”.
React

Kabar rilis dan royalti masuk ke inbox kamu.

Vue

Kabar rilis dan royalti masuk ke inbox kamu.

DoUse a switch for a setting that applies the moment it changes.
Don'tDon't put switches in a form that waits for a save button; use checkboxes there.
1234
  1. 1Track, primary when on
  2. 2Thumb
  3. 3Label
  4. 4Off state

A pill track that is input when off and primary when on, with a background thumb. It sits before its label and a one-sentence description, in a Field with the horizontal orientation.

Two sizes: default and sm.

React Vue Flutter Type Default Use
checked / defaultChecked v-model / default-value value boolean false On or off.
onCheckedChange @update:model-value onChanged function none Runs when it changes.
size size width / height default, sm default The size.
disabled disabled enabled: false boolean false Blocks changes.
label in FieldLabel label in FieldLabel label, sublabel text none Names the setting.
import { useId } from "react"
import { Field, FieldContent, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchDemo() {
const id = useId()
return (
<Field orientation="horizontal" className="w-full max-w-sm">
<Switch id={`${id}-email`} aria-describedby={`${id}-email-hint`} />
<FieldContent>
<FieldLabel htmlFor={`${id}-email`}>Kirim notifikasi email</FieldLabel>
<FieldDescription id={`${id}-email-hint`}>Kabar rilis dan royalti masuk ke inbox kamu.</FieldDescription>
</FieldContent>
</Field>
)
}
Terminal window
npx shadcn@latest add https://design.sosialoka.tech/r/react/switch.json
  • It has role="switch" and aria-checked.
  • Space toggles it; clicking the label toggles it too.
  • Tie the description to it with aria-describedby.