# Switch

Turns a setting on or off at once, with no Save. Built on ui-sdk's Switch: Space and Enter flip it, and it submits with its form. SwitchRow lays out a labelled setting.

- Beta: ready to use; its API can still change in a minor release.
- Category: Selection

```tsx
import { Switch, SwitchRow } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for a setting that takes effect at once with no Save, like email notifications for assigned tasks.
- Do: Use `SwitchRow` for a setting on its own line: the label names the switch, the description says what changes, and pressing the label flips it.
- Do: Write the label as a statement of what the switch turns on, like Email me when a task is assigned, so on and off read plainly.
- Do: Use `size="sm"` in dense rows, like a table of firm settings. Both sizes extend their hit area past the track.
- Don't: Use it inside a form that waits for Save, like a new contact form. Use `Checkbox`, so nothing changes before the lawyer saves.
- Don't: Use it to pick between two named options, like Email or Phone. Use `RadioGroup` or `SegmentedControl`, which show both names.

## Keyboard

| Keys | What they do |
| --- | --- |
| Space | Toggles it. |
| Enter | Submits the form, and never toggles it. |

## Related

- [Checkbox](./checkbox.md)
- [Toggle](./toggle.md)
- [Segmented Control](./segmented-control.md)

## Examples

What the examples import:

```tsx
import { Fragment } from "react";
import { Switch, SwitchRow } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const sizes = ["md", "sm"] as const;

const states = ["Off", "On", "Disabled", "Disabled on"] as const;
```

### States and sizes

Hover for the darker edge or the lifted navy, press and hold to stretch the thumb, and Tab for focus.

```tsx
<div className="grid grid-cols-[auto_repeat(4,auto)] items-center justify-start gap-x-10 gap-y-5">
  <span />
  {states.map(state => (
    <span key={state} className="text-small text-neutral-text-muted">
      {state}
    </span>
  ))}
  {sizes.map(size => (
    <Fragment key={size}>
      <span className="text-small text-neutral-text-muted">{size}</span>
      {states.map(state => (
        <Switch
          key={`${size}-${state}`}
          size={size}
          aria-label={`${state}, ${size}`}
          {...stateProps[state]}
        />
      ))}
    </Fragment>
  ))}
</div>
```

### Switch row

A setting on a line. The label names the switch and the description describes it, so a screen reader hears both. Pressing the label flips it too.

```tsx
<div className="flex w-100 flex-col gap-2">
  <SwitchRow
    label="Email me when a task is assigned"
    description="You still see it in Notifications either way."
    defaultChecked
  />
  <SwitchRow label="Show archived opportunities" />
  <SwitchRow
    label="Sync with LEAP"
    description="Your firm administrator manages this."
    defaultChecked
    disabled
  />
</div>
```

## Props

Both also take ui-sdk's Switch props: checked, defaultChecked, onCheckedChange, disabled, readOnly, required, name, value and form.

### Switch

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"md" \| "sm"` | `"md"` | md (32 × 18) by default; sm (24 × 14) in dense rows. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### SwitchRow

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `ReactNode` |  | What the switch turns on, as a statement: "Email me when a task is assigned". |
| `description` | `ReactNode` |  | A quieter line under the label, saying what changes. |
