suiss/ui · Component
Toggle Group
A set of toggle buttons for choosing one or several related options, including segmented controls.
Toggle Group is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
ToggleGroup wraps the toggle group primitive of the chosen base and holds ToggleGroupItem buttons that share the group's variant and size. Items can be spaced apart, attached into one bar, or combined into a segmented control.
Use a segmented group to switch between views, such as day, week and month. For a single on-off option, use Toggle; for switching panels of content, use Tabs.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Usage
Composition
Use the following composition to build a ToggleGroup:
Examples
Outline
Set variant="outline" on the group to give every spaced item a border.
Attached
Use spacing={0} with the outline variant to join items into one bordered bar.
Sizes
Use the size prop on the group to set the height and padding of every item: sm, default or lg.
Segmented
With spacing={0} and the default variant, the group becomes a segmented control: a filled track where a raised pill slides to the pressed item. This one allows a single choice.
Segmented vertical
Set orientation="vertical" to stack a segmented control.
Segmented multiple
A segmented control that allows several pressed items. The sliding pill appears only when exactly one item is pressed.
Segmented sizes
Compares segmented controls in each size.
Single selection
Allows only one pressed item in an outline group; pressing an item releases the others.
Vertical
Set orientation="vertical" to stack attached outline items.
Disabled
Set disabled on the group to disable every item.
Set isDisabled on the group to disable every item.
Accessibility
- The group has the
grouprole; name it witharia-label. - Each item is a
buttonwitharia-pressed. Icon-only items need anaria-label.
- The group has the
grouprole; name it witharia-label. - With
type="multiple"each item is abuttonwitharia-pressed. Withtype="single"each item has theradiorole witharia-checked. Icon-only items need anaria-label.
- With
selectionMode="multiple"the group has thetoolbarrole and each item is abuttonwitharia-pressed. With"single"the group is aradiogroupand each item has theradiorole witharia-checked. Name the group witharia-label. Icon-only items need anaria-label.
- Arrow keys move focus between items: left and right in a horizontal group, up and down in a vertical one.
Spacetoggles the focused item.
disabledon the group disables every item.
isDisabledon the group disables every item.
API Reference
ToggleGroup
The container that holds the pressed values and shared styles.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
spacing | number | 2 |
orientation | "horizontal" | "vertical" | "horizontal" |
multiple | boolean | false |
defaultValue | any[] | — |
value | any[] | — |
onValueChange | (value: any[]) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
spacing | number | 2 |
orientation | "horizontal" | "vertical" | "horizontal" |
type | "single" | "multiple" | — |
defaultValue | string | string[] | — |
value | string | string[] | — |
onValueChange | (value: string | string[]) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
spacing | number | 2 |
orientation | "horizontal" | "vertical" | "horizontal" |
selectionMode | "single" | "multiple" | "single" |
defaultSelectedKeys | Iterable<Key> | — |
selectedKeys | Iterable<Key> | — |
onSelectionChange | (keys: Set<Key>) => void | — |
isDisabled | boolean | false |
Also accepts the props of the primitive part it wraps.
The group's value is an array of item values. Use defaultValue for an uncontrolled group, or value with onValueChange to control it. Set multiple to let several items be pressed at once; without it, pressing an item releases the others.
Set type to "single" or "multiple"; it is required. With "single" the value is one item value, and pressing an item releases the others. With "multiple" it is an array of item values. Use defaultValue for an uncontrolled group, or value with onValueChange to control it.
The selection is a set of item ids. Use defaultSelectedKeys for an uncontrolled group, or selectedKeys with onSelectionChange to control it. Set selectionMode="multiple" to let several items be pressed at once; the default, "single", releases the others when an item is pressed.
variant and size style every item; values set on a ToggleGroupItem are overridden by the group's. spacing sets the gap between items on the spacing scale. orientation also sets which arrow keys move focus.
The segmented indicator's position is set through --active-toggle-left, --active-toggle-top, --active-toggle-width and --active-toggle-height. Its animation is removed when reduced motion is preferred.
The group sets data-variant, data-size, data-spacing and data-orientation.
The group sets data-variant, data-size and data-spacing, plus data-horizontal or data-vertical.
ToggleGroupItem
A toggle button within the group, identified by its value.
| Prop | Type | Default |
|---|---|---|
value | string | — |
disabled | boolean | false |
A toggle button within the group, identified by its id.
| Prop | Type | Default |
|---|---|---|
id | Key | — |
isDisabled | boolean | false |
Also accepts the props of the primitive part it wraps.
Each part sets a data-slot, such as toggle-group-item or toggle-group-indicator, for styling.
Web preview
ToggleGroup is built on the toggle group primitive and lays out ToggleGroupItem toggles in a row or a column.
With spacing={0}, the default variant becomes a segmented control and the outline variant joins items into one bordered strip. Use the segmented style for switching views and the spaced style for toolbars.
Installation
- Install the following dependencies:
-
Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/toggle-group.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/toggle-group.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/toggle-group.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Outline
The outline variant.
Web preview
Attached
Outlined items joined into one strip with spacing={0}.
Web preview
Sizes
The sm, default and lg sizes.
Web preview
Segmented
A single-selection segmented control for calendar views.
With spacing={0}, the default variant becomes a segmented control on a bg-secondary track. The selected item is marked by a raised indicator that slides to the new item when the selection changes, over 300 ms. The indicator respects the system reduce motion setting and is hidden when the selected item is disabled.
Web preview
Segmented vertical
The segmented control in a vertical orientation.
Web preview
Segmented multiple
A segmented control that allows several selected items. With multiple, each pressed item is raised on its own.
Web preview
Segmented sizes
Segmented controls at each size.
Web preview
Single selection
Outlined alignment options where only one item can be pressed.
Web preview
Vertical
Joined outline items stacked in a column.
Web preview
Disabled
A disabled group.
Web preview
Accessibility
- A single-selection group has the
radiogrouprole and its items have theradiorole. - A
multiplegroup has thegrouprole and its items have thetogglebuttonrole. - Items report checked and disabled states to VoiceOver and TalkBack.
- The sliding segmented indicator is hidden from VoiceOver and TalkBack.
- Icon-only items need an
accessibilityLabel; label the group itself withaccessibilityLabelwhen its purpose is not clear from context.
API Reference
ToggleGroup
Container that holds the shared value.
| Prop | Type | Default |
|---|---|---|
spacing | number | 2 |
Also accepts the props of React.ComponentProps<typeof ToggleGroupPrimitive>.
The value is always an array of strings. Use value and onValueChange for controlled use, or defaultValue. Set multiple to allow more than one pressed item; without it, pressing an item replaces the selection. spacing sets the gap between items in steps of 4 points, and 0 joins them. orientation switches between a row and a column, and disabled on the group turns off every item.
ToggleGroupItem
One toggle in the group.
Also accepts the props of React.ComponentProps<typeof TogglePrimitive>.
Give each item a value. Items take the same variant and size styles as Toggle. Give icon-only items an accessibilityLabel.
ToggleGroupIcon
Icon sized for a toggle group item.
Also accepts the props of React.ComponentProps<typeof Icon>.
Sizes the icon to 16 points.
ToggleGroup
Container that holds the shared value.
| Parameter | Type | Default |
|---|---|---|
selection | Binding<Value?> | - |
variant | ToggleVariant | .default |
size | ToggleSize | .default |
spacing | Int | 2 |
orientation | Axis | .horizontal |
content | () -> Content | - |
ToggleGroupItem
One toggle in the group.
| Parameter | Type | Default |
|---|---|---|
value | Value | - |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
Overloads: init(value:label:), init(_:value:).
ToggleGroup
Container that holds the shared value.
| Parameter | Type | Default |
|---|---|---|
value | String? | - |
onValueChange | (String?) -> Unit | - |
modifier | Modifier | Modifier |
variant | ToggleVariant | ToggleVariant.Default |
size | ToggleSize | ToggleSize.Default |
spacing | Int | 2 |
orientation | Orientation | Orientation.Horizontal |
enabled | Boolean | true |
content | @Composable () -> Unit | - |
values | Set<String> | - |
onValuesChange | (Set<String>) -> Unit | - |
Overloads: ToggleGroup(value, onValueChange, content), ToggleGroup(values, onValuesChange, content).
ToggleGroupItem
One toggle in the group.
| Parameter | Type | Default |
|---|---|---|
value | String | - |
modifier | Modifier | Modifier |
enabled | Boolean | true |
interactionSource | MutableInteractionSource? | null |
content | @Composable RowScope.() -> Unit | - |
text | String | - |
Overloads: ToggleGroupItem(value, interactionSource, content), ToggleGroupItem(text, value).