

# ToggleGroup

A row or column of related toggle buttons where one or several can be pressed: text formatting (bold, italic, underline), alignment, view switches. Compose it from `ToggleGroup.Item`s; each item is a native `<button>` that takes `disabled`, `aria-label`, `title` and event handlers directly.

Reach for a different component when the choice is different:

- Exactly one option must always be chosen, with a sliding indicator: [`SegmentedControl`](/components/segmented-control).
- A form value with radio semantics (`name`, validation, labels): [`RadioGroup`](/components/radio-group).
- A single independent on/off button: [`ToggleButton`](/components/toggle-button).

## Examples

### Single

Without `multiple` at most one item is pressed. Pressing the pressed item turns it off, and `onValueChange` receives `null`. Add `required` to keep one item pressed.

```tsx
import { ToggleGroup } from '@var-ui/react';

<ToggleGroup aria-label="View" defaultValue="list">
  <ToggleGroup.Item value="list">List</ToggleGroup.Item>
  <ToggleGroup.Item value="grid">Grid</ToggleGroup.Item>
  <ToggleGroup.Item value="board">Board</ToggleGroup.Item>
</ToggleGroup>
```

```astro
<!-- No Astro binding: ToggleGroup holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Multiple

With `multiple`, any number of items can be pressed and `value` / `onValueChange` use `string[]`.

```tsx
import { ToggleGroup } from '@var-ui/react';

<ToggleGroup aria-label="Text style" multiple defaultValue={['bold']}>
  <ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
  <ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
  <ToggleGroup.Item value="underline">Underline</ToggleGroup.Item>
  <ToggleGroup.Item value="strike" disabled>
    Strike
  </ToggleGroup.Item>
</ToggleGroup>
```

```astro
<!-- No Astro binding: ToggleGroup holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Icons only

Icon-only items must carry an `aria-label`. Use `required` for a choice that always has a value, like alignment.

```tsx
import { ToggleGroup } from '@var-ui/react';

function AlignIcon({ align }: { align: 'left' | 'center' | 'right' }) {
  const widths = [14, 9, 12];
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true" fill="currentColor">
      {widths.map((w, i) => {
        const x = align === 'left' ? 1 : align === 'right' ? 15 - w : (16 - w) / 2;
        return <rect key={i} x={x} y={3 + i * 4} width={w} height="2" rx="1" />;
      })}
    </svg>
  );
}

<ToggleGroup aria-label="Text alignment" defaultValue="left" required>
  <ToggleGroup.Item value="left" aria-label="Align left" title="Align left">
    <AlignIcon align="left" />
  </ToggleGroup.Item>
  <ToggleGroup.Item value="center" aria-label="Align center" title="Align center">
    <AlignIcon align="center" />
  </ToggleGroup.Item>
  <ToggleGroup.Item value="right" aria-label="Align right" title="Align right">
    <AlignIcon align="right" />
  </ToggleGroup.Item>
</ToggleGroup>
```

```astro
<!-- No Astro binding: ToggleGroup holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Sizes, orientation, tone and appearance

`size`, `tone`, `appearance` (`outline`, `subtle`, `ghost`) and `orientation` use the same names as the recipe axes. A vertical group moves focus with Up and Down.

```tsx
import { HStack, ToggleGroup } from '@var-ui/react';

<HStack gap="lg">
  {(['sm', 'md', 'lg'] as const).map((size) => (
    <ToggleGroup key={size} aria-label={"Size " + size} size={size} defaultValue="a">
      <ToggleGroup.Item value="a">One</ToggleGroup.Item>
      <ToggleGroup.Item value="b">Two</ToggleGroup.Item>
    </ToggleGroup>
  ))}
  <ToggleGroup aria-label="Vertical" orientation="vertical" tone="success" defaultValue="b">
    <ToggleGroup.Item value="a">Top</ToggleGroup.Item>
    <ToggleGroup.Item value="b">Middle</ToggleGroup.Item>
    <ToggleGroup.Item value="c">Bottom</ToggleGroup.Item>
  </ToggleGroup>
  <ToggleGroup aria-label="Ghost" appearance="ghost" multiple defaultValue={['a']}>
    <ToggleGroup.Item value="a">Ghost</ToggleGroup.Item>
    <ToggleGroup.Item value="b">Group</ToggleGroup.Item>
  </ToggleGroup>
</HStack>
```

```astro
<!-- No Astro binding: ToggleGroup holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### In a toolbar

The group works inside a [`Toolbar`](/components/toolbar). Toolbar has no arrow-key handling of its own, so each group keeps its own Tab stop and arrow-key roving; they do not conflict. Give the toolbar a `label` and each group an `aria-label`.

