Switch
A simple switch component on top of the native checkbox.
Source Code
"use client";
import { cn } from "@/lib/utils";
const Switch = ({
className,
...props
}: Omit<React.ComponentPropsWithRef<"input">, "type">) => {
return (
<input
type="checkbox"
className={cn(
"appearance-none",
"bg-foreground/20 relative h-6 w-11 cursor-pointer rounded-xl transition",
// circle
"before:ease-out-expo before:bg-background before:absolute before:top-0.5 before:left-0.5 before:size-5 before:rounded-xl before:transition-transform before:duration-200",
// disabled
"disabled:before:opacity-30",
// checked
"checked:bg-foreground checked:before:translate-x-full",
// focus
"focus-visible:ring-ring focus-visible:ring-4 focus-visible:outline-none",
// stretch animation
"before:origin-left active:before:scale-x-110 active:before:rounded-[calc(var(--spacing)*2.25)] active:checked:before:origin-right",
className
)}
{...props}
/>
);
};
export { Switch };
API Reference
Extends the native input
element with type="checkbox"
.
Examples
Simple
Basic usage of the switch component.
Disabled
Example of a disabled switch.
Disabled Checked
Example of a disabled switch in the checked state.
Custom style
Example showing custom styling options.
Best Practices
-
Usage:
- Use for immediate actions
- Provide clear labels
- Consider state persistence
-
Accessibility:
- Use clear state indicators