# 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. --- ## Anatomy ```tsx ``` ## 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() { } ``` ### 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(['left']); { @value = e.value; }} class={styles.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'] });
{'Value: '} {@toggleGroup.value.join(', ')}
} ``` ### 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() { } ``` ## 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