# Toggle Group

> Toggle button group for React and shadcn, single or multiple selection, for toolbars like bold, italic and underline.

Toggle buttons sharing one highlight: it travels between items in single mode, and pressed neighbours fuse into one shape in multiple mode. Base UI ToggleGroup behaviour.

- Tier: Free (MIT)
- Interaction: Select
- Page: https://eased.dev/components/toggle-group
- Installs: motion, @base-ui/react
- Registry: https://eased.dev/r/toggle-group.json

## Install

```bash
npx shadcn add https://eased.dev/r/toggle-group.json
```

A row of toggle buttons on Base UI's ToggleGroup (one tab stop, arrow keys, `aria-pressed` on each), sharing one highlight that travels or fuses.

## When to use

- Formatting toolbars where several options can be on at once: bold, italic, underline, strikethrough (`multiple`).
- One-of-several settings that act on something else, like text alignment, where clearing the choice is allowed (single mode).
- Filter rows of on/off chips, such as days of the week or tags: `variant="chips"`.

## When not to use

- A single choice between views or values, where one must always be selected: use [Segmented](/components/segmented), which has radio semantics and submits with a form.
- Switching between panels of content: use [Tabs](/components/tabs).
- A single on/off setting with a label: use a [Switch](/components/switch) or a [Checkbox](/components/checkbox).

## Usage

```tsx formatting-toolbar.tsx
import { useState } from "react";
import { ToggleGroup, ToggleGroupItem } from "@/components/eased/toggle-group/toggle-group";

export function FormattingToolbar() {
  const [formats, setFormats] = useState<string[]>(["bold"]);
  return (
    <ToggleGroup multiple aria-label="Text formatting" value={formats} onValueChange={setFormats}>
      <ToggleGroupItem value="bold" aria-label="Bold"><BoldIcon /></ToggleGroupItem>
      <ToggleGroupItem value="italic" aria-label="Italic"><ItalicIcon /></ToggleGroupItem>
      <ToggleGroupItem value="underline" aria-label="Underline"><UnderlineIcon /></ToggleGroupItem>
    </ToggleGroup>
  );
}
```

## Examples

### Single choice that can't be cleared

Single mode lets people press the pressed item to clear it. To keep one selected, ignore empty updates.

```tsx
<ToggleGroup aria-label="Alignment" value={align} onValueChange={(v) => v.length && setAlign(v)}>
  <ToggleGroupItem value="left" aria-label="Align left"><AlignLeftIcon /></ToggleGroupItem>
  <ToggleGroupItem value="center" aria-label="Align centre"><AlignCenterIcon /></ToggleGroupItem>
  <ToggleGroupItem value="right" aria-label="Align right"><AlignRightIcon /></ToggleGroupItem>
</ToggleGroup>
```

### Chips

Separate bordered chips that fill when pressed, for filter rows. Wrap a long row in a horizontal [Scroll Area](/components/scroll-area).

```tsx
<ToggleGroup variant="chips" multiple aria-label="Days" defaultValue={["mon", "wed"]}>
  <ToggleGroupItem value="mon">Mon</ToggleGroupItem>
  <ToggleGroupItem value="tue">Tue</ToggleGroupItem>
  <ToggleGroupItem value="wed">Wed</ToggleGroupItem>
</ToggleGroup>
```

## API reference

### ToggleGroup

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `string[]` | | Pressed values (controlled). |
| `defaultValue` | `string[]` | `[]` | Initially pressed values (uncontrolled). |
| `onValueChange` | `(value: string[]) => void` | | |
| `multiple` | `boolean` | `false` | `false`: at most one item pressed, and the highlight travels between them. `true`: any number, and pressed neighbours fuse into one shape. |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | |
| `variant` | `"track" \| "chips"` | `"track"` | `track`: items share one 32px bordered track and one highlight, the height of Segmented, Tabs and a small Button. `chips`: separate 32px bordered chips that fill when pressed. |
| `disabled` | `boolean` | | Disables every item. |
| `aria-label` / `aria-labelledby` | `string` | | Names the group, e.g. "Text formatting". |
| `className` | `string` | `""` | Appended to the group's classes. |

### ToggleGroupItem

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `string` | | |
| `children` | `ReactNode` | | An icon, a short label, or both. Icons are sized to 16px. |
| `aria-label` | `string` | | Required for icon-only items, e.g. "Bold". |
| `disabled` | `boolean` | | |
| `className` | `string` | `""` | |

## Keyboard

| Key | Action |
|---|---|
| `Tab` | Moves focus into the group, then out of it. |
| `←` / `→` | Moves focus between items, wrapping at the ends (`↑` / `↓` when vertical). |
| `Home` / `End` | Moves focus to the first or last item. |
| `Enter` / `Space` | Presses or releases the focused item. |

## Accessibility

- Each item is a button with `aria-pressed`; name the group with `aria-label` and give every icon-only item its own `aria-label`.
- One tab stop for the whole group, so keyboard users are not walked through every item.
- Items have no extended hit area: they sit edge to edge, and a larger target would steal the neighbour's clicks.
- A pressed item's label changes colour, never weight or size, so nothing shifts.

## Motion

In single mode one highlight travels to the newly pressed item on the snappy spring; when the group was empty it grows in from 0.9 instead, and clearing the group fades it out in 100ms. In multiple mode each pressed item's highlight grows in from 0.9 on the snappy spring with a 120ms fade, leaves in 100ms, and a bridge fades in over 150ms across the seam to a pressed neighbour, so a run of pressed items reads as one shape. Chips change colour over 150ms. Reduced motion: highlights fade in 100ms without scaling, the travelling highlight jumps, and the bridge fades in 100ms.

