# Toggle Button

## Usage

`ToggleButton` is a button with a pressed and an unpressed state. It stands for a lasting choice — bold text, a view mode, a favorite, a running recording — rather than a one-off action. It reuses the look, the sizes, and the async action machinery of `Button`, and adds the pressed state, the `aria-pressed` semantics, and the icon and weight shift that come with it.

On its own, the pressed state is bound to the component:

```vue
<script setup lang="ts">
import { ToggleButton } from '@astryx-vue/core/ToggleButton'
import { shallowRef } from 'vue'

const isPinned = shallowRef(false)
</script>

<template>
  <ToggleButton v-model:is-pressed="isPinned" label="Pin to top" />
</template>
```

A click emits `pressedChange` first, carrying the next state and the originating click event, and then writes `v-model:is-pressed`. Calling `event.preventDefault()` in that handler means you own the toggle: the component neither changes the state nor runs `pressedChangeAction`. With nothing bound at all, the button keeps the pressed state itself.

When the toggle has to wait for a request or a route, use `pressedChangeAction` instead of hand-written loading state. While the action is pending the button shows the next state and a spinner, and it stays clickable: a second click reverses the toggle that has not settled yet instead of being dropped.

```vue
<script setup lang="ts">
import { ToggleButton } from '@astryx-vue/core/ToggleButton'
import { shallowRef } from 'vue'

const isShared = shallowRef(false)

async function shareWithTeam(next: boolean) {
  await api.setShared(boardId, next)
}
</script>

<template>
  <ToggleButton
    v-model:is-pressed="isShared"
    label="Share with team"
    :pressed-change-action="shareWithTeam"
  />
</template>
```

Related toggles belong in a `ToggleButtonGroup`, so a screen reader announces them as one set:

```vue
<script setup lang="ts">
import { ToggleButton, ToggleButtonGroup } from '@astryx-vue/core/ToggleButton'
import { ref } from 'vue'

const view = ref<string | null | string[]>('board')
</script>

<template>
  <ToggleButtonGroup v-model:value="view" label="View mode">
    <ToggleButton value="list" label="List" />
    <ToggleButton value="board" label="Board" />
  </ToggleButtonGroup>
</template>
```

A group is single-select by default: one member is pressed, and clicking it again clears the selection (the value becomes `null`). With `type="multiple"` each member switches on its own, which suits filters and column pickers. The group size reaches every member unless the member sets its own; a disabled group disables all of them, while a member that disables itself stays disabled even in an enabled group. The group owns the pressed state of its members, so a member's own `isPressed`, `pressedChange` and `pressedChangeAction` are ignored.

For an icon-only toggle set `isIconOnly`: the button becomes square, `label` becomes the accessible name, and `tooltip` supplies the hover hint. To swap in a filled glyph while pressed, put it in the `pressedIcon` slot; the slot falls back to `icon` when it is empty.

### Anatomy

- **Icon**: A leading icon that represents the toggle action, like a star for favorite or bold "B" for formatting.
- **Pressed icon**: An alternate icon shown when pressed: typically a filled version of the default icon to reinforce the active state.
- **Label**: The visible text or accessible name. For icon-only toggles, used as the aria-label and auto-tooltip.
- **Spinner**: Replaces the icon during async operations triggered by pressedChangeAction.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Show the pressed state with a filled or colored icon, such as a microphone that becomes a solid square, so the current state is obvious at a glance. |
| Do | Keep the label text identical in both states and let the background, the weight, and the icon carry the difference. |
| Do | Wrap related toggles in a `ToggleButtonGroup` with a `label`, so a screen reader announces them as one set. |
| Do | Reach for `pressedChangeAction` when a request backs the toggle; the button then previews the next state and shows the spinner for you. |
| Don't | Use it for one-time actions such as "Submit" or "Delete"; those are plain `Button`s. |
| Don't | Mix regular `Button`s into a `ToggleButtonGroup`; a group holds `ToggleButton`s only. |
| Don't | Use it for a setting that persists across sessions; `Switch` reads as a setting, a toggle reads as an action on the current content. |

## Examples

### Color

Toggle buttons with colored icons in the pressed state. Shows accent-colored toolbar formatting and semantic reaction colors.

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { ToggleButton } from '@astryx-vue/core/ToggleButton'
import { reactive } from 'vue'

const toolbar = reactive({ bold: true, italic: false, underline: true, strikethrough: false, link: false })
const reactions = reactive({ star: false, heart: false, bookmark: true, bell: false })

