oakoss/ui
Components

Toggle Group

A set of toggles that selects one option, or several.

SourceReact Aria

Installation

npx shadcn@latest add @oakoss/toggle-group

Usage

import {
  ToggleGroup,
  ToggleGroupItem,
} from '#/components/ui/inputs/toggle-group';

<ToggleGroup aria-label="Alignment">
  <ToggleGroupItem id="start">Start</ToggleGroupItem>
  <ToggleGroupItem id="center">Center</ToggleGroupItem>
  <ToggleGroupItem id="end">End</ToggleGroupItem>
</ToggleGroup>;

Examples

Multiple

selectionMode="multiple" lets any number of items be on, as in a text formatting toolbar.

Joined

joined sets the items edge to edge, sharing their borders. Here disallowEmptySelection keeps one view always chosen.

Accessibility

React Aria renders single selection, the default, as a radiogroup of radio items, and multiple selection as a toolbar of buttons with aria-pressed. Name the group with aria-label or aria-labelledby.

  • Focus: the group is one Tab stop. Arrow keys move focus between items without changing the selection, and stop at the ends.
  • Deselecting: in single selection, pressing the selected item clears it, which a radio group normally can't. Set disallowEmptySelection to keep one selected.
  • Names: an item can't see its group's size, so an icon-only item's name isn't checked in the type. Give each one an aria-label.
  • Target size: items sit 8px apart, closer than their 44px targets would allow, so the targets are off inside a group.
  • Disabled: isDisabled disables every item and marks the group aria-disabled.
KeyAction
TabMoves focus into or out of the group
ArrowLeft / ArrowRightMoves focus between items when horizontal; reversed right to left
ArrowUp / ArrowDownMoves focus between items when vertical
Space / EnterSelects or toggles the focused item

Follows the WAI-ARIA radio group and toolbar patterns.

API

ToggleGroup

Extends React Aria's ToggleButtonGroup.

Props

Prop

Type

Also accepts all React Aria ToggleButtonGroup props.

Data attributes

AttributePresent when
data-slottoggle-group, unless you pass your own
data-orientationAlways; horizontal or vertical
data-sizeAlways; the items' default size
data-variantAlways; the items' default look
data-joinedjoined is set
data-disabledisDisabled is set

ToggleGroupItem

Extends React Aria's ToggleButton, styled as a Toggle.

Props

Prop

Type

Also accepts all React Aria ToggleButton props. id is required: it's the key the group selects, and an item without one isn't part of the group's selection.

Data attributes

Toggle's, with data-slot set to toggle-group-item.