ToggleButtonGroup
Groups toggle buttons for single or multiple selection
Usage
import {ToggleButton, ToggleButtonGroup} from "@peakui/react";Each ToggleButton in a group selects by its id, which is also the key in selectedKeys and defaultSelectedKeys.
Anatomy
import {ToggleButton, ToggleButtonGroup} from "@peakui/react";
export default () => (
<ToggleButtonGroup selectionMode="multiple">
<ToggleButton id="first">First</ToggleButton>
<ToggleButton id="second">
<ToggleButtonGroup.Separator />
Second
</ToggleButton>
<ToggleButton id="third">
<ToggleButtonGroup.Separator />
Third
</ToggleButton>
</ToggleButtonGroup>
);Examples
Sizes
The size set on the group reaches every button, so the group stays uniform.
Orientation
Full Width
Selection Mode
selectionMode="single" renders a radiogroup of mutually exclusive buttons; selectionMode="multiple" renders a toolbar of independent toggles.
Detached
isDetached separates the buttons with gaps instead of joining their edges.
Without Separator
Omit <ToggleButtonGroup.Separator /> and the buttons sit flush against each other.
Controlled
Disabled State
isDisabled on the group disables every button; a button's own isDisabled={false} opts it back in.
Customization
Tailwind CSS
Pass utilities through className. They are merged with the group's own classes.
<ToggleButtonGroup className="gap-2" isDetached selectionMode="multiple">
<ToggleButton id="first">First</ToggleButton>
<ToggleButton id="second">Second</ToggleButton>
</ToggleButtonGroup>Global CSS
Override the group's classes in the components layer.
@layer components {
.toggle-button-group--detached {
@apply gap-2;
}
.toggle-button-group__separator {
@apply opacity-25;
}
}Styling Reference
PeakUI follows the BEM methodology, so variants and states are plain class names you can target.
CSS Classes
The ToggleButtonGroup component uses these CSS classes (view source styles):
Base & Layout Classes
.toggle-button-group- Base container styles.toggle-button-group--horizontal- Horizontal orientation.toggle-button-group--vertical- Vertical orientation.toggle-button-group--full-width- Full width modifier.toggle-button-group__separator- Separator element inside a button
Modifier Classes
.toggle-button-group--detached- Detached mode (separated buttons with gaps)
Slots
[data-slot="toggle-button-group"][data-slot="toggle-button-group-separator"]
API Reference
ToggleButtonGroup
| Prop | Type | Default | Description |
|---|---|---|---|
selectionMode | 'single' | 'multiple' | 'single' | Whether one or multiple buttons can be selected |
selectedKeys | Iterable<Key> | - | Controlled selection state |
defaultSelectedKeys | Iterable<Key> | - | Default selected keys (uncontrolled) |
onSelectionChange | (keys: Set<Key>) => void | - | Called when selection changes |
disallowEmptySelection | boolean | false | Prevents clearing all selections; keeps one option selected with selectionMode="single" |
orientation | 'horizontal' | 'vertical' | 'horizontal' | Layout direction |
size | 'sm' | 'md' | 'lg' | - | Size propagated to child ToggleButtons |
isDetached | boolean | false | Whether buttons are visually separated with gaps |
fullWidth | boolean | false | Whether the group fills available width |
isDisabled | boolean | - | Disables all buttons in the group |
className | string | - | Additional CSS classes |
children | React.ReactNode | - | ToggleButtons, each selecting by its id |
Every other React Aria ToggleButtonGroup prop is accepted.
ToggleButtonGroup.Separator
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
render | DOMRenderFunction<'span', undefined> | - | Replaces the rendered element. It must still return a <span> |
Place it inside a ToggleButton (not between them), on every button except the first.