Toggle Group
View as MarkdownA set of toggle buttons where one or several can be pressed.
Features
Permalink to heading "Features"- Single or multiple selection
- Roving focus: the group is one tab stop and the arrow keys move between the items, with Home and End to jump to the first and last item
- Supports horizontal and vertical orientations
- Optionally prevents the pressed item from being deselected
- Supports disabled items and a disabled group
<primitives:toggleGroup.root>
<primitives:toggleGroup.item />
</primitives:toggleGroup.root>
Installation
Permalink to heading "Installation"typo3 ui:add toggle-group
Please copy the files manually from GitHub into your project.
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Multiple
Permalink to heading "Multiple"Allow several items to be pressed at the same time.
Vertical Orientation
Permalink to heading "Vertical Orientation"Stack the items vertically. The arrow keys follow the orientation.
Disabled
Permalink to heading "Disabled"Disable the whole group, or single items. A disabled group always wins over an item, an item can not opt out of it.
Not Deselectable
Permalink to heading "Not Deselectable"Keep one item always pressed. This has no effect with multiple.
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.
toggleGroup.root
Permalink to heading "toggleGroup.root"Provides the shared state and wraps the items. Renders a <div> element with role="group".
| Name | Description | Required | Default |
|---|---|---|---|
defaultValue | string[]The initial value of the pressed items when rendered. Use when you don't need to control the value of the toggle group. | No | - |
disabled | booleanWhether all items of the toggle group are disabled. | No | - |
multiple | booleanWhether multiple items can be pressed at the same time. | No | false |
deselectable | booleanWhether the pressed item can be deselected again. Ignored when `multiple` is `true`. | No | true |
loopFocus | booleanWhether the keyboard navigation loops from the last item to the first, and vice versa. | No | true |
rovingFocus | booleanWhether the items use a roving tabindex, so the whole group is a single tab stop and the arrow keys move the focus. | No | true |
orientation | Enum\OrientationThe orientation of the toggle group. | No | Horizontal |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-toggle-group-root | <uid> |
data-disabled | Present when disabled |
data-multiple | |
data-orientation | The orientation of the toggle-group |
toggleGroup.item
Permalink to heading "toggleGroup.item"A toggle button of the group. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
value | stringThe value of the toggle group item. | Yes | - |
disabled | booleanWhether the item is disabled. An item of a disabled group is always disabled. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-toggle-group-item | <uid> |
data-pressed | Present when pressed |
data-disabled | Present when disabled |
data-orientation | The orientation of the item |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | 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. |
getRootState | () => RootState | Returns the state of the toggle group |
Accessibility
Permalink to heading "Accessibility"| 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. |