shadcn.io is not affiliated with official shadcn/ui
Installation
bunx --bun shadcn@latest add https://kit.dev/r/aria/switch.jsonnpx shadcn@latest add https://kit.dev/r/aria/switch.jsonpnpm dlx shadcn@latest add https://kit.dev/r/aria/switch.jsonyarn shadcn@latest add https://kit.dev/r/aria/switch.jsonInstall the following dependencies:
bun add react-aria-componentsnpm install react-aria-componentspnpm add react-aria-componentsyarn add react-aria-componentsCopy and paste the following code into your project.
"use client";
import {
composeRenderProps,
Switch as SwitchPrimitive,
type SwitchProps as SwitchPrimitiveProps,
} from "react-aria-components";
import { cn } from "@/lib/utils";
export const Switch = (
props: SwitchPrimitiveProps & {
size?: "sm" | "default";
}
) => {
const { className, size = "default", children, ...rest } = props;
return (
<SwitchPrimitiveUpdate the import paths to match your project setup.
Anatomy
Switch
├── SwitchLabel
├── SwitchControl
└── SwitchThumbUsage
import { Field, FieldLabel } from "@/components/ui/field";
import { Switch } from "@/components/ui/switch";<Field className="w-fit" orientation="horizontal">
<Switch />
<FieldLabel>Airplane Mode</FieldLabel>
</Field>Accessibility
| Key | Description |
|---|---|
Space | Activate the focused control. |
Demos
Description
Switch with a label and description.
Choice Card
Card-style selection where FieldLabel wraps the entire Field for a clickable card pattern.
Disabled
Add the disabled prop to the Switch and Field to disable the control.
Invalid
Add aria-invalid to the Switch and invalid to the Field for invalid styling.
Sizes
Use the size prop to change the size of the switch.
Rtl
Right-to-left switch. See the RTL configuration guide for document direction.
API Reference
Switch
PropType
AttributeType