# Switch

shadcn switch as a two-state pixel toggle with a dithered on side.

Source: https://www.ui95.dev/ui/switch

## Examples

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

import type { ShowcaseDemo } from "../types"

type SwitchState = {
  label: string
  size: "sm" | "default"
  isChecked: boolean
  isDisabled?: boolean
  isInvalid?: boolean
}

const STATES: readonly SwitchState[] = [
  { label: "off", size: "default", isChecked: false },
  { label: "on", size: "default", isChecked: true },
  { label: "sm off", size: "sm", isChecked: false },
  { label: "sm on", size: "sm", isChecked: true },
  { label: "disabled off", size: "default", isChecked: false, isDisabled: true },
  { label: "disabled on", size: "default", isChecked: true, isDisabled: true },
  { label: "invalid", size: "default", isChecked: false, isInvalid: true },
]

function renderSwitch({ label, size, isChecked, isDisabled, isInvalid }: SwitchState) {
  const id = `switch-${label.replace(/\s/g, "-")}`
  return (
    <div className="flex items-center gap-3">
      <Switch
        id={id}
        size={size}
        defaultChecked={isChecked}
        disabled={isDisabled}
        aria-invalid={isInvalid}
      />
      <Label htmlFor={id}>Turbo</Label>
    </div>
  )
}

export const demo: ShowcaseDemo = {
  span: "single",
  variants: STATES.map((state) => ({ label: state.label, render: () => renderSwitch(state) })),
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/switch.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cn radix-ui
```

`components/ui/switch.tsx`

```tsx
"use client"

import { cn } from "cn"
import { Switch as SwitchPrimitive } from "radix-ui"
import * as React from "react"

function Switch({
  className,
  size = "default",
  ...props
}: React.ComponentProps<typeof SwitchPrimitive.Root> & {
  size?: "sm" | "default"
}) {
  return (
    <SwitchPrimitive.Root
      data-slot="switch"
      data-size={size}
      className={cn(
        "peer group/switch relative inline-flex shrink-0 items-stretch pixel-border border-border bg-card after:absolute after:-inset-x-3 after:-inset-y-2 aria-invalid:border-dashed aria-invalid:border-destructive data-[size=default]:h-6 data-[size=default]:w-11 data-[size=sm]:h-5 data-[size=sm]:w-8 data-disabled:cursor-not-allowed data-disabled:border-muted-foreground",
        className
      )}
      {...props}
    >
      <span
        aria-hidden="true"
        className="pointer-events-none absolute inset-y-0 left-0 hidden w-1/2 bg-foreground dither-50 group-data-checked/switch:block group-data-disabled/switch:bg-muted-foreground"
      />
      <SwitchPrimitive.Thumb
        data-slot="switch-thumb"
        className="pointer-events-none relative block h-full w-1/2 pixel-border border-border bg-secondary bevel-raised offset-shadow-none data-checked:translate-x-full group-data-disabled/switch:border-muted-foreground group-data-disabled/switch:bg-muted"
      />
    </SwitchPrimitive.Root>
  )
}

export { Switch }
```

## API reference

### Switch

| Prop | Type | Default |
| --- | --- | --- |
| `size` | `"sm" \| "default"` | `"default"` |

## Sounds

- `switch.on`
- `switch.off`