function toggleToolbar(key: keyof typeof toolbar) {
  toolbar[key] = !toolbar[key]
}
function toggleReaction(key: keyof typeof reactions) {
  reactions[key] = !reactions[key]
}
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Toolbar
      </Text>
      <Stack direction="horizontal" :gap="1">
        <ToggleButton label="Bold" :is-pressed="toolbar.bold" is-icon-only @update:is-pressed="toggleToolbar('bold')">
          <template #icon>
            <Icon icon="i-heroicons-bold" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-bold" color="accent" />
          </template>
        </ToggleButton>
        <ToggleButton label="Italic" :is-pressed="toolbar.italic" is-icon-only @update:is-pressed="toggleToolbar('italic')">
          <template #icon>
            <Icon icon="i-heroicons-italic" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-italic" color="accent" />
          </template>
        </ToggleButton>
        <ToggleButton label="Underline" :is-pressed="toolbar.underline" is-icon-only @update:is-pressed="toggleToolbar('underline')">
          <template #icon>
            <Icon icon="i-heroicons-underline" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-underline" color="accent" />
          </template>
        </ToggleButton>
        <ToggleButton label="Strikethrough" :is-pressed="toolbar.strikethrough" is-icon-only @update:is-pressed="toggleToolbar('strikethrough')">
          <template #icon>
            <Icon icon="i-heroicons-strikethrough" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-strikethrough" color="accent" />
          </template>
        </ToggleButton>
        <ToggleButton label="Link" :is-pressed="toolbar.link" is-icon-only @update:is-pressed="toggleToolbar('link')">
          <template #icon>
            <Icon icon="i-heroicons-link" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-link" color="accent" />
          </template>
        </ToggleButton>
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Reactions
      </Text>
      <Stack direction="horizontal" :gap="1">
        <ToggleButton label="Star" :is-pressed="reactions.star" is-icon-only @update:is-pressed="toggleReaction('star')">
          <template #icon>
            <Icon icon="i-heroicons-star" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-star-solid" color="yellow" />
          </template>
        </ToggleButton>
        <ToggleButton label="Like" :is-pressed="reactions.heart" is-icon-only @update:is-pressed="toggleReaction('heart')">
          <template #icon>
            <Icon icon="i-heroicons-heart" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-heart-solid" color="red" />
          </template>
        </ToggleButton>
        <ToggleButton label="Save" :is-pressed="reactions.bookmark" is-icon-only @update:is-pressed="toggleReaction('bookmark')">
          <template #icon>
            <Icon icon="i-heroicons-bookmark" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-bookmark-solid" color="blue" />
          </template>
        </ToggleButton>
        <ToggleButton label="Follow" :is-pressed="reactions.bell" is-icon-only @update:is-pressed="toggleReaction('bell')">
          <template #icon>
            <Icon icon="i-heroicons-bell" color="secondary" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-bell-solid" color="accent" />
          </template>
        </ToggleButton>
      </Stack>
    </Stack>
  </Stack>
</template>
```

### Icon swap

Icon-only toggle buttons that swap between outline and solid icons when pressed. Use for actions like favorite, bookmark, or mute where the icon itself communicates the state.

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { ToggleButton } from '@astryx-vue/core/ToggleButton'
import { ref } from 'vue'

const isFavorited = ref(false)
const isBookmarked = ref(true)
const isMuted = ref(false)
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Text type="supporting" color="secondary">
      Outline → solid icon swap on press
    </Text>
    <Stack align="center" direction="horizontal" :gap="3">
      <ToggleButton label="Favorite" :is-pressed="isFavorited" is-icon-only @update:is-pressed="isFavorited = $event">
        <template #icon>
          <Icon icon="i-heroicons-star" />
        </template>
        <template #pressedIcon>
          <Icon icon="i-heroicons-star-solid" />
        </template>
      </ToggleButton>
      <ToggleButton label="Bookmark" :is-pressed="isBookmarked" is-icon-only @update:is-pressed="isBookmarked = $event">
        <template #icon>
          <Icon icon="i-heroicons-bookmark" />
        </template>
        <template #pressedIcon>
          <Icon icon="i-heroicons-bookmark-solid" />
        </template>
      </ToggleButton>
      <ToggleButton
        :label="isMuted ? 'Unmute notifications' : 'Mute notifications'"
        :is-pressed="isMuted"
        is-icon-only
        @update:is-pressed="isMuted = $event"
      >
        <template #icon>
          <Icon icon="i-heroicons-bell" />
        </template>
        <template #pressedIcon>
          <Icon icon="i-heroicons-bell-slash" />
        </template>
      </ToggleButton>
    </Stack>
  </Stack>
</template>
```

### Label

Toggle buttons with visible text labels that show a font weight shift on press. Use when the icon alone is not enough to communicate the action.

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { ToggleButton, ToggleButtonGroup } from '@astryx-vue/core/ToggleButton'
import { ref } from 'vue'

