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 with arrow keys, Home and End
- Supports horizontal and vertical orientations
- Can keep one item pressed
- Supports disabled items and a disabled group
<primitives:toggleGroup.root>
<primitives:toggleGroup.item />
</primitives:toggleGroup.root>
Installation
Permalink to heading "Installation"typo3 ui:add toggleGroup
Please copy the files manually from GitHub into your project: ToggleGroup
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Allow several pressed items
Permalink to heading "Allow several pressed items"Set multiple so items press independently. Bold, italic and underline below combine.
Stack the items vertically
Permalink to heading "Stack the items vertically"Set orientation to vertical. The up and down arrow keys move the focus.
Disable items
Permalink to heading "Disable items"Set disabled on the group to disable all its items, or on single items. The first group below is disabled, in the second the Center item is.
Keep one item pressed
Permalink to heading "Keep one item pressed"Set deselectable="{false}" for a choice like a billing period, where one option stays selected. multiple ignores it.
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 |
provider | stringThe `id` of the `.provider` this root takes its `rootId` and props from. | No | - |
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. |