# Toggle

Keep a button pressed, or group toggles with a shared value and arrow-key navigation.

Use a toggle for a button that stays on until you press it again, like starring a document or turning on Bold. Put several in a toggle group to share one value, as a text editor's formatting buttons or a view switcher do.

```angular-ts title="basic.ts"
import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Toggle, ToggleIcon } from "@ui-sdk/angular/toggle"

@Component({
  imports: [Toggle, ToggleIcon, LucideStar],
  template: `
    <div data-slot="card">
      <div data-slot="stack">
        <h3>Q3 Roadmap</h3>
        <p data-slot="hint">Edited by Hollis Grant</p>
      </div>
      <button uiToggle aria-label="Star">
        <span uiToggleIcon data-variant="fill">
          <svg lucideStar></svg>
        </span>
      </button>
    </div>
  `,
})
class Example {}

export { Example }
```

Press the star beside Q3 Roadmap, or focus it and press Enter or Space. It fills in while pressed, and its accessible name stays Star.

## Installation

```bash
npm install @ui-sdk/angular @ui-sdk/core
```

## Composition

Use the following composition to build Toggle:

```text
uiToggle
└── uiToggleIcon
```

And a `uiToggleGroup`:

```text
ToggleGroup
├── Toggle
│   └── ToggleIcon
└── Toggle
    └── ToggleIcon
```

`uiToggle` goes on a native button that reports its state with `aria-pressed`. `uiToggleIcon` is optional: it holds an icon and hides it from screen readers. In a group, give each toggle a `value`; the group's value lists the pressed ones.

## Usage

Import the parts and put them together:

```ts
import { Component } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import { Toggle, ToggleIcon } from "@ui-sdk/angular/toggle"

@Component({
  selector: "app-example",
  imports: [Toggle, ToggleIcon, LucideStar],
  template: `
    <button uiToggle aria-label="Star">
      <span uiToggleIcon>
        <svg lucideStar></svg>
      </span>
    </button>
  `,
})
class Example {}

export { Example }
```

Keep the label the same in both states: screen readers announce whether the button is pressed. An icon-only toggle needs an `aria-label`, as above. Set `defaultPressed` to start pressed. The toggle always uses `type="button"`, so pressing it never submits a form.

## Group

Put toggles in a `uiToggleGroup`, from the same `@ui-sdk/angular/toggle` entry, to share a value and arrow-key navigation. Give the group an accessible name and each toggle a `value`. Set `multiple` to let several toggles stay pressed.

```angular-ts title="group.ts"
import { Component } from "@angular/core"
import { LucideBold, LucideItalic, LucideUnderline } from "@lucide/angular"
import { Toggle, ToggleGroup, ToggleIcon } from "@ui-sdk/angular/toggle"

@Component({
  imports: [
    Toggle,
    ToggleGroup,
    ToggleIcon,
    LucideBold,
    LucideItalic,
    LucideUnderline,
  ],
  template: `
    <div uiToggleGroup multiple aria-label="Text formatting">
      <button uiToggle value="bold" aria-label="Bold">
        <span uiToggleIcon>
          <svg lucideBold></svg>
        </span>
      </button>
      <button uiToggle value="italic" aria-label="Italic">
        <span uiToggleIcon>
          <svg lucideItalic></svg>
        </span>
      </button>
      <button uiToggle value="underline" aria-label="Underline">
        <span uiToggleIcon>
          <svg lucideUnderline></svg>
        </span>
      </button>
    </div>
  `,
})
class Example {}

export { Example }
```

Press Bold and Italic to keep both pressed. Left and Right move focus without changing either.

The group owns which toggles are pressed, so a toggle's own `pressed` and `defaultPressed` don't apply inside it. Its value is always an array, in the toggles' document order.

## Single Selection

Without `multiple`, pressing a toggle unpresses the others. Pressing the pressed toggle again clears the selection, unless you set `deselectable` to `false`, as this alignment group does. `deselectable` doesn't choose an initial value for you, so pair it with `defaultValue`. It only limits what people can do: the controller's `setValue([])` still clears the group.