const isVisible = ref(true)
const filters = ref<string[]>([])
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Standalone with label and icon
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <ToggleButton
          label="Visible"
          :is-pressed="isVisible"
          @update:is-pressed="isVisible = $event"
        >
          <template #icon>
            <Icon :icon="isVisible ? 'i-heroicons-eye' : 'i-heroicons-eye-slash'" />
          </template>
          {{ isVisible ? 'Visible' : 'Hidden' }}
        </ToggleButton>
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Labeled group — filter toolbar
      </Text>
      <ToggleButtonGroup v-model:value="filters" type="multiple" label="Filters">
        <ToggleButton value="filter" label="Filter">
          <template #icon>
            <Icon icon="i-heroicons-funnel" />
          </template>
          Filter
        </ToggleButton>
        <ToggleButton value="nearby" label="Nearby">
          <template #icon>
            <Icon icon="i-heroicons-map-pin" />
          </template>
          Nearby
        </ToggleButton>
      </ToggleButtonGroup>
    </Stack>
  </Stack>
</template>
```

### States

Default, pressed, disabled, and loading states of a standalone toggle button. Shows how visual treatment changes across states.

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { Stack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
import { ToggleButton } from '@astryx-vue/core/ToggleButton'
</script>

<template>
  <Stack direction="vertical" :gap="4">
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Default
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <ToggleButton label="Favorite" :is-pressed="false">
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-star-solid" />
          </template>
        </ToggleButton>
        <ToggleButton label="Favorite" :is-pressed="false" is-icon-only>
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-star-solid" />
          </template>
        </ToggleButton>
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Pressed
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <ToggleButton label="Favorite" :is-pressed="true">
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-star-solid" />
          </template>
        </ToggleButton>
        <ToggleButton label="Favorite" :is-pressed="true" is-icon-only>
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
          <template #pressedIcon>
            <Icon icon="i-heroicons-star-solid" />
          </template>
        </ToggleButton>
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Disabled
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <ToggleButton label="Favorite" :is-pressed="false" is-disabled>
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
        </ToggleButton>
        <ToggleButton label="Favorite" :is-pressed="false" is-icon-only is-disabled>
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
        </ToggleButton>
      </Stack>
    </Stack>
    <Stack direction="vertical" :gap="1">
      <Text type="supporting" color="secondary">
        Loading
      </Text>
      <Stack align="center" direction="horizontal" :gap="3">
        <ToggleButton label="Favorite" :is-pressed="false" is-loading>
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
        </ToggleButton>
        <ToggleButton label="Favorite" :is-pressed="false" is-icon-only is-loading>
          <template #icon>
            <Icon icon="i-heroicons-star" />
          </template>
        </ToggleButton>
      </Stack>
    </Stack>
  </Stack>
</template>
```

## ToggleButton Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | required | Accessible name of the button. It names the control when the default slot supplies visible content, doubles as `aria-label` in icon-only mode, and is the visible text when nothing else is provided. |
| `isPressed` | `boolean` | `false` | Whether the button is pressed, bound with `v-model:is-pressed`. Leave it unbound and the button owns the pressed state, starting unpressed. Inside a ToggleButtonGroup the group owns it and this prop is ignored. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Button size. It falls back to the size of the enclosing group, then to `md`. |
| `elevation` | `'none' \| 'low' \| 'med' \| 'high'` | `'none'` | Resting shadow depth for floating (FAB-style) toggles, matching Button. A grouped button keeps its own elevation. |
| `isDisabled` | `boolean` | `false` | Disables interaction. A disabled group disables every member, and an enabled group never re-enables a member that disabled itself. A disabled button is marked `aria-disabled` instead of the native attribute, so it stays focusable. |
| `isLoading` | `boolean` | `false` | Shows the loading state: the content gives way to a spinner and `aria-busy` is set. |
| `isIconOnly` | `boolean` | `false` | Renders a square, icon-only button that uses label as its accessible name; no label text and no width reservation are rendered. |
| `pressedChangeAction` | `(isPressed: boolean) => void \| Promise<void>` | — | Action for an API- or navigation-backed toggle. It runs after `click` and `pressedChange` unless either handler prevents the click default. While it is pending the button previews the next state with a spinner and stays clickable, so another click reverses the unsettled toggle. Ignored for a group member with a value. |
| `tooltip` | `string` | — | Text shown on hover and keyboard focus, usually to name an icon-only toggle. |
| `value` | `string` | — | Value identifier inside a ToggleButtonGroup; the group uses it to decide the pressed state. It is not a form value. |

