PeakUI

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

PropTypeDefaultDescription
selectionMode'single' | 'multiple''single'Whether one or multiple buttons can be selected
selectedKeysIterable<Key>-Controlled selection state
defaultSelectedKeysIterable<Key>-Default selected keys (uncontrolled)
onSelectionChange(keys: Set<Key>) => void-Called when selection changes
disallowEmptySelectionbooleanfalsePrevents 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
isDetachedbooleanfalseWhether buttons are visually separated with gaps
fullWidthbooleanfalseWhether the group fills available width
isDisabledboolean-Disables all buttons in the group
classNamestring-Additional CSS classes
childrenReact.ReactNode-ToggleButtons, each selecting by its id

Every other React Aria ToggleButtonGroup prop is accepted.

ToggleButtonGroup.Separator

PropTypeDefaultDescription
classNamestring-Additional CSS classes
renderDOMRenderFunction<'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.

On this page