shadcn.io is not affiliated with official shadcn/ui
Shadcn Switch for React and Tailwind
A switch for toggling a binary state.
Installation
bunx --bun shadcn@latest add https://kit.dev/r/switch.jsonpnpm dlx shadcn@latest add https://kit.dev/r/switch.jsonnpx shadcn@latest add https://kit.dev/r/switch.jsonyarn shadcn@latest add https://kit.dev/r/switch.jsonInstall the following dependencies:
bun add @ark-ui/reactpnpm add @ark-ui/reactnpm install @ark-ui/reactyarn add @ark-ui/reactCopy and paste the following code into your project.
"use client";
import { Switch as ArkSwitch, useSwitchContext } from "@ark-ui/react/switch";
import type React from "react";
import { cn } from "@/lib/utils";
export const useSwitch = useSwitchContext;
export const Switch = (props: React.ComponentProps<typeof ArkSwitch.Root>) => {
const { className, tabIndex, ...rest } = props;
return (
<ArkSwitch.Root
className={cn(
"group/switch",
"[--thumb-size:--spacing(5)] sm:[--thumb-size:--spacing(4)]",
"h-[calc(var(--thumb-size)+2px)] w-[calc(var(--thumb-size)*2-2px)]",
"p-px",
"inline-flex shrink-0 items-center",
"rounded-full border border-transparent",
"transition-all",
"outline-none [[data-focus-visible],[data-invalid]]:ring-[3px]",
"data-focus-visible:border-primary data-focus-visible:ring-ring/32",
"data-invalid:border-destructive data-invalid:ring-destructive/24",
"dark:data-invalid:border-destructive-foreground dark:data-invalid:ring-destructive-foreground/20",
"data-[state=checked]:bg-primary",
"data-[state=unchecked]:bg-input dark:data-[state=unchecked]:bg-input",
"data-disabled:pointer-events-none data-disabled:opacity-64",
"motion-reduce:transition-none!",
className
)}
data-slot="switch"
{...rest}
>
<ArkSwitch.Control
className="flex size-full items-center"
data-slot="switch-control"
>
<ArkSwitch.Thumb
className={cn(
"block",
"aspect-square h-full w-auto",
"bg-background",
"rounded-full ring-0",
"pointer-events-none",
"transition-transform",
"data-[state=checked]:translate-x-[calc(var(--thumb-size)-4px)]",
"dark:data-[state=checked]:bg-primary-foreground",
"data-[state=unchecked]:translate-x-0",
"dark:data-[state=unchecked]:bg-foreground",
"motion-reduce:transition-none!"
)}
data-slot="switch-thumb"
/>
</ArkSwitch.Control>
<ArkSwitch.HiddenInput tabIndex={tabIndex} />
</ArkSwitch.Root>
);
};Update 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";<div className="max-w-sm">
<Field orientation="horizontal">
<Switch defaultChecked />
<FieldLabel> Airplane mode</FieldLabel>
</Field>
</div>Accessibility
| Key | Description |
|---|---|
Space | Activate the focused control. |
Demos
Card
Controlled
Description
Disabled
Form
Invalid
Size
API Reference
Switch
label
PropType
AttributeType
CSS variableType
SwitchControl
span
PropType
AttributeType
CSS variableType
SwitchThumb
span
PropType
AttributeType
CSS variableType