ToggleGroup
Features
- Full keyboard navigation.
- Supports horizontal/vertical orientation.
- Support single and multiple pressed buttons.
- Can be controlled or uncontrolled.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import the component.
<script setup>
import { ToggleGroupItem, ToggleGroupRoot } from 'reka-ui'
</script>
<template>
<ToggleGroupRoot>
<ToggleGroupItem />
</ToggleGroupRoot>
</template>API Reference
Root
Contains all the parts of a toggle group.
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Item
An item in the group.
| Data Attribute | Value |
|---|---|
[data-state] | "checked" | "unchecked" |
[data-disabled] | Present when disabled |
[data-orientation] | "vertical" | "horizontal" |
Examples
Ensuring there is always a value
You can control the component to ensure a value.
<script setup>
import { ToggleGroupItem, ToggleGroupRoot } from 'reka-ui'
import { ref } from 'vue'
const value = ref('left')
</script>
<template>
<ToggleGroupRoot
:model-value="value"
@update:model-value="(val) => {
if (val) value = val
}"
>
<ToggleGroupItem value="left">
<TextAlignLeftIcon />
</ToggleGroupItem>
<ToggleGroupItem value="center">
<TextAlignCenterIcon />
</ToggleGroupItem>
<ToggleGroupItem value="right">
<TextAlignRightIcon />
</ToggleGroupItem>
</ToggleGroupRoot>
</template>Real-world examples
- Toolbar Rich Text: A formatting toolbar combining ToggleGroup, DropdownMenu and Tooltip under one roving focus ring.
Accessibility
Uses roving tabindex to manage focus movement among items.
Keyboard Interactions
| Key | Description |
|---|---|
Tab | Moves focus to either the pressed item or the first item in the group. |
Space | Activates/deactivates the item. |
Enter | Activates/deactivates the item. |
ArrowDown | Moves focus to the next item in the group. |
ArrowRight | Moves focus to the next item in the group. |
ArrowUp | Moves focus to the previous item in the group. |
ArrowLeft | Moves focus to the previous item in the group. |
Home | Moves focus to the first item. |
End | Moves focus to the last item. |
In a right-to-left layout (dir="rtl" on ToggleGroupRoot, or on a ConfigProvider), a horizontal group's ArrowLeft moves to the next item and ArrowRight to the previous one. A vertical group uses ArrowUp and ArrowDown in either direction. See Internationalization.
Headless composable
useToggleGroup holds the pressed value or values (type single or multiple, modelValue, changeModelValue(value)), the root part surface and getItemSurface(value) for each item. Arrow-key navigation between the items is not part of it: wrap them in a RovingFocusGroup, or use the components. It is pure, so it can be called outside setup(). The composable is experimental; see Headless composables.
