# Switch

An on/off control, monochrome: an ink track when on; with a visible label the whole row toggles.

Live variants: https://design.arcadia.is/components/switch

```tsx
import { Switch } from "@arcadia/ui";
```

## In context

Toggle a workspace preference. The change takes effect immediately.

```tsx
import { useState } from "react";
import { Switch } from "@arcadia/ui";

function Settings() {
  const [tips, setTips] = useState(true);
  return (
    <div role="group" aria-label="Workspace preferences" {...sx("cmp-form", cmp.form)}>
      <Switch
        label="Show shortcut tips"
        hint="Show keys beside toolbar actions."
        checked={tips}
        onCheckedChange={setTips}
      />
      <Switch
        label="Resume interrupted work"
        hint="Picks up where a chat stopped after a reload."
        defaultChecked
      />
      <Switch label="Completion sound" hint="Needs system notifications." disabled />
    </div>
  );
}
```

## When to use

- Turn a setting on or off immediately.
- Hide the label only when the context is clear.

## When not to use

- Use `Checkbox` for items selected in a form or list.
- Use `SegmentedControl` to choose among several options.

## Usage

```tsx
import { useState } from "react";
import { Switch } from "@arcadia/ui";

function Usage() {
  const [on, setOn] = useState(false);
  return <Switch label="Chime when a turn ends" checked={on} onCheckedChange={setOn} />;
}
```

Use a switch for changes that take effect immediately.

A visible label makes the whole row a click target.

Add a hint to explain why an option is disabled.

## Examples

### Track only

Use `hideLabel` in tight spaces and keep an accessible name.

```tsx
import { Switch } from "@arcadia/ui";

<Switch label="Auto model" hideLabel size="sm" defaultChecked />
<Switch label="Quiet mode" hideLabel size="sm" />
```

### In a form

Save the weekly digest preference.

```tsx
import { useState } from "react";
import { Button, Switch } from "@arcadia/ui";

function InAForm() {
  const [saved, setSaved] = useState<string | null>(null);
  const save = (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setSaved(new FormData(e.currentTarget).get("digest") ? "on" : "off");
  };
  return (
    <form {...sx("cmp-form", cmp.form)} onSubmit={save}>
      <Switch
        name="digest"
        label="Weekly digest"
        defaultChecked
      />
      <div {...sx("cmp-form-actions", cmp.formActions)}>
        {saved && <span {...sx("cmp-result", cmp.result)}>Saved: digest {saved}</span>}
        <Button type="submit" variant="primary" size="sm">Save</Button>
      </div>
    </form>
  );
}
```

## Matrix

Size and state

Rows: Small, Medium. Columns: Off, On, Hover, Focus, Disabled, Disabled, on, Read-only, on.

## Customising

Set layout and override the switch’s documented knobs.

```tsx
import * as stylex from "@stylexjs/stylex";

const styles = stylex.create({
  end: { marginInlineStart: "auto" },
});

<Switch label="Chime" hideLabel rootStyle={styles.end} />
```

- `--switch-track` (`Paint`, default `var(--fill-2)`): Track fill when off.
- `--switch-track-hover` (`Paint`, default `var(--fill-1)`): Track fill when off, under the pointer (on the track or its label row).
- `--switch-track-on` (`Paint`, default `var(--bg-inverse)`): Track fill when on.
- `--switch-track-on-press` (`Paint`, default `var(--bg-inverse-hover)`): Track fill when on, pressed.
- `--switch-knob-on` (`Paint`, default `var(--bg-app)`): Knob fill when on.

`rootStyle` lands on the label row, or on the track with `hideLabel`.

## Accessibility

The platform's own checkbox with `role="switch"`, laid clear over the track: it takes the focus, the press and the keys, and the kit draws the track and the knob. A read-only switch turns a press back before it toggles. The label and a `hint` are wired to it by id, so it reads "Resume interrupted work, switch, on, Picks up where a chat stopped after a reload". `label` is required, even with `hideLabel`. A label says what the switch turns on; one that cannot takes a `hint`, and a disabled switch says why in its hint, as Completion sound does above. Under forced colours, which drop every fill, the track keeps an edge in the system's ink, and on it fills with the system's highlight under a knob in the highlight's text.

- Space: Toggles the switch
- Tab: Moves to the next control

## API reference

### Switch

- `checked` (`boolean`): Controlled state.
- `defaultChecked` (`boolean`, default `false`): Initial state when uncontrolled, and what a form's reset puts back.
- `onCheckedChange` (`(checked: boolean, details: { event: Event }) => void`): Called with the new state when the switch toggles.
- `readOnly` (`boolean`): Shown but not changeable (`aria-readonly`).
- `label` (`ReactNode`, required): Required. Rendered as the row label, or as `aria-label` with `hideLabel` (then pass a string).
- `hint` (`string`): Why or how, for a switch its label cannot explain: a `Hint` after the label, and the switch's description. At most 160 characters.
- `hideLabel` (`boolean`): Render only the track; the label becomes its accessible name.
- `size` (`"sm" | "md"`, default `"md"`): `md`: 32×20 with a 16px knob, for settings. `sm`: 26×16 with a 12px knob, for menu rows.
- `rootStyle` (`SwitchStyle`): Typed StyleX override on the outermost element (the label row, or the track with `hideLabel`): layout and the `--switch-*` knobs.
- `disabled` (`boolean`): Dims the row and blocks toggling.

Rest props go to `<input>` props.
