# Switch

> An on/off setting that takes effect immediately.

Category: Selection · Atomic level: atom · Built on Base UI Switch
Page: https://ui.dtheo.dev/docs/components/switch

## Install

```bash
npx dtheo@latest add switch
# or, with the shadcn CLI
npx shadcn@latest add https://ui.dtheo.dev/r/switch.json
```

Dependencies: @base-ui/react, clsx, tailwind-merge. Registry dependencies: utils.

## Exports

`Switch`

## Use it for

- Preferences and live toggles that apply at once

## Reach for something else

- Choices confirmed with Save: use Checkbox

## Keyboard

| Keys | Action |
| --- | --- |
| Space / Enter | Toggles the switch |

## Examples

### switch

```tsx
import * as React from 'react';
import { Field, FieldDescription, FieldLabel } from '@/components/ui/field';
import { Switch } from '@/components/ui/switch';

export default function SwitchExample() {
  const [streaming, setStreaming] = React.useState(true);
  return (
    <div className="flex w-full max-w-sm flex-col gap-4">
      <Field>
        <FieldLabel className="w-full items-center justify-between gap-4">
          Stream live traces
          <Switch checked={streaming} onCheckedChange={setStreaming} />
        </FieldLabel>
        <FieldDescription>
          {streaming
            ? 'Bench 3 is streaming torque and current at 1 kHz.'
            : 'Paused. Traces still record on the bench.'}
        </FieldDescription>
      </Field>
      <Field>
        <FieldLabel className="w-full items-center justify-between gap-4">
          Stop on winding overheat
          <Switch defaultChecked />
        </FieldLabel>
        <FieldDescription>Cuts motor power when a winding passes 85 °C.</FieldDescription>
      </Field>
      <Field disabled>
        <FieldLabel className="w-full items-center justify-between gap-4">
          Write gains to flash
          <Switch />
        </FieldLabel>
        <FieldDescription>Locked while R-0412 is running.</FieldDescription>
      </Field>
    </div>
  );
}
```

## Used in

- Forms pattern (template): https://ui.dtheo.dev/docs/patterns/forms