## Notes for AI

Use ToggleGroup for pressing options on and off: `multiple` for formatting and filters, single mode for a choice that may be cleared. Give icon-only items an `aria-label`, and use `variant="chips"` for filter rows.

Do not use it for a required single choice or a form value: [Segmented](/components/segmented) is the radio-style control. Do not draw your own pressed background or merge pressed items yourself; the highlight and the bridge already do it.

## Related

- [Segmented](/components/segmented): a required single choice with a raised thumb.
- [Tabs](/components/tabs): switching between panels of content.
- [Scroll Area](/components/scroll-area): for a chip row that scrolls sideways.

## Example

`components/eased/toggle-group/toggle-group-demo.tsx`

```tsx
"use client";

import { useState, type ReactNode } from "react";
import { DemoFrame } from "../lib/demo-frame";
import { ToggleGroup, ToggleGroupItem } from "./toggle-group";

function Icon({ children }: { children: ReactNode }) {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth={1.5} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
      {children}
    </svg>
  );
}

const FORMATS = [
  { value: "bold", label: "Bold", icon: <path d="M4.5 3h4.25a2.5 2.5 0 0 1 0 5H4.5zM4.5 8h5a2.5 2.5 0 0 1 0 5h-5z" /> },
  { value: "italic", label: "Italic", icon: <path d="M10 3H6.5M9.5 13H6M9 3 7 13" /> },
  { value: "underline", label: "Underline", icon: <path d="M4.5 2.5v4.5a3.5 3.5 0 0 0 7 0V2.5M3.5 13.5h9" /> },
  { value: "strike", label: "Strikethrough", icon: <path d="M3 8h10M10.75 4.25C10.3 3.3 9.3 2.75 8 2.75c-1.8 0-3 .9-3 2.25 0 .9.6 1.6 1.6 2M5 11.5c.45 1.1 1.55 1.75 3 1.75 1.9 0 3.1-.95 3.1-2.4 0-.55-.15-1-.45-1.35" /> },
];

const ALIGNS = [
  { value: "left", label: "Align left", icon: <path d="M2.5 3.5h11M2.5 6.5h7M2.5 9.5h11M2.5 12.5h7" /> },
  { value: "center", label: "Align centre", icon: <path d="M2.5 3.5h11M4.5 6.5h7M2.5 9.5h11M4.5 12.5h7" /> },
  { value: "right", label: "Align right", icon: <path d="M2.5 3.5h11M6.5 6.5h7M2.5 9.5h11M6.5 12.5h7" /> },
  { value: "justify", label: "Justify", icon: <path d="M2.5 3.5h11M2.5 6.5h11M2.5 9.5h11M2.5 12.5h11" /> },
];

const ALIGN_CLASS: Record<string, string> = {
  left: "text-left",
  center: "text-center",
  right: "text-right",
  justify: "text-justify",
};

const DECORATION: Record<string, string> = {
  "false-false": "",
  "true-false": "underline",
  "false-true": "line-through",
  "true-true": "[text-decoration-line:underline_line-through]",
};

export function ToggleGroupDemo() {
  const [formats, setFormats] = useState<string[]>(["bold"]);
  const [align, setAlign] = useState<string[]>(["left"]);
  const on = (f: string) => formats.includes(f);

  return (
    <DemoFrame title="Release notes" meta="Draft">
      <div className="flex flex-wrap items-center gap-2 border-b border-line-subtle px-4 py-2.5">
        <ToggleGroup multiple aria-label="Text formatting" value={formats} onValueChange={setFormats}>
          {FORMATS.map((f) => (
            <ToggleGroupItem key={f.value} value={f.value} aria-label={f.label}>
              <Icon>{f.icon}</Icon>
            </ToggleGroupItem>
          ))}
        </ToggleGroup>
        <span aria-hidden className="mx-1 h-5 w-px bg-line-subtle" />
        <ToggleGroup aria-label="Alignment" value={align} onValueChange={(v) => v.length && setAlign(v)}>
          {ALIGNS.map((a) => (
            <ToggleGroupItem key={a.value} value={a.value} aria-label={a.label}>
              <Icon>{a.icon}</Icon>
            </ToggleGroupItem>
          ))}
        </ToggleGroup>
      </div>

      <div className="min-h-0 flex-1 overflow-y-auto p-4">
        <p className="mb-2 text-ui-xs text-text-muted">v2.14 · Oct 17</p>
        <p
          className={`text-ui-md text-text text-pretty ${ALIGN_CLASS[align[0] ?? "left"]} ${on("bold") ? "font-semibold" : ""} ${
            on("italic") ? "italic" : ""
          } ${DECORATION[`${on("underline")}-${on("strike")}`]} decoration-text-muted underline-offset-3`}
        >
          Checkout now remembers your shipping address between sessions.
        </p>
        <p className="mt-3 text-ui text-text-secondary text-pretty">
          Saved cards now show their expiry month, and a declined payment keeps everything you typed, so a retry is one
          tap.
        </p>
        <p className="mt-4 text-ui-xs font-medium text-text-muted uppercase tracking-caps">Fixed</p>
        <ul className="mt-1.5 flex list-disc flex-col gap-1 pl-4 text-ui text-text-secondary marker:text-text-muted">
          <li>Promo codes no longer reset when you change the quantity.</li>
          <li>Order emails show the delivery window in your time zone.</li>
        </ul>
      </div>
    </DemoFrame>
  );
}
```
