# Switch

Turns a setting on or off immediately.

- Family: selection
- Import: `import { Switch } from "@senvenli/react/selection";`
- Page: https://design.senvenli.com/docs/components/switch
- JSON: https://design.senvenli.com/docs/components/switch.json

## Example: A setting

Named by its on-state; it applies at once.

```tsx
import { Switch } from "@senvenli/react/selection";

export default function Example() {
  return <Switch defaultSelected>Online orders</Switch>;
}
```

## Examples

### Saved by the app

Controlled, with a description that reports the state.

```tsx
import { Switch } from "@senvenli/react/selection";
import { useState } from "react";

export default function Example() {
  const [isOn, setIsOn] = useState(false);
  return (
    <Switch isSelected={isOn} onChange={setIsOn} description={isOn ? "A summary is sent every Monday at 9:00." : "No summary is sent."}>
      Weekly summary email
    </Switch>
  );
}
```

### Disabled

A setting that cannot change, on and off.

```tsx
import { Stack } from "@senvenli/react/layout";
import { Switch } from "@senvenli/react/selection";

export default function Example() {
  return (
    <Stack space="space-150">
      <Switch isDisabled defaultSelected description="Required for every member of this workspace.">
        Two-step sign-in
      </Switch>
      <Switch isDisabled description="Available on the Business plan.">
        Audit log
      </Switch>
    </Stack>
  );
}
```

### Sizes

`size` small, medium (default) or large.

```tsx
import { Stack } from "@senvenli/react/layout";
import { Switch } from "@senvenli/react/selection";

export default function Example() {
  return (
    <Stack space="space-150">
      <Switch size="small" defaultSelected>
        Small
      </Switch>
      <Switch defaultSelected>Medium</Switch>
      <Switch size="large" defaultSelected>
        Large
      </Switch>
    </Stack>
  );
}
```

## Guidelines

### When to use it

- A setting that applies at once.

### Use something else when

- [checkbox](https://design.senvenli.com/docs/components/checkbox.md): The choice is submitted with a form.

### Do and avoid

- Name the setting by its on-state ("Online orders"), never "On/Off".

## Accessibility

Keyboard:

- Space: Toggles it.

Built in:

- Announced as a switch with its state; a busy state while saving.

You provide:

- A label naming the setting.

## API reference

### Switch

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| size | `ComponentSize` |  | no | `small`, `medium` (default) or `large`; the provider default otherwise. |
| children | `React.ReactNode` |  | yes | The setting, named as its on-state ("Online orders"), never "On/Off". |
| isSelected | `boolean` |  | no |  |
| defaultSelected | `boolean` |  | no |  |
| onChange | `(isSelected: boolean) => void` |  | no |  |
| description | `React.ReactNode` |  | no |  |
| isDisabled | `boolean` |  | no |  |
| name | `string` |  | no |  |
| aria-busy | `boolean \| "true" \| "false"` |  | no | Marks a pending change ("saving"): `aria-busy` on the switch control itself, the `role="switch"` input, where screen readers read it. It does not lock the switch: it still toggles unless `isDisabled` is set too. Left out, the DOM is unchanged. It is applied after hydration (in a layout effect, as React Aria does not forward it), so server-rendered HTML does not carry it; it appears on the first client render. |

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-disabled`, `--ds-background-input`, `--ds-background-neutral-pressed`, `--ds-background-selected`, `--ds-border`, `--ds-border-bold`, `--ds-border-danger`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-radius-full`, `--ds-radius-large`, `--ds-radius-xsmall`, `--ds-shadow-raised`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-surface-raised`, `--ds-text`, `--ds-text-disabled`, `--ds-text-on-brand`, `--ds-text-subtlest`

## Related

- [checkbox](https://design.senvenli.com/docs/components/checkbox.md)
- [toggle-button](https://design.senvenli.com/docs/components/toggle-button.md)
