Component guideEditable source

Switch

A two-state control for enabling or disabling a setting.

Original component docs
On this page4 sectionsBack to top

Use Switch for an on/off setting. Use Checkbox for an explicit choice or agreement submitted with a form. Decide whether changing the switch saves immediately or remains a draft.

Live preview
Loading preview...

#Installation

Complete the base setup. For manual installation:

npx shadcn@latest add https://neobrutal-ui.andongmin.com/r/switch.json

#Usage

"use client";
import { useState } from "react";
import { Switch } from "@/components/ui/switch";

export default function NotificationSetting() {
  const [enabled, setEnabled] = useState(false);
  return (
    <div className="grid gap-3">
      <div className="flex items-center gap-3">
        <Switch id="notifications" checked={enabled} onCheckedChange={setEnabled} />
        <label htmlFor="notifications">Email notifications</label>
      </div>
      <p role="status">Preview setting: {enabled ? "on" : "off"}.</p>
    </div>
  );
}

#API reference

PropPurposeDefault
checked / onCheckedChangeControlled boolean stateUncontrolled
defaultCheckedInitial statefalse
sizedefault or smdefault
name / valueForm key and checked valueNative behavior
disabled / requiredInteraction and form constraintsfalse
idVisible label targetGenerated if omitted

#Accessibility

Use a stable label describing the setting, not changing action text such as Enable/Disable. Do not rely only on color to show state. Show pending or error state for server updates and never imply an unsaved change has been persisted.