# Switch

> iOS-sized toggle in ink whose thumb slides on the spring.

Docs: https://plumbago.ochotzas.com/components/switch

## Installation

```sh
npx plumbago add switch
```

This copies `components/ui/switch.tsx` into your project, along with `cn`, and installs `@base-ui/react`.

## Usage

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

<Switch aria-label="Accept online orders" defaultChecked />
```

## Examples

### Preview

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

export default function SwitchDemo() {
  return (
    <label className="flex items-center gap-4 font-medium">
      <Switch defaultChecked />
      Accept online orders
    </label>
  )
}
```

### In list rows

```tsx
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle } from "@/components/ui/island"
import { Switch } from "@/components/ui/switch"

export default function SwitchInListRows() {
  return (
    <Island variant="list" className="w-full max-w-md">
      <ListRow render={<label />}>
        <ListRowText>
          <ListRowTitle>Online orders</ListRowTitle>
          <ListRowSubtitle>Guests can order from their table</ListRowSubtitle>
        </ListRowText>
        <Switch defaultChecked />
      </ListRow>
      <ListRow render={<label />}>
        <ListRowText>
          <ListRowTitle>Kitchen printer</ListRowTitle>
          <ListRowSubtitle>Print a ticket for every order</ListRowSubtitle>
        </ListRowText>
        <Switch defaultChecked />
      </ListRow>
      <ListRow render={<label />}>
        <ListRowText>
          <ListRowTitle>Tips on receipts</ListRowTitle>
          <ListRowSubtitle>Managed by your payment provider</ListRowSubtitle>
        </ListRowText>
        <Switch disabled />
      </ListRow>
    </Island>
  )
}
```

### With status

```tsx
import { BlurSwap } from "@/lib/blur-swap"
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle } from "@/components/ui/island"
import { Switch } from "@/components/ui/switch"
import { useState } from "react"

export default function SwitchWithStatus() {
  const [taking, setTaking] = useState(true)

  return (
    <Island variant="list" className="w-full max-w-md">
      <ListRow render={<label />}>
        <ListRowText>
          <ListRowTitle>Table ordering</ListRowTitle>
          <ListRowSubtitle>
            <BlurSwap text={taking ? "Guests can order from their table" : "Paused, guests can only browse the menu"} />
          </ListRowSubtitle>
        </ListRowText>
        <Switch checked={taking} onCheckedChange={setTaking} />
      </ListRow>
    </Island>
  )
}
```

## Built on Base UI

Switch wraps these Base UI parts and keeps their props:

- [Switch](https://base-ui.com/react/components/switch)

## Source

```tsx
import { Switch as BaseSwitch } from "@base-ui/react/switch"
import { cn } from "@/lib/cn"
import type { ComponentProps } from "react"

export type SwitchProps = Omit<ComponentProps<typeof BaseSwitch.Root>, "className"> & {
  className?: string
}

export function Switch({ className, ...props }: SwitchProps) {
  return (
    <BaseSwitch.Root
      data-slot="switch"
      className={cn(
        "group/switch relative inline-flex h-[31px] w-[51px] shrink-0 cursor-pointer items-center rounded-full bg-track p-0.5 transition-colors duration-base ease-out",
        "data-checked:bg-ink data-disabled:cursor-default data-disabled:opacity-40",
        "after:absolute after:-inset-y-[7px] after:inset-x-0 after:content-['']",
        className,
      )}
      {...props}
    >
      <BaseSwitch.Thumb
        data-slot="switch-thumb"
        className={cn(
          "h-[27px] w-[27px] rounded-full bg-white shadow-[0_2px_6px_rgba(0,0,0,0.18)] data-checked:translate-x-5 data-checked:bg-on-ink",
          "transition-[translate,width,background-color] duration-spring ease-spring",
          "group-active/switch:not-data-disabled:w-[33px] data-checked:group-active/switch:not-data-disabled:translate-x-[14px]",
        )}
      />
    </BaseSwitch.Root>
  )
}
```
