# Switch

A native checkbox exposed as an immediate on or off setting. (CSS only.)

Reference: https://timeless.build/docs/components/switch/

> **Choosing between components.** Use Switch for a setting that applies immediately. Use [Toggle](/docs/components/toggle/) when the control is a button whose pressed state is part of a toolbar.

## Markup

```html
<label class="ui-choice" for="dark-mode">
  <input class="ui-switch" id="dark-mode" name="dark-mode" type="checkbox" role="switch" checked aria-describedby="dark-mode-description">
  <span data-ui-part="body">
    <span data-ui-part="title">Dark mode</span>
    <span id="dark-mode-description" data-ui-part="description">Follow the documentation theme.</span>
  </span>
</label>
```

## Install

```js
import '@timelessui/components/css/tokens.css'
import '@timelessui/components/css/core/forms.css'
import '@timelessui/components/css/themes/atmosphere/tokens.css'
import '@timelessui/components/css/themes/atmosphere/forms.css'
```

## Anatomy

Parts are authored in your own markup and identified by the selector below. Required parts
must be present for the component to work. Private `data-ui-internal-*` hooks are written by
the runtime and must never be authored.

**ui-choice**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `body` | No | `[data-ui-part~='body']` | Wrapper for the title and description beside the control. |
| `title` | No | `[data-ui-part~='title']` | The choice label text. |
| `description` | No | `[data-ui-part~='description']` | Supporting copy under the choice label. |

## Attributes

Every value below is implemented by the stylesheets this component ships. Boolean attributes
are presence-based: author the attribute with no value, or omit it.

**ui-choice**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `data-ui-density` | `compact` · `normal` · `spacious` | `normal` | Gap between the control and its label. |

## State

Native attributes, ARIA, and platform pseudo-classes are authoritative. Style state through
the selectors below rather than adding your own state classes.

| State | Source | How to express it |
| --- | --- | --- |
| `invalid` | native | Native `:invalid`, or `aria-invalid="true"`. |
| `disabled` | native | Native `disabled`. |

## Styling

Root identity: `ui-switch`, `ui-choice`.
Required stylesheets: `tokens.css`, `core/forms.css`, `themes/atmosphere/tokens.css`, `themes/atmosphere/forms.css`.

This component adds no custom properties of its own. Restyle it through the design tokens at https://timeless.build/docs/styling/theming/ or your own CSS.

Design tokens this component reads (26), global and set at the theme level: `--ui-accent`, `--ui-bg-control`, `--ui-bg-control-muted`, `--ui-bg-surface`, `--ui-bg-surface-subtle`, `--ui-danger`, `--ui-danger-soft`, `--ui-disabled-opacity`, `--ui-duration-fast`, `--ui-ease-standard`, `--ui-fg`, `--ui-fg-muted`, `--ui-fg-subtle`, `--ui-focus`, `--ui-line`, `--ui-line-strong`, `--ui-radius-md`, `--ui-radius-pill`, `--ui-radius-sm`, `--ui-shadow-control`, `--ui-shadow-inset`, `--ui-space-1`, `--ui-space-2`, `--ui-space-3`, `--ui-space-4`, `--ui-space-5`.

## Accessibility

Keep the native elements, roles, and relationships shown in the markup. Timeless adds only the
state and keyboard coordination the platform does not already provide, and it never supplies
your accessible names — those depend on your content.

Follows the [Switch pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/) from the ARIA Authoring Practices Guide.

A native `input[type="checkbox"]` with `role="switch"`, which is the whole component: Tab reaches it, Space toggles it, the checked state is `:checked`, and it submits and resets with the form. `role="switch"` changes only how it is announced — on or off rather than checked or unchecked — so use it when the control takes effect immediately and leave it off when the change needs saving. Label it as you would any checkbox, and do not add `aria-checked`; the native state is authoritative and the two can disagree.

## Before JavaScript runs

This primitive is CSS only. There is nothing to register and nothing to wait for.