## ToggleButtonGroup Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | required | Accessible name of the group, rendered as `aria-label` on the `role="group"` container. |
| `type` | `'single' \| 'multiple'` | `'single'` | Selection mode. `single` keeps one member pressed and clears on a second click; `multiple` lets members switch on their own. |
| `value` | `string \| null \| string[]` | `null` | The current selection, bound with `v-model:value`. A string or `null` in single mode, an array of strings in multiple mode (an empty array when none is pressed). |
| `orientation` | `'horizontal' \| 'vertical'` | `'horizontal'` | Layout direction of the members. A vertical group stretches each member to the full width. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Size the members inherit; a member with its own `size` wins. |
| `isDisabled` | `boolean` | `false` | Disables every member. A member that disables itself stays disabled as well. |

## Events

| Name | Payload | Description |
| --- | --- | --- |
| `pressedChange` | `(isPressed: boolean, event: MouseEvent)` | The state the click asks for, with the originating click event. Call `event.preventDefault()` to own the toggle: the component then neither changes the state nor runs `pressedChangeAction`. |
| `update:isPressed` | `(isPressed: boolean)` | Backs `v-model:is-pressed`. It is not emitted when the handler prevents the click default. |
| `click` | `(event: MouseEvent)` | The originating click. On a standalone button it is emitted first, before `pressedChange` and `pressedChangeAction`; calling `event.preventDefault()` here cancels the toggle, so neither runs. On a group member with a value it is emitted after the group toggles. It is not emitted while the button is disabled. |
| `change` | `(value: string \| null \| string[])` | The group selection: a string or `null` in single mode, an array in multiple mode. |

## Slots

| Name | Description |
| --- | --- |
| `default` | Visible content. It replaces label as the visible text while label stays the accessible name; with nothing in it, label is rendered. |
| `icon` | Decorative icon before the label, or the only content of an icon-only button. Put an icon class in a sized element, such as `<span class="i-carbon-filter" style="width: 16px; height: 16px" />`, so it matches the button's icon box. |
| `pressedIcon` | Icon shown while pressed, typically a filled version of `icon`. Falls back to `icon` when empty. The artwork owns its own color, so pass an element that already carries it, such as `<span class="i-carbon-stop-filled" style="color: var(--astryx-icon-error-foreground)" />`. |

ToggleButtonGroup takes its members through the default slot:

| Name | Description |
| --- | --- |
| `default` | The ToggleButton members. Each one needs its own `value`. |

## Keyboard

| Key | Behavior |
| --- | --- |
| `Tab` | Moves through every available toggle; a group does not collapse its members into a single tab stop. |
| `Enter` / `Space` | Toggles the focused button, exactly like a click. |

The group does not take over the arrow keys: each member is its own tab stop, so `Tab` walks them one by one and the arrow keys do not move focus between members — unlike `ButtonGroup`. Disabled members stay tab stops too (marked `aria-disabled`); they just do not activate.

## Template ref

`ToggleButton` exposes:

- `element`: `HTMLButtonElement | HTMLAnchorElement | HTMLElement | null`.
- `focus(options?: FocusOptions)`: focuses the button.
- `blur()`: removes focus from the button.

## Native attributes

The root element is `<button type="button">`.

It receives:

- HTML attributes.
- `aria-*` attributes.
- `data-*` attributes.
- `class`.
- `style`.
- Native event listeners.

The component sets `aria-pressed` from the pressed state, and uses `data-pressed`, `data-variant="ghost"`, and `data-size` for theming; leave those to the component. In icon-only mode `aria-label` comes from `label`.

## Accessibility

### Text label

- Criterion: 1.4.3 Contrast (Minimum)
- Check: 4.5:1
- States: Unselected, Selected, Hover, Pointer down

The label must have at least 4.5:1 contrast with the button background when selected and unselected. For Hover and Pointer down, measure the final background after the overlay is applied.

### Essential icon or spinner arc

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Icon only, Loading

An icon-only ToggleButton must have at least 3:1 contrast between its icon and button background. The moving spinner arc must also meet 3:1. An icon beside a visible label does not need its own check.

### Selected state indicator

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1 if relied upon
- States: Selected

The selected background must reach 3:1 only when users need it to tell selected from unselected. Label weight or a changed icon can also show selection.

### Visible control boundary

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1 if needed
- States: Rest

The button edge needs 3:1 contrast only when users need it to see the control. A visible label or icon can show the control instead.

### Keyboard focus indicator

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Focus visible

The focus outline must have at least 3:1 contrast with the area around the button. Check both selected and unselected states.

### Disabled appearance

- Criterion: 1.4.3 and 1.4.11 exceptions
- Check: Not required
- States: Disabled

Disabled controls do not need to meet these contrast ratios.
