# Toggle Group

A set of toggle buttons that share one value, such as the text alignment buttons of an editor. By default one item is pressed at a time; with multiple, each item presses on its own. The group is one tab stop: arrow keys move focus between its items, and Enter or Space presses one. It renders role="group"; name it with aria-label or aria-labelledby. A group that is a whole toolbar, of three controls or more, passes role="toolbar", and then also gets aria-orientation when vertical.

- Entry point: `menhir/toggle-group`
- Size: 1.9 kB, minified and gzipped, Solid left out

> To add Toggle Group to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<ToggleGroup.Root>
  <ToggleGroup.Item />
</ToggleGroup.Root>
```

## Example

### Tailwind CSS

`ToggleGroupExample.tsx`:

```tsx
import { ToggleGroup } from "menhir/toggle-group";

const item =
  "cursor-pointer rounded-md border border-stone-300 px-3 py-1 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-pressed:border-transparent data-pressed:bg-stone-800 data-pressed:text-white dark:border-stone-700 dark:data-pressed:bg-stone-300 dark:data-pressed:text-stone-900";

export default function ToggleGroupExample() {
  return (
    <ToggleGroup.Root
      aria-label="Text alignment"
      defaultValue={["left"]}
      class="inline-flex gap-1"
    >
      <ToggleGroup.Item value="left" class={item}>
        Left
      </ToggleGroup.Item>
      <ToggleGroup.Item value="center" class={item}>
        Center
      </ToggleGroup.Item>
      <ToggleGroup.Item value="right" class={item}>
        Right
      </ToggleGroup.Item>
    </ToggleGroup.Root>
  );
}
```

### CSS

`ToggleGroupExample.tsx`:

```tsx
import { ToggleGroup } from "menhir/toggle-group";

import "./toggle-group.css";

export default function ToggleGroupExample() {
  return (
    <ToggleGroup.Root
      aria-label="Text alignment"
      defaultValue={["left"]}
      class="toggle-group"
    >
      <ToggleGroup.Item value="left" class="toggle-group-item">
        Left
      </ToggleGroup.Item>
      <ToggleGroup.Item value="center" class="toggle-group-item">
        Center
      </ToggleGroup.Item>
      <ToggleGroup.Item value="right" class="toggle-group-item">
        Right
      </ToggleGroup.Item>
    </ToggleGroup.Root>
  );
}
```

`toggle-group.css`:

```css
/* The least CSS that makes the Toggle Group visible: buttons in a row,
   the pressed one filled, selected by data-pressed. Everything else is the
   browser's. Colours are Tailwind's stone palette; light-dark() picks the
   second one in dark mode (color-scheme: dark). */
.toggle-group {
  display: inline-flex;
  gap: 0.25rem;
}

.toggle-group-item {
  padding: 0.25rem 0.75rem;
  border: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(37.4% 0.01 67.558) /* stone-700 */
    );
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  cursor: pointer;
}

.toggle-group-item[data-pressed] {
  border-color: transparent;
  background: light-dark(
    oklch(26.8% 0.007 34.298) /* stone-800 */,
    oklch(86.9% 0.005 56.366) /* stone-300 */
  );
  color: light-dark(#fff, oklch(21.6% 0.006 56.043) /* stone-900 */);
}
```

## API reference

### Root

<div role="group">, with data-horizontal or data-vertical, and data-disabled.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string[]` | — | The values of the pressed items, when controlled. Pair it with onValueChange. Nothing pressed is []. |
| `defaultValue` | `string[]` | `[]` | The values of the items pressed at first, when uncontrolled. |
| `onValueChange` | `(value: string[]) => void` | — | Called with the new values when the user presses or releases an item. A controlled group changes only if value does. |
| `multiple` | `boolean` | `false` | Whether several items can be pressed at once. Otherwise pressing an item releases the one pressed before. |
| `disabled` | `boolean` | `false` | Whether every item ignores the user. They cannot be focused either. |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | Whether the items sit side by side, moved between with Left and Right, or stacked, moved between with Up and Down. |
| `render` | `(props) => JSX.Element` | — | Renders the group with your own component. Read once. |

### Item

<button type="button" aria-pressed>, with data-pressed or data-unpressed, and data-disabled. Name it with its content, or with aria-label when it shows only an icon. Its render function must render a <button>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | Identifies the item in the group's value. Required. |
| `disabled` | `boolean` | `false` | Whether the item ignores the user. Arrow keys skip it. |
| `render` | `(props) => JSX.Element` | — | Renders the item with your own button component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Tab` | Moves focus into the group, to the item that last had focus or else the first, or out of it. |
| `ArrowRight` | Moves focus to the next item, or the first from the last. Horizontal groups; the previous item in right-to-left text. |
| `ArrowLeft` | Moves focus to the previous item, or the last from the first. Horizontal groups; the next item in right-to-left text. |
| `ArrowDown` | Moves focus to the next item, or the first from the last. Vertical groups. |
| `ArrowUp` | Moves focus to the previous item, or the last from the first. Vertical groups. |
| `Home` | Moves focus to the first item. |
| `End` | Moves focus to the last item. |
| `Space` | Presses or releases the focused item. |
| `Enter` | Presses or releases the focused item. |
