<!-- index: /slotted/llms.txt -->
# toggle

`data-ui="toggle"` · Button that stays pressed or unpressed.

## Import

```tsx
import { Toggle, ToggleDefaultsProvider } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Glyph } from '@slotted/react/icon';
import { Toggle } from '@slotted/react/toggle';

export default function ToggleBasic() {
  return <Toggle icon={<Glyph name="pin" />}>Pin header</Toggle>;
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | button, or the element render returns | yes |
| `icon` | span | no |
| `label` | span | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `pressed` | `boolean` | — | Toggle | Present means the consumer owns the state, for the life of the component. Absent, the toggle owns it and starts from defaultPressed. |
| `defaultPressed` | `boolean` | false | Toggle | What an uncontrolled toggle starts from, read once. A later change to it does not silently become new state. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | Toggle | The control step. It reads the control scale, so the toggle is as tall as the field beside it. |
| `variant` | `'outline' \| 'ghost' \| 'chips'` | outline | Toggle | outline draws its own edge, ghost is bare until the toggle is on, and chips is a rounded item that presses in the neutral ink. There is no tone axis: a mode is one color when it is on, and an action that persists, with a tone, is button’s ToggleButton. |
| `icon` | `ReactNode` | — | Toggle | The mark drawn before the words, sized from the control glyph scale and hidden from the reading order. Alone, it makes the toggle square. |
| `children` | `ReactNode` | — | Toggle | The words, which never change with the state. Absent, the toggle needs an aria-label. |
| `disabled` | `boolean` | false | Toggle | The native attribute, which is also what makes the browser refuse focus. A toggle that is on and unavailable keeps saying both. |
| `type` | `'button' \| 'submit' \| 'reset'` | button | Toggle | The native type. A toggle in a form is not a form control — checkbox and switch are — so it is a button unless the consumer says otherwise. |
| `render` | `(props: ToggleRootProps) => ReactNode` | — | Toggle | Substitutes the root and receives the data attributes, aria-pressed, className, style and the native props, with type, disabled and ref. It stays a button. |
| `ref` | `Ref<HTMLButtonElement>` | — | Toggle | Reaches the element. focus() and click() are the element’s own, so no imperative handle is invented for what the platform already ships. |
| `ToggleDefaultsProvider` | `{ size, variant }` | — | a subtree | The two decisions an application takes once. A toggle that states either itself still wins. |
| `name, value, form, aria-*` | `the button’s own attributes` | — | Toggle | Every native attribute reaches the button unchanged and means what a button’s does. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onPressedChange` | `(pressed: boolean, request: ChangeRequest<boolean>)` | Before the state moves. The request carries the new value, the value it replaces, the reason — pointer or keyboard — the originating event, and cancel(), which refuses the move before anything commits. Nothing is emitted for a value equal to the one the toggle already holds. |
| `onClick, onFocus, onBlur` | `the element’s own events` | Whenever the button fires them. Toggle composes with a consumer’s handler rather than replacing it: the consumer’s runs first, and always runs. Calling preventDefault() in onClick stops the toggle as well. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `variant` | `outline`, `ghost`, `chips` | `outline` |

## Rules

- The APG Button pattern’s toggle button (w3.org/WAI/ARIA/apg/patterns/button/): a native button with aria-pressed, and no navigation pattern around it — a single control has no arrows of its own.
- aria-pressed is written in both states, so the control is announced as a toggle rather than as a button that happens to be pressed — and the label never changes with the state, which the pattern calls critical. A toggle with only a mark takes its name from an aria-label the consumer writes.
- Inside a toolbar the bar owns the tab stop and the arrows and reaches this control as it reaches every focusable control it holds; the toggle writes no listener. Space and Enter press it, from the platform.
- disabled is the native attribute, because that is also what makes the browser refuse focus and skip the control. The toggle is not a form control: a value a form submits is checkbox or switch.
- The target is the control itself, never narrower than it is tall, so the WCAG 2.2 minimum of 24 by 24 holds at the smallest step and the tightest density — and the theme grows every control to 44 under a coarse pointer.