```tsx
import { Button, ToggleGroup, Toolbar } from '@var-ui/react';

<Toolbar
  label="Formatting"
  startContent={
    <ToggleGroup aria-label="Text style" multiple>
      <ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
      <ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
    </ToggleGroup>
  }
  endContent={<Button>Share</Button>}
/>
```

```astro
<!-- No Astro binding: ToggleGroup holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Controlled

Drive the pressed items with `value` and `onValueChange`.

```tsx
import { ToggleGroup } from '@var-ui/react';
import { useState } from 'react';

const [value, setValue] = useState<string[]>(['bold']);

<ToggleGroup aria-label="Text style" multiple value={value} onValueChange={setValue}>
  <ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
  <ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
  <ToggleGroup.Item value="underline">Underline</ToggleGroup.Item>
</ToggleGroup>
<p>Pressed: {value.join(', ') || 'none'}</p>
```

```astro
<!-- No Astro binding: ToggleGroup holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

## Props

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| appearance | "ghost" \| "outline" \| "subtle" \| undefined | no | outline | Resting look: `outline` and `subtle` connect the items, `ghost` spaces them. |
| children | ReactNode | no |  | The `ToggleGroup.Item`s. |
| className | string \| undefined | no |  |  |
| defaultValue | string \| string[] \| null \| undefined | no |  | Initially pressed item when uncontrolled. Initially pressed items when uncontrolled. |
| disabled | boolean \| undefined | no |  | Disables every item. |
| multiple | boolean \| undefined | no |  | Allow several pressed items at once. Without it at most one item is pressed. |
| onValueChange | ((value: string \| null) => void) \| ((value: string[]) => void) \| undefined | no |  | Called with the pressed item (`null` when it was turned off). There is no native `onChange`: items are buttons. Called with every pressed item, in the order they were turned on. |
| orientation | "horizontal" \| "vertical" \| undefined | no | horizontal | Layout axis. Vertical groups move focus with Up/Down instead of Left/Right. |
| ref | Ref<HTMLDivElement> \| undefined | no |  | Ref to the `role="group"` element. |
| required | boolean \| undefined | no |  | Keeps one item pressed: the pressed item cannot be turned off by pressing it again (in `multiple` mode, the last pressed item cannot). The HTML `required` of a group that must have a value. |
| size | "lg" \| "md" \| "sm" \| undefined | no | "md" | Control density. Falls back to the surrounding control size, then `md`. |
| style | CSSProperties \| undefined | no |  |  |
| tone | "accent" \| "danger" \| "info" \| "neutral" \| "success" \| "warning" \| undefined | no | accent | Color of the pressed items. |
| value | string \| string[] \| null \| undefined | no |  | Controlled pressed item (`null` = controlled, nothing pressed). Controlled pressed items. |

`ToggleGroup.Item` takes `value` (required), `disabled` and everything a `<button>` takes. Pressing it calls your `onClick` first; call `event.preventDefault()` there to cancel the toggle.

## Accessibility

- Pattern: the APG [toggle button](https://www.w3.org/WAI/ARIA/apg/patterns/button/) pattern. The root is `role="group"` (name it with `aria-label` or `aria-labelledby`) and every item is a native `<button type="button">` with `aria-pressed`. This is the correct semantics for deselectable single-select and for multi-select. ToggleGroup never uses `role="radiogroup"`, because radios cannot be turned off; use `SegmentedControl` or `RadioGroup` when you want radio behaviour.
- Focus: roving tabindex (the APG toolbar keyboard model). The group is a single Tab stop, on the pressed item or else the first enabled item. Arrow keys move focus without pressing, and do not wrap. Disabled items are skipped.
- RTL: Left and Right are mirrored in a right-to-left context (read from the computed `direction`); the connected corners and borders use logical properties.
- Forced colors: the pressed item is drawn with `Highlight` / `HighlightText`, so the state does not depend on a background color.
- Static markup: items render as plain buttons with no `tabindex`, so before hydration every item is tabbable and pressed items already carry `aria-pressed` and `data-selected`. The roving Tab stop is applied on the client. There is no Astro component because pressing needs client state.
- The group is not a form control and submits nothing. Mirror `value` into a hidden `<input>` if you need it in a form.

| Key                | Action                                              |
| ------------------ | --------------------------------------------------- |
| Tab / Shift+Tab    | Moves focus into or out of the group (one Tab stop) |
| Right / Left arrow | Next / previous item (mirrored in RTL), horizontal  |
| Down / Up arrow    | Next / previous item, vertical                      |
| Home / End         | First / last enabled item                           |
| Space / Enter      | Toggles the focused item                            |


> Also accepts all standard HTML attributes (ARIA attributes, event handlers, styles, …). Run `var-ui component toggle-group --all-props` for the complete list.
