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-ui
Anatomy
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.
Prop | Default | Type |
---|---|---|
as | 'div' | AsTag | Component The element or component this component should render as. Can be overwrite by |
asChild | boolean Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
defaultValue | AcceptableValue | AcceptableValue[] The default active value of the item(s). Use when you do not need to control the state of the item(s). | |
dir | 'ltr' | 'rtl' The reading direction of the combobox when applicable. | |
disabled | false | boolean When |
loop | true | boolean When |
modelValue | AcceptableValue | AcceptableValue[] The controlled value of the active item(s). Use this when you need to control the state of the items. Can be binded with | |
name | string The name of the field. Submitted with its owning form as part of a name/value pair. | |
orientation | 'vertical' | 'horizontal' The orientation of the component, which determines how focus moves: | |
required | boolean When | |
rovingFocus | true | boolean When |
type | 'single' | 'multiple' Determines whether a "single" or "multiple" items can be pressed at a time. This prop will be ignored if any of |
Emit | Payload |
---|---|
update:modelValue | [payload: AcceptableValue | AcceptableValue[]] Event handler called when the value of the toggle changes. |
Slots (default) | Payload |
---|---|
modelValue | AcceptableValue | AcceptableValue[] | undefined Current toggle values |
Data Attribute | Value |
---|---|
[data-orientation] | "vertical" | "horizontal" |
Item
An item in the group.
Prop | Default | Type |
---|---|---|
as | 'button' | AsTag | Component The element or component this component should render as. Can be overwrite by |
asChild | boolean Change the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
defaultValue | boolean The pressed state of the toggle when it is initially rendered. Use when you do not need to control its open state. | |
disabled | boolean When | |
modelValue | boolean The controlled pressed state of the toggle. Can be bind as | |
value* | AcceptableValue A string value for the toggle group item. All items within a toggle group should use a unique value. |
Data Attribute | Value |
---|---|
[data-state] | "on" | "off" |
[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 { ref } from 'vue'
import { ToggleGroupItem, ToggleGroupRoot } from 'reka-ui'
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>
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. |