# Toggle Group

URL: https://ark-ui.com/docs/components/toggle-group
Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/components/toggle-group.mdx

A set of two-state buttons that can be toggled on or off.

---

<ComponentPreview id="ToggleGroup" />

## Anatomy

<Anatomy id="toggle-group" />

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

## Examples

**Example: basic**

```ripple
import { ToggleGroup } from 'ark-ripple/toggle-group';
import { TextAlignCenter, TextAlignJustify, TextAlignStart, TextAlignEnd } from 'lucide-ripple';
import styles from 'styles/toggle-group.module.css';

export component Basic() {
  <ToggleGroup.Root defaultValue={['left']} class={styles.Root}>
    <ToggleGroup.Item value="left" class={styles.Item}>
      <TextAlignStart />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="center" class={styles.Item}>
      <TextAlignCenter />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="right" class={styles.Item}>
      <TextAlignEnd />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="justify" class={styles.Item}>
      <TextAlignJustify />
    </ToggleGroup.Item>
  </ToggleGroup.Root>
}

```

### Controlled

Use the `value` and `onValueChange` props to control the toggle group state.

**Example: controlled**

```ripple
import { ToggleGroup } from 'ark-ripple/toggle-group';
import { TextAlignCenter, TextAlignJustify, TextAlignStart, TextAlignEnd } from 'lucide-ripple';
import { track } from 'ripple';
import styles from 'styles/toggle-group.module.css';

export component Controlled() {
  let value = track<string[]>(['left']);

  <ToggleGroup.Root
    {value}
    onValueChange={(e) => {
      @value = e.value;
    }}
    class={styles.Root}
  >
    <ToggleGroup.Item value="left" class={styles.Item}>
      <TextAlignStart />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="center" class={styles.Item}>
      <TextAlignCenter />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="right" class={styles.Item}>
      <TextAlignEnd />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="justify" class={styles.Item}>
      <TextAlignJustify />
    </ToggleGroup.Item>
  </ToggleGroup.Root>
}

```

### Root Provider

An alternative way to control the toggle group is to use the `RootProvider` component and the `useToggleGroup` hook.
This way you can access the state and methods from outside the component.

**Example: root-provider**

```ripple
import { ToggleGroup, useToggleGroup } from 'ark-ripple/toggle-group';
import { TextAlignCenter, TextAlignJustify, TextAlignStart, TextAlignEnd } from 'lucide-ripple';
import styles from 'styles/toggle-group.module.css';

export component RootProvider() {
  const toggleGroup = useToggleGroup({ defaultValue: ['left'] });

  <div class="stack">
    <output>
      {'Value: '}
      {@toggleGroup.value.join(', ')}
    </output>
    <ToggleGroup.RootProvider value={toggleGroup} class={styles.Root}>
      <ToggleGroup.Item value="left" class={styles.Item}>
        <TextAlignStart />
      </ToggleGroup.Item>
      <ToggleGroup.Item value="center" class={styles.Item}>
        <TextAlignCenter />
      </ToggleGroup.Item>
      <ToggleGroup.Item value="right" class={styles.Item}>
        <TextAlignEnd />
      </ToggleGroup.Item>
      <ToggleGroup.Item value="justify" class={styles.Item}>
        <TextAlignJustify />
      </ToggleGroup.Item>
    </ToggleGroup.RootProvider>
  </div>
}

```

### Multiple

Demonstrates how to enable `multiple` selection within the group.

**Example: multiple**

```ripple
import { ToggleGroup } from 'ark-ripple/toggle-group';
import { Bold, Italic, Underline } from 'lucide-ripple';
import styles from 'styles/toggle-group.module.css';

export component Multiple() {
  <ToggleGroup.Root defaultValue={['bold']} multiple class={styles.Root}>
    <ToggleGroup.Item value="bold" class={styles.Item}>
      <Bold />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="italic" class={styles.Item}>
      <Italic />
    </ToggleGroup.Item>
    <ToggleGroup.Item value="underline" class={styles.Item}>
      <Underline />
    </ToggleGroup.Item>
  </ToggleGroup.Root>
}

```

## API Reference

### Props

**Component API Reference**

**Root Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `defaultValue` | `string[]` | No | The initial selected value of the toggle group when rendered.
Use when you don't need to control the selected value of the toggle group. |
| `deselectable` | `boolean` | No | Whether the toggle group allows empty selection.
**Note:** This is ignored if `multiple` is `true`. |
| `disabled` | `boolean` | No | Whether the toggle is disabled. |
| `id` | `string` | No | The unique identifier of the machine. |
| `ids` | `Partial<{ root: string; item: (value: string) => string }>` | No | The ids of the elements in the toggle. Useful for composition. |
| `loopFocus` | `boolean` | No | Whether to loop focus inside the toggle group. |
| `multiple` | `boolean` | No | Whether to allow multiple toggles to be selected. |
| `onValueChange` | `(details: ValueChangeDetails) => void` | No | Function to call when the toggle is clicked. |
| `orientation` | `Orientation` | No | The orientation of the toggle group. |
| `rovingFocus` | `boolean` | No | Whether to use roving tab index to manage focus. |
| `value` | `string[]` | No | The controlled selected value of the toggle group. |


**Root Data Attributes:**

| Attribute | Value |
|-----------|-------|
| `[data-scope]` | toggle-group |
| `[data-part]` | root |
| `[data-disabled]` | Present when disabled |
| `[data-orientation]` | The orientation of the toggle-group |
| `[data-focus]` | Present when focused |


**Item Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `string` | Yes |  |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `disabled` | `boolean` | No |  |


**Item Data Attributes:**

| Attribute | Value |
|-----------|-------|
| `[data-scope]` | toggle-group |
| `[data-part]` | item |
| `[data-focus]` | Present when focused |
| `[data-disabled]` | Present when disabled |
| `[data-orientation]` | The orientation of the item |
| `[data-state]` | "on" | "off" |


**RootProvider Props:**

| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseToggleGroupReturn` | Yes |  |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |


### Context

**API:**

| Property | Type | Description |
|----------|------|-------------|
| `value` | `string[]` | The value of the toggle group. |
| `setValue` | `(value: string[]) => void` | Sets the value of the toggle group. |
| `getItemState` | `(props: ItemProps) => ItemState` | Returns the state of the toggle item. |


## Accessibility

### Keyboard Support

<KeyBindingsTable id="toggle-group" />