# Switch

A toggle that turns one setting on or off straight away.

```tsx
import { Switch } from '@halo-ds/react';
```

## When to use it
Use a switch for a setting that takes effect the moment it is flipped, with no Save button. Switch is the toggle on its own; Switch field adds a label and description, with the switch at the start or the end of the row.

## Do
- Name the setting, like "Email notifications", not the action.
- Apply the change straight away, and show its effect.
- Say why a switch is disabled in its description.
- Use End in settings lists, so switches line up on the right.

## Don't
- Use a switch in a form with a Save button; use a Checkbox.
- Change the label when the switch flips, like "On" and "Off".
- Use a switch to pick between two options; use a Radio or Segmented control.
- Leave a switch without a visible label.

## Switch or Checkbox?
Both turn something on or off. Pick by when the change takes effect.
- **Switch**: The change happens straight away, with no Save button. For settings, like notifications or reduced motion. The label names the setting, not an action.
- **Checkbox or Radio**: Checkbox: the choice is sent with a form, or people agree to terms. Radio: one option from a short list of choices. If people must press Save, use a Checkbox, not a Switch.

## Props
### Switch props
| Prop | Type | What it does |
| --- | --- | --- |
| `label` | `ReactNode` | The setting it turns on or off. Figma: Label. |
| `description` | `ReactNode` | Extra detail, like why it is disabled. Figma: Description. |
| `checked` | `boolean \| undefined` |  |
| `defaultChecked` | `boolean \| undefined` |  |
| `onCheckedChange` | `((checked: boolean) => void) \| undefined` |  |
| `size` | `"sm" \| "md" \| undefined` | 28 by 16 (sm) or 36 by 20 (md). Figma: Size. |
| `controlPosition` | `"end" \| "start" \| undefined` | Switch before the label, or at the end of a full-width row. Figma: Control position. |
| `classNames` | `Partial<Record<SwitchPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<SwitchPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { Switch } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ display: 'grid', gap: 16, width: 320 }}>
      <Switch label="Dark mode" defaultChecked />
      <Switch label="Auto-save" description="Saves every change as you type." />
    </div>
  );
}
```

### Settings list
controlPosition="end" puts every switch on the right, lined up.
```tsx
import { Switch } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: 360 }}>
      <Switch controlPosition="end" label="Email notifications" description="A weekly summary of activity." defaultChecked />
      <Switch controlPosition="end" label="Reduce motion" description="Turn off animations." />
      <Switch controlPosition="end" label="Sync calendar" description="Connect a calendar first." disabled />
    </div>
  );
}
```

### Small
Small switches for view options in panels.
```tsx
import { Switch } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack">
      <Switch size="sm" label="Show weekends" defaultChecked />
      <Switch size="sm" label="Show week numbers" />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/switch
