# Switch

> A setting that's on or off and takes effect straight away. A pill track with a thumb that moves across. The label is SwitchField's.

| | |
|---|---|
| Kind | component |
| Contract | `review` · [switch.contract.yaml](https://github.com/Runceanu/rost/blob/master/contracts/switch.contract.yaml) |
| Guidance | `agreed` |
| Source | [Figma 528:402](https://www.figma.com/design/1Zy0wiWKXQAvsfDBX3KR0d/UI-Kit-v01?node-id=528-402) · read 2026-10-30 · frozen 2026-10-30 |
| Implemented in | nowhere yet |

## Install

It lands in `src/rost/switch/`, with what it needs and its contract:

```sh
npx github:Runceanu/rost add switch
```

Then import `src/rost/fonts.css` and `src/rost/tokens.css` once, at the root of the app. More on this in [Install](https://rost.build/docs/install.md).

## Props

| Prop | Type | Values | Default |
|---|---|---|---|
| `checked` | boolean |  | `false` |

## States

`Default` · `Hover` · `Pressed` · `Focus` · `Disabled`

## Anatomy

- `root`: A `<button role="switch">` holding the track. The browser's own look goes. It has no words of its own: SwitchField puts a label beside it, or give it an `aria-label`.
- `track`: The pill. 40 by 24, the size of a Toggle across and a Chip down.
- `background`: The track's fill, on its own layer. Off, it's faded the way an off Toggle's plate is, so the thumb on top stays solid. The edge is the track's.
- `thumb`: The 16px dot, 4px in from the track's edge. It sits left when off and right when on. It jumps for now: there are no motion tokens yet, and when there are it should slide.
- `focus-ring`: Around the track, keeping the pill's shape.

## When to use

- **A setting that works at once.** A setting that's on or off and works the moment you flip it: email me receipts, show cents, dark charts.

## When not to use

- **A choice that waits for Save.** A choice that waits for a Save button. That's a checkbox, and Rost doesn't have one yet.
- **Filtering the screen.** Filtering what's on screen. That's **Chip**, with `selected`.
- **A button that stays pressed.** An icon button that stays pressed, like bold in an editor. That's **Toggle**.

## Behaviour

Off is a faded track with the thumb on the left, the way an off Toggle is faded. On is a solid primary green track with a dark thumb on the right. Hover, press and focus come from the browser and follow Toggle's colours. Disabled fades it.

I built it in code from Toggle's and Button's tokens, then drew it in the kit, on a page of its own, bound to the same variables. Its label moved to SwitchField on 2026-10-31. The ten variants match Figma.

## Accessibility

- **Element**: `<button type="button" role="switch">` with `aria-checked`. Switch sets both.
- **Name**: It has no text of its own. In a SwitchField the label names it; on its own it needs an `aria-label`.
- **Keyboard**: Tab reaches it. Space flips it, and so does Enter, since it's a button.
- **Focus**: `focus-ring` around the track, on `:focus-visible`.
- **Colour**: On and off aren't only green and grey: the thumb moves from one end to the other.

## Content

The words go in SwitchField's label, or in `aria-label`. Say what's on when it's on, as a short phrase in sentence case: "Email me receipts", not "Receipts: on/off".

## Tokens

Every token this component uses, across all its variants.

| Token | Resolves to |
|---|---|
| `opacity/gentle` | `0.24` |
| `opacity/mild` | `0.32` |
| `opacity/solid` | `1` |
| `spacing/gap/2xsmall` | `4px` |
| `spacing/gap/none` | `0px` |
| `spacing/outline/medium` | `2px` |
| `spacing/outline/small` | `1px` |
| `spacing/radius/full` | `10000px` |
| `spacing/size/2xsmall` | `16px` |
| `spacing/size/medium` | `40px` |
| `spacing/size/small` | `24px` |
| `switch/off/default/thumb` | `#d3d9d5` |
| `switch/off/default/track` | `#3a3a3a` |
| `switch/off/disabled/thumb` | `#d3d9d5` |
| `switch/off/disabled/track` | `#3a3a3a` |
| `switch/off/edge` | `#747573` |
| `switch/off/focus/focus-ring` | `#999` |
| `switch/off/focus/thumb` | `#d3d9d5` |
| `switch/off/focus/track` | `#3a3a3a` |
| `switch/off/hover/thumb` | `#ededed` |
| `switch/off/hover/track` | `#555555` |
| `switch/off/pressed/thumb` | `#d3d9d5` |
| `switch/off/pressed/track` | `#282828` |
| `switch/on/default/thumb` | `#060805` |
| `switch/on/default/track` | `#00d669` |
| `switch/on/disabled/thumb` | `#060805` |
| `switch/on/disabled/track` | `#00d669` |
| `switch/on/edge` | `#00d669` |
| `switch/on/focus/focus-ring` | `#b6ffda` |
| `switch/on/focus/thumb` | `#060805` |
| `switch/on/focus/track` | `#00d669` |
| `switch/on/hover/thumb` | `#060805` |
| `switch/on/hover/track` | `#00b450` |
| `switch/on/pressed/thumb` | `#060805` |
| `switch/on/pressed/track` | `#00913a` |

## No token, on purpose

| Part | Property | Value | Why |
|---|---|---|---|
| `track` | `outline-offset` | `calc(-1 * var(--spacing-outline-small))` | Composed from a token: the edge sits inside the track. |
| `focus-ring` | `inset` | `calc(-1 * var(--spacing-gap-2xsmall))` | Composed from a token: the ring sits spacing/gap/2xsmall outside the track. |
| `thumb` | `left` | `calc(100% - var(--spacing-size-2xsmall) - var(--spacing-gap-2xsmall))` | Composed from tokens: the far end of the track, less the thumb and its 4px. |

See it live, with every value of every variant: [rost.build/components/switch/](https://rost.build/components/switch/)
