Skip to main content

Toggle Group

View as Markdown

A set of toggle buttons where one or several can be pressed.

Read more about installing Components and Primitives.

Set multiple so items press independently. Bold, italic and underline below combine.

Set orientation to vertical. The up and down arrow keys move the focus.

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.

Set deselectable="{false}" for a choice like a billing period, where one option stays selected. multiple ignores it.

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.

Provides the shared state and wraps the items. Renders a <div> element with role="group".

NameDescriptionRequiredDefault
defaultValuestring[]
The initial value of the pressed items when rendered. Use when you don't need to control the value of the toggle group.
No
-
disabledboolean
Whether all items of the toggle group are disabled.
No
-
multipleboolean
Whether multiple items can be pressed at the same time.
No
false
deselectableboolean
Whether the pressed item can be deselected again. Ignored when `multiple` is `true`.
No
true
loopFocusboolean
Whether the keyboard navigation loops from the last item to the first, and vice versa.
No
true
rovingFocusboolean
Whether the items use a roving tabindex, so the whole group is a single tab stop and the arrow keys move the focus.
No
true
orientationEnum\Orientation
The orientation of the toggle group.
No
Horizontal
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
rootIdstring
The root ID of the component, used for hydration and identification.
No
-
idsarray
The IDs of of the component parts for composition.
No
[]
autoMountboolean
Whether the client initializes the component on its own. Set it to false to mount it yourself with `mount()`
No
true
providerstring
The `id` of the `.provider` this root takes its `rootId` and props from.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Rendered data attributes

AttributeDescription
data-toggle-group-root<uid>
data-disabledPresent when disabled
data-multiple
data-orientationThe orientation of the toggle-group

A toggle button of the group. Renders a <button> element.

NameDescriptionRequiredDefault
valuestring
The value of the toggle group item.
Yes
-
disabledboolean
Whether the item is disabled. An item of a disabled group is always disabled.
No
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Rendered data attributes

AttributeDescription
data-toggle-group-item<uid>
data-pressedPresent when pressed
data-disabledPresent when disabled
data-orientationThe orientation of the item
NameTypeDescription
valuestring[]The value of the toggle group.
setValue(value: string[]) => voidSets the value of the toggle group.
getItemState(props: ItemProps) => ItemStateReturns the state of the toggle item.
getRootState() => RootStateReturns the state of the toggle group
KeyDescription
TabMoves focus to either the pressed item or the first item in the group.
SpaceActivates/deactivates the item.
EnterActivates/deactivates the item.
ArrowDownMoves focus to the next item in the group.
ArrowRightMoves focus to the next item in the group.
ArrowUpMoves focus to the previous item in the group.
ArrowLeftMoves focus to the previous item in the group.
HomeMoves focus to the first item.
EndMoves focus to the last item.