# Switch

A marble that rolls along a sunken groove and squashes as it lands.

Docs: https://cozy-ui.solo.engineer/docs/components/switch/

Install: `npx shadcn@latest add @cozy/switch`

### Usage

```tsx
import { Switch } from "@/components/ui/switch"

<Switch defaultChecked />
```

### Example

```tsx
import { useState } from "react";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

export default function SwitchDemo() {
  const [music, setMusic] = useState(true);
  return (
    <div className="grid gap-4">
      <Label className="cursor-pointer"><Switch checked={music} onCheckedChange={setMusic} /> Music {music ? "on" : "off"}</Label>
      <Label className="cursor-pointer"><Switch /> Vibration</Label>
      <Label><Switch disabled defaultChecked /> Cloud save (offline)</Label>
    </div>
  );
}
```

### Source (components/ui/switch.tsx)

```tsx
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("cz-switch", className)} {...props}>
      <SwitchPrimitive.Thumb data-slot="switch-thumb" className="cz-switch__thumb" />
    </SwitchPrimitive.Root>
  );
}

export { Switch };
```

npm: `import { Switch } from "@0xtrou/cozy-ui"` (after `npm install @0xtrou/cozy-ui` and `import "@0xtrou/cozy-ui/styles.css"`).