## Tokens

`--slotted-control-border-color`, `--slotted-control-border-width`, `--slotted-control-font-family`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-font-weight`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-lg`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-letter-spacing`, `--slotted-control-line-height`, `--slotted-control-padding-inline-lg`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-radius`, `--slotted-control-shadow`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-background`, `--slotted-disabled-border`, `--slotted-disabled-foreground`, `--slotted-duration-instant`, `--slotted-focus-ring-color`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-weight-medium`, `--slotted-radius-full`, `--slotted-text`, `--slotted-toggle-active-background`, `--slotted-toggle-border-color`, `--slotted-toggle-chips-pressed-background`, `--slotted-toggle-chips-pressed-color`, `--slotted-toggle-chips-radius`, `--slotted-toggle-color`, `--slotted-toggle-disabled-border-color`, `--slotted-toggle-disabled-color`, `--slotted-toggle-disabled-pressed-background`, `--slotted-toggle-hover-background`, `--slotted-toggle-pressed-background`, `--slotted-toggle-pressed-border-color`, `--slotted-toggle-pressed-color`, `--slotted-toggle-pressed-hover-background`, `--slotted-toggle-radius`, `--slotted-toggle-shadow`, `--slotted-tone-accent-border`, `--slotted-tone-accent-subtle-active`, `--slotted-tone-accent-subtle-hover`, `--slotted-tone-accent-text`, `--slotted-tone-neutral-on-solid`, `--slotted-tone-neutral-solid`, `--slotted-tone-neutral-solid-hover`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`

## Examples

### Playground

One toggle with every axis exposed as a control below the canvas. Pressing it changes the state and leaves the label “Wrap lines” as written, so a screen reader announces the same name with a different pressed state.

Excerpt — not a complete module

```tsx
<Toggle icon={<WrapText size={16} />} defaultPressed>
  Wrap lines
</Toggle>;
```

### Owned by the consumer, or by the toggle

The left toggle owns its state and starts from defaultPressed. The right toggle is controlled: the application holds the value, the toggle emits a request that carries the reason, and the application decides. Both write aria-pressed the same way.

Excerpt — not a complete module

```tsx
const [wrap, setWrap] = useState(false);

<Toggle icon={<WrapText size={16} />} onPressedChange={setWrap} pressed={wrap}>
  Wrap lines
</Toggle>;
```

### Outline, ghost and chips

outline draws its own edge and stands beside a field. ghost is the toggle inside something already framed, such as a toolbar or a card header. chips is a rounded item that presses in the neutral ink, which suits a row of filters. outline and ghost turn on into the same accent pair, so “on” is one color wherever the control is.

Excerpt — not a complete module

```tsx
<Toggle icon={<Pin size={16} />} variant="ghost">
  Pin header
</Toggle>;
```

### Three steps, one control tall

Every step reads the control scale, so a toggle beside a text field of the same step has the same height as it, and the density switch in the toolbar moves both together.

Example

```tsx
import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Toggle } from '@slotted/react/toggle';

export default function ToggleSizes() {
  return (
    <Inline align="center" gap={2}>
      <Toggle icon={<Glyph name="text-wrap" />} size="sm">
        Dense rows
      </Toggle>
      <Toggle icon={<Glyph name="text-wrap" />} size="md">
        Default rows
      </Toggle>
      <Toggle icon={<Glyph name="text-wrap" />} size="lg">
        Roomy rows
      </Toggle>
    </Inline>
  );
}
```

### A mark and no words

Without children the toggle draws a square the height of the control, with the mark centered in it. The name comes from aria-label, which the type does not require and the application writes.

Excerpt — not a complete module

```tsx
<Toggle aria-label="Bold" icon={<Bold size={16} />} defaultPressed />;
```

### Every state the contract declares

Hover takes the neutral ground when off and the accent ground’s own hover step when on. Disabled keeps both facts of a toggle that is on: the ground says unavailable and the edge still says on, so a reader learns the mode from a control that cannot be changed.

Example

```tsx
import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Toggle } from '@slotted/react/toggle';

