About
An accessible toggle switch built on Base UI's Switch primitive. Animated thumb with a springy press/scale interaction, two sizes (default and sm), controlled or uncontrolled usage via checked / defaultChecked, disabled state, and easy accent recoloring through the data-checked:* utilities. Re-exports SwitchPrimitive for advanced composition.
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Switch to your project and fits it to your theme.
Code
How Switch is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
import { Switch } from "@/components/ui/cnippet-switch";
export default function SwitchColors() {
return (
<div className="flex flex-col gap-4">
<div className="flex items-center gap-2">
<Switch className="data-checked:bg-blue-500" defaultChecked id="sw-blue" />
<label className="font-medium text-sm" htmlFor="sw-blue">
Blue
</label>
</div>
<div className="flex items-center gap-2">
<Switch
className="data-checked:bg-green-500"
defaultChecked
id="sw-green"
/>
<label className="font-medium text-sm" htmlFor="sw-green">
Green
</label>
</div>
<div className="flex items-center gap-2">
<Switch
className="data-checked:bg-yellow-500"
defaultChecked
id="sw-yellow"
/>
<label className="font-medium text-sm" htmlFor="sw-yellow">
Yellow
</label>
</div>
</div>
);
}
















