Switch
A toggle control for a single setting, with default and small sizes.
Preview
switch-demo
Installation
pnpm dlx shadcn@latest add http://localhost:3000/r/switch.jsonSource
import { Switch as SwitchPrimitive } from "@base-ui/react/switch";
import { cn } from "@/lib/utils";
function Switch({
className,
size = "default",
...props
}: SwitchPrimitive.Root.Props & {
size?: "default" | "sm";
}) {
return (
<SwitchPrimitive.Root
data-slot="switch"
data-size={size}
className={cn(
"inline-flex shrink-0 cursor-pointer items-center rounded-full bg-input p-0.5 inset-shadow-xs transition-colors outline-none focus-visible:ring-3 focus-visible:ring-ring/20 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 data-checked:bg-primary data-[size=default]:h-5 data-[size=default]:w-9 data-[size=sm]:h-4 data-[size=sm]:w-7",
className,
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className="block rounded-full bg-background transition-transform in-data-[size=default]:size-4 in-data-[size=sm]:size-3 in-data-[size=default]:data-checked:translate-x-4 in-data-[size=sm]:data-checked:translate-x-3"
/>
</SwitchPrimitive.Root>
);
}
export { Switch };Props
| Prop | Type | Default | Description |
|---|---|---|---|
size | "default" | "sm" | "default" | Controls the track and thumb size. |
checked | boolean | - | Controlled checked state. |
defaultChecked | boolean | - | Initial checked state when uncontrolled. |
onCheckedChange | function | - | Called with the next checked state. |
className | string | - | Adds custom classes to the switch. |