```angular-ts title="group-single.ts"
import { Component } from "@angular/core"
import {
  LucideDynamicIcon,
  LucideTextAlignCenter,
  LucideTextAlignEnd,
  LucideTextAlignStart,
} from "@lucide/angular"
import { Toggle, ToggleGroup, ToggleIcon } from "@ui-sdk/angular/toggle"

@Component({
  imports: [Toggle, ToggleGroup, ToggleIcon, LucideDynamicIcon],
  template: `
    <div
      uiToggleGroup
      [defaultValue]="['start']"
      [deselectable]="false"
      aria-label="Text alignment"
    >
      @for (item of alignments; track item.value) {
        <button uiToggle [value]="item.value" [aria-label]="item.label">
          <span uiToggleIcon [mirror]="item.mirror">
            <svg [lucideIcon]="item.icon"></svg>
          </span>
        </button>
      }
    </div>
  `,
})
class Example {
  readonly alignments = [
    {
      value: "start",
      label: "Align start",
      icon: LucideTextAlignStart,
      mirror: true,
    },
    {
      value: "center",
      label: "Align center",
      icon: LucideTextAlignCenter,
      mirror: false,
    },
    {
      value: "end",
      label: "Align end",
      icon: LucideTextAlignEnd,
      mirror: true,
    },
  ]
}

export { Example }
```

Press Align center to replace Align start, then press Align center again. One alignment always stays pressed.

`mirror` on Align start and Align end flips their icons in right-to-left text. Use [Radio](/docs/angular/components/radio) instead when the arrow keys should select an option as they move; a toggle group moves focus first and waits for a press.

## Icon and Label

Put an icon beside the label with `uiToggleIcon`. The icon is hidden from screen readers, so the text alone names the button.

```angular-html
<button uiToggle>
  <span uiToggleIcon>
    <svg lucidePin></svg>
  </span>
  Pin
</button>
```

## Disabled

Set `disabled` on a toggle or a group to prevent presses. A disabled toggle can still start pressed, and a disabled `<fieldset>` around it disables it too.

```angular-html
<button uiToggle disabled defaultPressed>Pin</button>
```

A disabled toggle on its own leaves the tab order. In a group, disabled toggles stay reachable with the arrow keys and report `aria-disabled`, but ignore presses.

```angular-html
<div uiToggleGroup multiple aria-label="Text formatting">
  <button uiToggle value="bold">Bold</button>
  <button uiToggle value="italic">Italic</button>
  <button uiToggle value="underline" disabled>Underline</button>
</div>
```

## Focusable When Disabled

Set `focusableWhenDisabled` to keep a disabled toggle focusable. It uses `aria-disabled` and still ignores presses. Use `aria-describedby` to explain why it's unavailable:

```angular-html
<button uiToggle disabled focusableWhenDisabled aria-describedby="pin-hint">
  Pin
</button>
<p id="pin-hint">Only owners can pin messages.</p>
```

## Controlled