export default function ToggleStates() {
  return (
    <Inline gap={2}>
      <Toggle icon={<Glyph name="pin" />}>Pin header</Toggle>
      <Toggle defaultPressed icon={<Glyph name="bell" />}>
        Notify me
      </Toggle>
      <Toggle disabled icon={<Glyph name="star" />}>
        Starred only
      </Toggle>
      <Toggle defaultPressed disabled icon={<Glyph name="funnel" />}>
        Carrier filter
      </Toggle>
    </Inline>
  );
}
```

### Inside a toolbar

The bar is one tab stop with arrow navigation across every control it holds, and a toggle is one of those controls: Tab reaches the bar once, the arrows reach the toggle, Space presses it. Toggle writes no listener of its own.

Example

```tsx
import { Glyph } from '@slotted/react/icon';
import { Toggle } from '@slotted/react/toggle';
import { Toolbar, ToolbarSeparator } from '@slotted/react/toolbar';

export default function ToggleToolbar() {
  return (
    <Toolbar aria-label="Order table view">
      <Toggle defaultPressed icon={<Glyph name="pin" />} variant="ghost">
        Pin header
      </Toggle>
      <Toggle icon={<Glyph name="star" />} variant="ghost">
        Starred only
      </Toggle>
      <ToolbarSeparator />
      <Toggle icon={<Glyph name="text-wrap" />} variant="ghost">
        Wrap lines
      </Toggle>
    </Toolbar>
  );
}
```

### Content the application chooses

A long translated label in a narrow column clamps at the ellipsis and keeps its string whole in the DOM; a right-to-left document mirrors the mark to the other side of the words. Switch the direction in the toolbar to see the second.

Excerpt — not a complete module

```tsx
<Toggle icon={<WrapText size={16} />}>Show every line of the order notes</Toggle>;
```

### Defaults once, and the consumer’s own element

The provider sets size and variant for every toggle under it; a toggle that states either itself still wins. render hands the wiring to the consumer’s own button — it stays a button, because aria-pressed on a link is a link announcing a state a link does not hold.

Excerpt — not a complete module

```tsx
<ToggleDefaultsProvider size="sm" variant="ghost">
  <Toggle icon={<Pin size={14} />} render={(props) => <button {...props} data-track="pin" />}>
    Pin
  </Toggle>
</ToggleDefaultsProvider>;
```

### The keyboard, key by key

Tab reaches the control; Space and Enter turn it on and off; nothing else is bound. The request that reaches the consumer says keyboard rather than pointer, and the page below shows which one it saw.

Excerpt — not a complete module

```tsx
<Toggle icon={<WrapText size={16} />} onPressedChange={(pressed, request) => log(request.reason)}>
  Wrap lines
</Toggle>;
```

### Toggle, or ToggleButton

A mode is one color and true the moment it is pressed: Toggle. An action that persists — Follow, Subscribe — reaches a server, waits, and carries a tone beside the other actions of a page: button’s ToggleButton. Same state model, different meaning.

Excerpt — not a complete module

```tsx
<>
  <Toggle icon={<WrapText size={16} />}>Wrap lines</Toggle>
  <ToggleButton loading={following} onPressedChange={follow} pressed={followed}>
    Follow
  </ToggleButton>
</>;
```

### controlled.tsx

Example

```tsx
import { useState } from 'react';

import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Stack } from '@slotted/react/stack';
import { Toggle } from '@slotted/react/toggle';
import { Text } from '@slotted/react/typography';

export default function ToggleControlled() {
  const [wrap, setWrap] = useState(false);

  return (
    <Stack gap={2}>
      <Inline>
        <Toggle
          icon={<Glyph name="text-wrap" />}
          onPressedChange={(pressed) => setWrap(pressed)}
          pressed={wrap}
        >
          Wrap lines
        </Toggle>
      </Inline>
      <Text size="sm" tone="muted">
        {wrap ? 'Long order notes wrap onto further lines.' : 'Long order notes stay on one line.'}
      </Text>
    </Stack>
  );
}
```

### defaults.tsx

Example

```tsx
import { Inline } from '@slotted/react/inline';
import { Toggle, ToggleDefaultsProvider } from '@slotted/react/toggle';

