Form
Switch
An on/off control for settings that take effect immediately.
LiveRev. 01
News about products and features.
Sign-ins from new devices.
Installation
CLI
$ npx shadcn@latest add https://konsa.rohitxyz.me/r/switch.jsonManual
Install the dependencies
npm install radix-uiCopy the source into your project
components/ui/switch.tsx
"use client";
import * as React from "react";
import { Switch as SwitchPrimitive } from "radix-ui";
import { cn } from "@/lib/utils";
function Switch({
className,
...props
}: React.ComponentProps<typeof SwitchPrimitive.Root>) {
return (
<SwitchPrimitive.Root
data-slot="switch"
className={cn(
"peer inline-flex h-5 w-9 shrink-0 items-center rounded-full border border-transparent p-0.5 transition-colors outline-none",
"focus-visible:ring-2 focus-visible:ring-ring/40 focus-visible:ring-offset-2 focus-visible:ring-offset-background",
"data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",
"disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className="pointer-events-none block size-4 rounded-full bg-background shadow-sm ring-0 transition-transform duration-200 ease-[cubic-bezier(0.32,0.72,0,1)] data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0"
/>
</SwitchPrimitive.Root>
);
}
export { Switch };Update the import paths to match your project
The file imports cn from @/lib/utils. See installation.
Usage
import { Switch } from "@/components/ui/switch";<Switch id="wifi" />
<Label htmlFor="wifi">Wi-Fi</Label>