Bind `[(pressed)]`, or pass `pressed` and handle `(beforePressedChange)`, to own the state. Call `$event.cancel()` in `(beforePressedChange)` to keep the current state. A [controller](#toggle-controller) from `createToggleController()` presses the toggle from anywhere, such as a keyboard shortcut. Create one controller per mounted toggle.

```ts
import { Component, signal } from "@angular/core"
import { LucideStar } from "@lucide/angular"
import {
  createToggleController,
  Toggle,
  ToggleIcon,
} from "@ui-sdk/angular/toggle"

@Component({
  selector: "app-example",
  imports: [Toggle, ToggleIcon, LucideStar],
  template: `
    <button
      uiToggle
      aria-label="Star"
      [(pressed)]="starred"
      [controller]="star"
    >
      <span uiToggleIcon>
        <svg lucideStar></svg>
      </span>
    </button>
    <button type="button" (click)="star.toggle()">Toggle Star</button>
  `,
})
class Example {
  readonly starred = signal(false)

  readonly star = createToggleController()
}

export { Example }
```

`(beforePressedChange)` receives the new state and `details`, whose `reason` is `"press"` or `"imperative"`.

## Controlled Group

Bind `[(value)]`, or pass `value` and handle `(beforeValueChange)`, to own the group's value, or to show it somewhere else. A toggle's own `(beforePressedChange)` runs before the group's, and calling `$event.cancel()` in either cancels the change. A [controller](#togglegroup-controller) from `createToggleGroupController()` changes the value from anywhere: `setValue([])` clears it.

```ts
import { Component, signal } from "@angular/core"
import {
  createToggleGroupController,
  Toggle,
  ToggleGroup,
} from "@ui-sdk/angular/toggle"

@Component({
  selector: "app-example",
  imports: [Toggle, ToggleGroup],
  template: `
    <div
      uiToggleGroup
      multiple
      aria-label="Text formatting"
      [(value)]="formats"
      [controller]="editor"
    >
      <button uiToggle value="bold">Bold</button>
      <button uiToggle value="italic">Italic</button>
    </div>
    <button type="button" (click)="editor.setValue([])">
      Clear Formatting
    </button>
  `,
})
class Example {
  readonly formats = signal(["bold"])

  readonly editor = createToggleGroupController()
}

export { Example }
```

See [State and Events](/docs/angular/state-and-events) for controlled state, cancellation and debugging.

## Vertical

Set `orientation="vertical"` to move with Up and Down instead of Left and Right. The group and its toggles get `data-orientation`.

```angular-html
<div uiToggleGroup orientation="vertical" aria-label="Tool">...</div>
```

## Loop

Arrow keys wrap from the last toggle to the first. Set `loop` to `false` to stop at the ends.

```angular-html
<div uiToggleGroup multiple [loop]="false" aria-label="Text formatting">
  ...
</div>
```

## Forms

Toggles don't submit values. Use [Checkbox](/docs/angular/components/checkbox) for several form choices or [Radio](/docs/angular/components/radio) for one. If you keep a toggle group in a form, copy its value into hidden inputs yourself.

## Custom Rendering

Each directive goes on an element you write. Read its state from a template reference: with `#favorite="uiToggle"`, `favorite.state().pressed` says whether it's pressed, and a group's `state().value` holds its pressed values. See [Parts and Composition](/docs/angular/parts-and-composition) for more.

```angular-ts title="render.ts"
import { Component } from "@angular/core"
import { LucideHeart } from "@lucide/angular"
import { Toggle, ToggleIcon } from "@ui-sdk/angular/toggle"

@Component({
  imports: [Toggle, ToggleIcon, LucideHeart],
  template: `
    <button uiToggle #favorite="uiToggle" aria-label="Favorite">
      <span uiToggleIcon>
        <svg
          lucideHeart
          [attr.fill]="favorite.state().pressed ? 'currentColor' : 'none'"
        ></svg>
      </span>
    </button>
  `,
})
class Example {}

export { Example }
```

Press Favorite. The heart renders filled or empty from the toggle's pressed state.

## Styling

Style the pressed state with `data-pressed`, and use `data-disabled` for unavailable toggles. A toggle with a `uiToggleIcon` gets `data-has-icon`, and its group gets `data-has-icons` while any toggle has one, which helps align icon and text content. `data-orientation` tells a group's toggles which way it runs.

Each part has a `data-slot`, such as `toggle`, and its state attributes are listed under [Data Attributes](#data-attributes).

## Data Attributes

### Toggle
`[data-slot="toggle"]`

- `data-pressed`: Present while pressed.
- `data-disabled`: Present when disabled, by its own prop or its group.
- `data-orientation` (`horizontal`, `vertical`): Inside a group, the group's orientation.
- `data-has-icon`: Present while the toggle holds an icon part.

### ToggleIcon
`[data-slot="toggle-icon"]`

- `data-mirror`: Present on an icon that flips in right-to-left text, with `mirror`.

### ToggleGroup
`[data-slot="toggle-group"]`

- `data-orientation` (`horizontal`, `vertical`): Which arrow keys move between toggles.
- `data-disabled`: Present when the whole group is disabled.
- `data-has-icons`: Present while any toggle inside holds an icon part, so toggles without one can line up.

## API Reference

Each option below is an input on the directive, and each callback an output. A change callback is two outputs: `(beforePressedChange)`, with the details and `cancel()`, then `(pressedChange)` with the new value alone. `pressed`, and a group's `value`, can be bound with `[(pressed)]` and `[(value)]`.

### Toggle

- `pressed?` (`boolean`): Whether it's pressed. Pass it to control the toggle. Inside a group, the group's value decides.
- `defaultPressed?` (`boolean`): Whether it's pressed at first, when uncontrolled.
- `(beforePressedChange)`: Runs when a press wants to change it. `details.reason` says why, and `details.cancel()` stops it. Inside a group, it runs before the group's `(beforeValueChange)`, and canceling it cancels the group's change too.
- `value?` (`string`): Identifies the toggle inside a group, whose value lists the pressed toggles' values. Defaults to a generated id.
- `disabled?` (`boolean`): Stops presses. Alone, the toggle is natively disabled, unless `focusableWhenDisabled` is set. Inside a group or a toolbar, it stays focusable with `aria-disabled`, so arrow keys still reach it.
- `focusableWhenDisabled?` (`boolean`): Keeps a disabled toggle in the tab order with `aria-disabled` instead of the native attribute. Toggles in a group or a toolbar always are.
- `controller?` (`{ isPressed: () => boolean; setPressed: (pressed: boolean, options?: ToggleChangeOptions) => boolean; toggle: (options?: ToggleChangeOptions) => boolean; focus: () => void; }`): Presses it from anywhere. Create one with `createToggleController()`.
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.

### ToggleIcon

- `mirror?` (`boolean`): Flips the icon horizontally in right-to-left text, for icons that point a way, like arrows and chevrons. Icons that picture a thing, like a clock or a check, stay as they are.

### ToggleGroup

- `value?` (`T[]`): The pressed toggles' values. Pass it to control the group.
- `defaultValue?` (`T[]`): The toggles pressed at first, when uncontrolled.
- `(beforeValueChange)`: Runs when a toggle wants to be pressed or unpressed. `details.reason` says why, and `details.cancel()` stops it.
- `multiple?` (`boolean`): Whether several toggles can be pressed at once. Otherwise pressing one unpresses the others.
- `deselectable?` (`boolean`): Whether the pressed toggle can be unpressed when only one may be pressed. With `false`, one toggle stays pressed once any is, though `setValue([])` still clears it. Ignored with `multiple`.
- `disabled?` (`boolean`): Stops every toggle from being pressed. They stay focusable with `aria-disabled`, so the group keeps its tab stop and stays discoverable.
- `orientation?` (`ToggleGroupOrientation`): Which arrow keys move between toggles: left and right, or up and down.
- `loop?` (`boolean`): Whether arrow keys wrap from the last toggle to the first.
- `controller?` (`object`): Presses toggles from anywhere. Create one with `createToggleGroupController()`.
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.

### State

What each part's `state()` signal holds.

- `pressed` (`boolean`): Whether it's pressed. Inside a group, it follows the group's value.
- `disabled` (`boolean`): Whether it's disabled, by itself, its group or a fieldset around it.

In a group, a toggle's `disabled` includes the group's. The group:

- `value` (`T[]`): The pressed toggles' values.
- `multiple` (`boolean`): Whether several toggles can be pressed at once.
- `disabled` (`boolean`): Whether the whole group is disabled, by its `disabled` option or a disabled fieldset around it.
- `orientation` (`ToggleGroupOrientation`): The group's `orientation` option, or `"horizontal"` when it's unset or invalid.

`uiToggleIcon`:

- `mirror` (`boolean`): Whether it flips in right-to-left text.

### Reasons

`$event.reason` says why the toggle was pressed or unpressed.

- `press` (`MouseEvent | KeyboardEvent`): A single-element control was pressed: a checkbox, a switch or a toggle.
- `imperative` (`none`): A controller or `api` call.

For a group, it says why the pressed toggles changed.

- `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
- `imperative` (`none`): A controller or `api` call.

### Toggle Controller

Create one with `createToggleController()` and pass it as `controller`.

Every write method takes an optional `{ reason, event }`, runs the change callback like a press, and returns `false` when the change is canceled.

- `isPressed()` (`() => boolean`): Whether it's pressed. Inside a group, it follows the group's value.
- `setPressed(pressed, options?)` (`(pressed: boolean, options?: ToggleChangeOptions | undefined) => boolean`): Presses or unpresses it, as a user would. A disabled toggle ignores it. Returns false when canceled.
- `toggle(options?)` (`(options?: ToggleChangeOptions | undefined) => boolean`): Presses it, or unpresses it.
- `focus()` (`() => void`): Moves focus to it.

### ToggleGroup Controller

Create one with `createToggleGroupController()` and pass it as `controller`.

Every write method takes an optional `{ reason, event }`, runs `(beforeValueChange)` like a press, and returns `false` when the change is canceled.

- `getValue()` (`() => T[]`): The pressed toggles' values, in the order they appear.
- `setValue(value, options?)` (`(value: T[], options?: ToggleGroupChangeOptions | undefined) => boolean`): Sets the pressed toggles. Only the first counts unless `multiple` is set. Returns false when canceled.
- `isPressed(value)` (`(value: T) => boolean`): Whether a toggle is pressed.
- `toggle(value, options?)` (`(value: T, options?: ToggleGroupChangeOptions | undefined) => boolean`): Presses a toggle, or unpresses a pressed one, as a user would. Disabled toggles and `deselectable={false}` refuse it.
- `focus(value?)` (`(value?: T | undefined) => void`): Moves focus to a toggle, or to the one that holds the group's tab stop.

### Events

A `before` output emits `{ value, reason, event, cancel() }` before the change, and its plain output follows with the value alone. `(beforeValueChange)` never runs on mount. A bound `pressed` or `value` only updates when the callback didn't cancel the change.

| Binding                 | On              | Receives                                                                                                                              |
| ----------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `[(pressed)]`           | `uiToggle`      | Whether the toggle is pressed                                                                                                         |
| `(beforePressedChange)` | `uiToggle`      | `{ value, reason, cancel() }`: the new state, and the [reason](#reasons). In a group, it runs first and can cancel the group's change |
| `[(value)]`             | `uiToggleGroup` | The pressed toggles' values                                                                                                           |
| `(beforeValueChange)`   | `uiToggleGroup` | `{ value, reason, cancel() }`: the pressed toggles' values, and the [reason](#reasons)                                                |

## Accessibility

A toggle follows the toggle button in the [WAI-ARIA button pattern](https://www.w3.org/WAI/ARIA/apg/patterns/button/): a native button with `aria-pressed`. A group follows the keyboard of the [WAI-ARIA toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/), with one tab stop and arrow keys between toggles. It has the `group` role, not `toolbar`, so it can sit inside a [Toolbar](/docs/angular/components/toolbar), and not `radiogroup`, since its toggles are buttons.

### Keyboard

A group is one tab stop. Tab first enters on its first enabled pressed toggle, or its first enabled toggle, and later returns to the last focused one.

| Key          | Does                                                    |
| ------------ | ------------------------------------------------------- |
| Enter, Space | Presses or unpresses the focused toggle                 |
| Tab          | Moves into or out of a group                            |
| Left, Right  | Move focus in a horizontal group, swapped right to left |
| Up, Down     | Move focus in a vertical group                          |
| Home, End    | Move focus to the first or last toggle                  |

### Tested Behavior

Every line below is an automated test.

- A toggle starts unpressed.
- Clicking a toggle presses it, and clicking again unpresses it.
- Space presses a focused toggle.
- Enter presses a focused toggle.
- A toggle can start pressed.
- A change can be cancelled.
- A toggle keeps its name when it's pressed.

Runs in React, Vue and web components.

In a group:

**One pressed at a time**

- The group is named and its default toggle is pressed.
- Pressing a toggle unpresses the others.
- Pressing the pressed toggle unpresses it.
- A change can be cancelled.

**Several pressed at once**

- Pressing toggles adds them to the value.
- A disabled toggle can't be pressed.
- A disabled group presses nothing.

**Keyboard**

- Tab moves into the group once, and out of it.
- Only one toggle in a group is a tab stop.
- ArrowRight and ArrowLeft move focus without pressing.
- Focus wraps at both ends.
- Home and End move to the first and last toggles.
- ArrowUp and ArrowDown do nothing in a horizontal group.
- Space presses the focused toggle.
- Enter presses the focused toggle.
- Tab returns to the toggle focused last.
- The group itself is never a tab stop.
- Keys pressed without waiting still land.
- Arrow keys with a modifier are left to the browser.

**Options**

- With loop off, arrows stop at the ends.
- A vertical group moves with ArrowDown and ArrowUp.
- The group carries its orientation.

Runs in React, Vue and web components.
