# Toggle

A button that turns a formatting option or view mode on or off. The selected state uses a soft accent fill.

**Dependencies:** npm: 2 direct, 3 total; registry: 2 direct, 2 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Turn one option on or off, such as pinning an item or applying emphasis.
- Keep a toolbar action selected until the user turns it off.

### When not to use

- An action with no persistent on state. Use Button.
- A setting submitted in a form. Use Switch.
- Related selection options. Use ToggleGroup.

### Native

**Formatting**

```text
WerewolfToggle(pressed: bold, onPressedChange: { bold = $0 }, label: Text("Bold"))
```

```text
WerewolfToggle(pressed = bold, onPressedChange = { bold = it }, label = { Text("Bold") })
```

**Selected initially**

```text
WerewolfToggle(defaultPressed: true, label: Text("Italic"))
```

```text
WerewolfToggle(defaultPressed = true, label = { Text("Italic") })
```

**Icon only**

```text
WerewolfToggle(defaultPressed: false, label: Image(systemName: "bold"), accessibilityLabel: "Bold", iconOnly: true)
```

```text
WerewolfToggle(defaultPressed = false, label = { Icon(boldIcon, contentDescription = null) }, accessibilityLabel = "Bold", iconOnly = true)
```

**Icon with text**

```text
WerewolfToggle(defaultPressed: false, label: Text("Bold"), icon: Image(systemName: "bold"))
```

```text
WerewolfToggle(defaultPressed = false, icon = { Icon(boldIcon, contentDescription = null) }, label = { Text("Bold") })
```

## Variants

### variant

Transparent when off, or a surface with a neutral border for outline.

Values: `default`, `outline`. Default: `default`.

### size

Control height, text and icon size, the same steps as Button: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Small controls keep a minimum hit area.

Values: `sm`, `md`, `lg`, `xl`. Default: `md`.

## Examples

### Formatting

```tsx
<Toggle>Bold</Toggle>
```

### Selected initially

```tsx
<Toggle variant="outline" defaultPressed>Grid view</Toggle>
```

### Icon only

```tsx
<Toggle iconOnly aria-label="Bold">
  <IconBold size={16} />
</Toggle>
```

### Disabled selection

```tsx
<Toggle disabled defaultPressed>Locked format</Toggle>
```

### Icon with text

An icon and a stable label stay together while the selection changes.

```tsx
<Toggle variant="outline" defaultPressed>
  <IconPin size={16} />
  Pin item
</Toggle>
```

### Disabled icon states

```tsx
<Toggle iconOnly disabled aria-label="Bold">
  <IconBold size={16} />
</Toggle>
<Toggle iconOnly disabled defaultPressed aria-label="Italic">
  <IconItalic size={16} />
</Toggle>
```

## Do and don't

### Do

- Use a stable label that names the option.
- Use iconOnly with aria-label for compact formatting controls.

### Don't

- Do not use a Toggle for an action that has no persistent state.
- Do not use it for a setting that applies at once; use Switch.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `variant` | `enum` | `default`, `outline` | `default` | Transparent when off, or a surface with a neutral border for outline. |
| `size` | `enum` | `sm`, `md`, `lg`, `xl` | `md` | Control height, text and icon size, the same steps as Button: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Small controls keep a minimum hit area. |
| `disabled` | `boolean` |  | `false` | Prevents focus and selection and uses disabled colors. |
| `iconOnly` | `boolean` |  | `false` | Makes the button square. Requires aria-label on an icon-only button. |
| `pressed` | `boolean` |  |  | Controlled on or off state. Pair with onPressedChange. |
| `defaultPressed` | `boolean` |  | `false` | Initial state when uncontrolled. |
| `onPressedChange` | `function` |  |  | Receives the requested boolean state after activation. |
| `label` | `node` |  |  | Native only: the content, text or an icon. With iconOnly, pass the icon and name the toggle with accessibilityLabel. Only on iOS, Android. |
| `icon` | `node` |  |  | Native only: an icon before the label. Only on iOS, Android. |
| `accessibilityLabel` | `string` |  | `` | Native only: the name read by VoiceOver and TalkBack when the toggle shows only an icon. Set it with iconOnly, as aria-label is on web. Only on iOS, Android. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The toggle button, transparent by default or a bordered surface in outline. On uses the soft accent pair. |
| `icon` | An optional decorative icon, 16px in sm, md and lg and 20px in xl. |
| `hitArea` | A transparent pseudo-element that extends the button target to size-target-min. |

## Accessibility

Role: `button`

| Keys | Action |
| --- | --- |
| `Space, Enter` | Toggles pressed state. |
| `Tab` | Moves focus to the next control. |

- Text children provide the accessible name. iconOnly requires aria-label.
- aria-pressed exposes the on or off state. Keep the label stable when state changes.
- The button defaults to type="button" and does not submit its form.
- Pass size and weight to every icon. Use regular off and optionally fill on.

## Install

```bash
npx werewolf-ui add toggle
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add toggle --platform ios
npx werewolf-ui add toggle --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