export default function ToggleDefaults() {
  return (
    <ToggleDefaultsProvider size="sm" variant="chips">
      <Inline gap={2}>
        <Toggle defaultPressed>Late pickups</Toggle>
        <Toggle>Low stock</Toggle>
        <Toggle>Backordered</Toggle>
        <Toggle variant="ghost">Archived</Toggle>
      </Inline>
    </ToggleDefaultsProvider>
  );
}
```

### guard.tsx

Example

```tsx
import { useState } from 'react';

import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Stack } from '@slotted/react/stack';
import { Toggle } from '@slotted/react/toggle';
import { Text } from '@slotted/react/typography';

export default function ToggleGuard() {
  const [notice, setNotice] = useState('Locking freezes the entries of March 2026.');

  return (
    <Stack gap={2}>
      <Inline>
        <Toggle
          icon={<Glyph name="lock" />}
          onPressedChange={(pressed, request) => {
            if (pressed) {
              setNotice('March 2026 is locked.');
              return;
            }
            request.cancel();
            setNotice('A locked period is reopened by the finance approver.');
          }}
        >
          Lock period
        </Toggle>
      </Inline>
      <Text size="sm" tone="muted">
        {notice}
      </Text>
    </Stack>
  );
}
```

### icon-only.tsx

Example

```tsx
import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Toggle } from '@slotted/react/toggle';

export default function ToggleIconOnly() {
  return (
    <Inline gap={2}>
      <Toggle aria-label="Pin header" icon={<Glyph name="pin" />} />
      <Toggle aria-label="Starred only" defaultPressed icon={<Glyph name="star" />} />
      <Toggle aria-label="Notify me" icon={<Glyph name="bell" />} />
    </Inline>
  );
}
```

### reason.tsx

Example

```tsx
import { useState } from 'react';

import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Stack } from '@slotted/react/stack';
import { Toggle } from '@slotted/react/toggle';
import type { TogglePressedReason } from '@slotted/react/toggle';
import { Text } from '@slotted/react/typography';

export default function ToggleReason() {
  const [reason, setReason] = useState<TogglePressedReason | null>(null);

  return (
    <Stack gap={2}>
      <Inline>
        <Toggle
          icon={<Glyph name="rotate-cw" />}
          onPressedChange={(_pressed, request) => setReason(request.reason)}
        >
          Auto-refresh
        </Toggle>
      </Inline>
      <Text size="sm" tone="muted">
        {reason === null ? 'No change yet.' : `Last change: ${reason}.`}
      </Text>
    </Stack>
  );
}
```

### variants.tsx

Example

```tsx
import { Glyph } from '@slotted/react/icon';
import { Inline } from '@slotted/react/inline';
import { Stack } from '@slotted/react/stack';
import { Toggle } from '@slotted/react/toggle';

export default function ToggleVariants() {
  return (
    <Stack gap={3}>
      <Inline gap={2}>
        <Toggle defaultPressed icon={<Glyph name="pin" />} variant="outline">
          Pin header
        </Toggle>
        <Toggle icon={<Glyph name="text-wrap" />} variant="outline">
          Wrap lines
        </Toggle>
      </Inline>
      <Inline gap={2}>
        <Toggle defaultPressed icon={<Glyph name="bell" />} variant="ghost">
          Notify me
        </Toggle>
        <Toggle icon={<Glyph name="star" />} variant="ghost">
          Starred only
        </Toggle>
      </Inline>
      <Inline gap={2}>
        <Toggle defaultPressed variant="chips">
          Late pickups
        </Toggle>
        <Toggle variant="chips">Low stock</Toggle>
      </Inline>
    </Stack>
  );
}
```
