# Switch

An on/off setting on Base UI, for choices that take effect as a setting rather than a selection. Pass the label as children. Use Checkbox when the choice is part of a form the reader submits as a list of options.

```tsx
import { Switch } from '@blocks/design-system'
```

Switch is a client component. A Server Component can render it uncontrolled with `defaultChecked`; `checked` and `onCheckedChange` belong in a client-authored caller.

## In a native form

With a `name`, a checked switch submits its `value` (`on` unless you set one); an unchecked switch submits nothing, like a checkbox.

**Example: Station sharing** (`examples/switch/public-station.tsx`)

```tsx
// A Server Component: an uncontrolled switch submits with a native form.
import { Button, Stack, Switch } from '@blocks/design-system'

export default function PublicStation() {
  return (
    <Stack as="form" action="/components/switch" method="get" gap="sm" aria-label="Station sharing">
      <Switch name="public" value="yes" defaultChecked>
        Show on the public map
      </Switch>
      <div>
        <Button type="submit" variant="primary">
          Save sharing
        </Button>
      </div>
    </Stack>
  )
}
```

## Controlled

Own the state when something else on the page depends on it, and say what the setting is now.

**Example: Weekly digest** (`examples/switch/digest.tsx`)

```tsx
'use client'

import { Body, Stack, Switch } from '@blocks/design-system'
import { useState } from 'react'

export default function Digest() {
  const [enabled, setEnabled] = useState(false)
  return (
    <Stack gap="xs">
      <Switch checked={enabled} onCheckedChange={setEnabled}>
        Weekly snow digest
      </Switch>
      <Body margin="none" role="status">
        Digest emails are {enabled ? 'on' : 'off'}.
      </Body>
    </Stack>
  )
}
```

## Inside a Field

Without children, Switch takes its label and description from a surrounding Field.

**Example: Hourly sync** (`examples/switch/in-field.tsx`)

```tsx
import { Field, FieldDescription, FieldLabel, Switch } from '@blocks/design-system'

export default function InField() {
  return (
    <Field name="autoSync">
      <FieldLabel>
        <Switch defaultChecked />
        Sync readings every hour
      </FieldLabel>
      <FieldDescription>Turn off to save battery on solar-powered stations.</FieldDescription>
    </Field>
  )
}
```

## Props

**`Switch` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `checked` | `boolean` | — | Whether the switch is currently active. To render an uncontrolled switch, use the `defaultChecked` prop instead. |
| `children` | `ReactNode` | — | The switch's label, rendered next to it inside a `<label>`. |
| `defaultChecked` | `boolean` | `false` | Whether the switch is initially active. To render a controlled switch, use the `checked` prop instead. |
| `disabled` | `boolean` | `false` | Whether the component should ignore user interaction. |
| `form` | `string` | — | Identifies the form that owns the hidden input. Useful when the switch is rendered outside the form. |
| `id` | `string` | — | The id of the hidden input element. When `nativeButton` is `true`, the id is applied to the root element. |
| `inputRef` | `Ref<HTMLInputElement>` | — | A ref to access the hidden `<input>` element. |
| `name` | `string` | — | Identifies the field when a form is submitted. |
| `nativeButton` | `boolean` | `true` | Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`). |
| `onCheckedChange` | `((checked: boolean, eventDetails: { … }) => void)` | — | Event handler called when the switch is activated or deactivated. |
| `readOnly` | `boolean` | `false` | Whether the user should be unable to activate or deactivate the switch. |
| `render` | `ReactElement<unknown, string \| JSXElementConstructor<any>> \| ComponentRenderFn<HTMLProps, SwitchRootState>` | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |
| `required` | `boolean` | `false` | Whether the user must activate the switch before submitting a form. |
| `uncheckedValue` | `string` | — | The value submitted with the form when the switch is off. By default, unchecked switches do not submit any value, matching native checkbox behavior. |
| `value` | `string` | — | The value submitted with the form when the switch is on. By default, switch submits the "on" value, matching native checkbox behavior. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

## Accessibility

- Switch has the `switch` role and announces “on” or “off”. Space and Enter toggle it; clicking the label does too.
- Label the setting, not the action: “Weekly snow digest”, not “Turn on digest”.
- If turning a switch on has a delay or can fail, show the outcome nearby in words.
