# ToggleGroup

> A segmented row of toggle buttons with roving-tabindex keyboard navigation and single or multiple selection.

- Package: `fancy-ui-react` (React 18/19 + Tailwind CSS v4)
- Category: Actions
- Tags: toggle, segmented-control, roving-tabindex, compound, selection, toolbar
- Types: `ToggleGroupProps`, `ToggleGroupItemProps`, `ToggleGroupContext`
- Docs: https://fancy-ui.rama.app/docs/components/toggle-group
- Source: https://github.com/RamaHerbin/fancy-ui/tree/main/react/src/components/toggle-group

## Import

```tsx
import { ToggleGroup } from 'fancy-ui-react';
```

Setup (once per project): `npm install fancy-ui-react`, then put these lines in your main CSS file (the stylesheet import goes right after `@import "tailwindcss";`):

```css
@import "tailwindcss";
@import "fancy-ui-react/tailwind.css";
```

and import the component styles once in the app entry: `import "fancy-ui-react/styles.css";`. Full guide: https://fancy-ui.rama.app/llms.txt

The names below are the ones to write in React; when one differs from the Svelte name, the Svelte name follows in parentheses. `class` becomes `className`, a callback takes the name the React port declares (`onclick` becomes `onClick`), snippets become `ReactNode` props (render functions when they take arguments) and the default snippet is `children`. Divergences: https://github.com/RamaHerbin/fancy-ui/tree/main/react#divergences-from-the-svelte-api

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `"single" \| "multiple"` | `"single"` | Whether one item can be active at a time, or several. |
| `value` | `string \| string[]` | `""` | The active value(s), bindable — a string when type="single", an array of strings when type="multiple". |
| `onValueChange` | `(value: string \| string[]) => void` | — | Called with the new value, shaped to match `type`, whenever the selection changes. |
| `disabled` | `boolean` | `false` | Disables every item in the group. |
| `size` | `"sm" \| "md" \| "lg"` | `"md"` | Sizes every item. |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | The rail's stacking axis; both arrow-key pairs work either way. |
| `label` | `string` | — | Accessible name for the group. |
| `className` (Svelte `class`) | `string` | — | Additional CSS classes, merged onto the root. |
| `ref` | `HTMLDivElement \| null` | `null` | Bindable reference to the root element. |
| `sound` | `boolean` | `false` | Plays toggle-on/toggle-off on a multi-select item or select on a single-select change, once the user has enabled sound |
| `ToggleGroupItem.value` (required) | `string` | — | The item's value — what gets added to or removed from the group's selection. |
| `ToggleGroupItem.disabled` | `boolean` | `false` | Disables just this item, independent of the group's own `disabled`. |
| `ToggleGroupItem.label` | `string` | — | Accessible name for icon-only content on this item. Also the text fallback. |
| `ToggleGroupItem.className` (Svelte `ToggleGroupItem.class`) | `string` | — | Additional CSS classes, merged onto the item's button. |
| `ToggleGroupItem.ref` | `HTMLButtonElement \| null` | `null` | Bindable reference to the item's button element. |

## Node props

| Prop | Description |
| --- | --- |
| `children` | The ToggleGroup's content — the ToggleGroupItems. |
| `ToggleGroupItem.children` | The item's content, typically a glyph or a short label. |

## Example

```tsx
import { useState } from "react";
import { ToggleGroup, ToggleGroupItem } from "fancy-ui-react";

const iconProps = {
	className: "size-4",
	viewBox: "0 0 24 24",
	fill: "none",
	stroke: "currentColor",
	strokeWidth: 2,
	strokeLinecap: "round",
	"aria-hidden": true,
} as const;

export function ToggleGroupExample() {
	const [alignment, setAlignment] = useState("left");

	return (
		<ToggleGroup
			sound
			label="Text alignment"
			value={alignment}
			onValueChange={(next) => setAlignment(String(next))}
		>
			<ToggleGroupItem value="left" label="Align left">
				<svg {...iconProps}>
					<line x1="3" y1="6" x2="21" y2="6" />
					<line x1="3" y1="12" x2="15" y2="12" />
					<line x1="3" y1="18" x2="18" y2="18" />
				</svg>
			</ToggleGroupItem>
			<ToggleGroupItem value="center" label="Align center">
				<svg {...iconProps}>
					<line x1="3" y1="6" x2="21" y2="6" />
					<line x1="6" y1="12" x2="18" y2="12" />
					<line x1="4" y1="18" x2="20" y2="18" />
				</svg>
			</ToggleGroupItem>
			<ToggleGroupItem value="right" label="Align right">
				<svg {...iconProps}>
					<line x1="3" y1="6" x2="21" y2="6" />
					<line x1="9" y1="12" x2="21" y2="12" />
					<line x1="6" y1="18" x2="21" y2="18" />
				</svg>
			</ToggleGroupItem>
		</ToggleGroup>
	);
}
```
