Skip to content
Slotted

Toggle group

A set of two-state buttons that share one value, with one tab stop for the whole set.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { ToggleGroup, ToggleGroupDefaultsProvider, ToggleGroupItem } from '@slotted/react';

Value

Each item carries a value, and the group holds the values that are on. The value is an array in every mode, so a group that allows one item at a time holds an array of one. defaultValue sets the starting value of an uncontrolled group and is read once. multiple allows several items on at once. By default, pressing the active item turns it off and the group empties; deselectable={false} in React and [deselectable]="false" in Angular keeps the last item on, and the property has no effect while multiple is set.

One at a time, and none when the active item is pressed again
Several at once
One at a time, always one

Variants

variant sets how the items are drawn. outline, the default, draws the bar’s own edge. ghost draws none, for a bar inside a surface that already has one, such as a toolbar or a card header. The color of an item that is on is the same in both. chips draws separate rounded items that wrap onto the next line and are never joined, with the pressed item in the neutral tone. Use chips for filters that grow with the data.

outline
ghost
chips

Sizes

size takes sm, md or lg and sets the bar and every item to one control step. A bar is as tall as a text field or a button of the same size, so the three line up in one row.

sm
md
lg

Layout

fullWidth makes the items share the whole line; without it the bar is as wide as its labels. attached is true by default and joins the items into one bar, and false spaces them apart. orientation="vertical" stacks the items and writes aria-orientation on the group. The chips variant is never attached.

fullWidth
attached off
vertical

States

An item is pressed when its value is in the group’s value. disabled on an item sets the native disabled attribute, which removes it from the tab order and from the arrow keys while the rest of the bar stays live. An item that is on and disabled keeps both states, so the bar still shows the value. disabled on the group reaches every item that does not state its own.

One item unavailable
Unavailable while on
Whole group unavailable

Marks and words

An item takes a mark before its words. In React the mark is the icon prop of ToggleGroupItem; in Angular it is an element with slToggleGroupIcon inside the button. The mark is sized from the control glyph scale and hidden from assistive technology, so the words beside it name the item.

Marks without words

An item without words holds one mark and takes its accessible name from an aria-label on the item. Without the label the item has no name.

Controlled

Pass value to own the selection. A press calls onValueChange(value, request) in React and emits valueChange in Angular, and the group moves only when the application passes the new array back. The request carries value, previous, reason (pointer or keyboard) and cancel(). In Angular the emitted object is the request, so the new array is $event.value. Here the handler refuses a fourth column with cancel().

2 of 3 columns: sku, quantity

Inside a toolbar

A toolbar is one tab stop with arrow navigation across every control it holds. A toggle group inside a toolbar does not hold a second tab stop, and the toolbar moves focus between the buttons of the group. The group finds the toolbar in the DOM, so nothing is passed. rovingFocus states the choice for a bar that is not a Slotted toolbar.

Inside a field

A toggle group inside a field is named by the field label through aria-labelledby. An aria-label or aria-labelledby written on the group wins over the field label. A group is not a form control: it submits no value.

The carrier confirms the slot the day before.

Keyboard

Tab enters the group once, on the pressed item or on the first item that is not disabled. The arrow keys, Home and End move focus and never change the value. Space and Enter toggle the focused item. A horizontal group answers ArrowLeft and ArrowRight, and a vertical group answers ArrowUp and ArrowDown; the other pair is left to the page so scrolling continues. loop is true by default and wraps focus past the ends; loop={false} in React and [loop]="false" in Angular stops it at the first and last items.

Arrows wrap at the ends
Arrows stop at the ends

Defaults

ToggleGroupDefaultsProvider in React and provideToggleGroupDefaults in Angular set size, variant, orientation, attached, deselectable and loop once for every group under them. A group that states a value of its own keeps it. In the example the second group states variant="outline" and takes the rest from the provider.

Inherits the provider
States its own variant

Accessibility

KeyEffectFocus
Tabmoves focus into the group, which holds one tab stop however many items it hasthe pressed item, or the first item that is not disabled
Spacetoggles the focused itemstays on the item
Entertoggles the focused itemstays on the item
ArrowRightmoves to the next item in a horizontal group and wraps at the end; it never toggles, because an item is a buttonthe item moved to
ArrowLeftmoves to the previous item in a horizontal group and wraps at the start; it never toggles, because an item is a buttonthe item moved to
ArrowDownthe same as ArrowRight in a vertical group, and nothing in a horizontal one so the page keeps scrollingthe item moved to, in a vertical group
ArrowUpthe same as ArrowLeft in a vertical group, and nothing in a horizontal one so the page keeps scrollingthe item moved to, in a vertical group
Homemoves to the first item that is not disabledthe first enabled item
Endmoves to the last item that is not disabledthe last enabled item
  • There is no APG pattern called “toggle group”. The container is a group, each item is the Button pattern’s toggle button (w3.org/WAI/ARIA/apg/patterns/button/), and the movement between them is the Toolbar pattern’s roving tabindex (w3.org/WAI/ARIA/apg/patterns/toolbar/). Citing one and implying the other is how a family ends up with half a keyboard.
  • So the arrows move focus and never toggle, which is the one place toggle group and segmented-control genuinely differ and the one a reader feels. Space and Enter turn the focused item on or off; Home and End reach the ends; a horizontal group ignores the cross axis so the page keeps scrolling with focus inside it.
  • aria-pressed is written in both states, so an item is announced as a toggle rather than as a button that happens to be pressed — and the label never changes with the state, which the Button pattern calls critical. An item with only a mark takes its name from an aria-label the consumer writes.
  • Inside a toolbar the group gives its keyboard up: a bar is already one tab stop with arrow navigation across every control it holds. The bar is found in the DOM by its public identifier rather than through an imported context, so neither depends on the other; rovingFocus states it outright for a bar this library did not write.
  • disabled is the native attribute, because that is also what makes the browser refuse focus and what makes the ring skip the item. The group is not a form control: a set of choices a form submits is checkbox-group for many and segmented-control for one.
  • The target is the item itself, never narrower than it is tall, so the WCAG 2.2 minimum of 24 by 24 holds at the smallest step and the tightest density — and the theme grows every control to 44 under a coarse pointer.

API

ToggleGroup

Props

valuereadonly string[]
Present means the consumer owns the set, for the life of the component. Absent, the group owns it and starts from defaultValue
defaultValuereadonly string[]
What an uncontrolled group starts from, read once. A later change to it does not silently become new state
multiplebooleanDefault false
Whether more than one item may be on at a time. The value stays an array either way, so both bindings hold one shape and a single-select consumer writes an array of one
deselectablebooleanDefault true
Whether the last item on may be turned off. Ignored while multiple is true, where a set may always empty
size'sm' | 'md' | 'lg'Default md
The control step the bar and every item sit at. It reads the control scale, so the bar is as tall as the field beside it
variant'outline' | 'ghost' | 'chips'Default outline
Whether the bar draws its own edge, or draws its items as chips: separate rounded items that wrap, never joined, the pressed one in the neutral tone ink. There is no tone axis: “on” is one color across the system, and a bar that wants the whole palette is button-group around ToggleButtons
orientation'horizontal' | 'vertical'Default horizontal
How the items flow, and which pair of arrows moves focus. A vertical group writes aria-orientation
attachedbooleanDefault true
Whether the items are joined into one bar or spaced apart. A joined bar overlaps its items by one border so a shared column holds one line
fullWidthbooleanDefault false
Whether the items share the whole line. Left alone the bar is as wide as its labels
loopbooleanDefault true
Whether the arrows wrap past the ends of the set
rovingFocusbooleanDefault inferred
Whether the group holds a tab stop of its own. Unstated it is inferred: a group inside a toolbar stands down, because a bar is already one stop with arrow navigation across every control it holds
disabledbooleanDefault false
Unstated rather than false, so an item may state its own. Stated, it reaches every item as the native attribute, which is also what makes the browser skip it with the arrows
value, on ToggleGroupItemstring
On ToggleGroupItemWhat this item turns on, and what the group’s value holds. It is required: an item with no value is a control that changes nothing
icon, on ToggleGroupItemReactNode
On ToggleGroupItemThe mark drawn before the words, sized from the control glyph scale and hidden from the reading order
disabled, on ToggleGroupItemboolean
On ToggleGroupItemOne item out of action while the rest of the bar stays live. An item that is on and unavailable keeps saying both

Events

NamePayloadWhen
onValueChange(value: readonly string[], request: ChangeRequest<readonly string[]>)Before the value moves. The request carries the new array, the array it replaces, the reason — pointer or keyboard — the originating event, and cancel(), which refuses the move before anything commits. Nothing is emitted for a value equal to the one the group already holds.
onClick, onFocus, onBlur, on each itemthe element’s own eventsWhenever that item’s button fires them. ToggleGroupItem composes with the application’s handler rather than replacing it: the application’s runs first, and always runs. Calling preventDefault() in onClick stops the toggle as well.

Composition

render(props: ToggleGroupRootProps | ToggleGroupItemRootProps) => ReactNode
On ToggleGroup, ToggleGroupItemSubstitutes either root and hands it every attribute the family was going to write, including the ref the group needs for its own ring
refRef<HTMLDivElement> | Ref<HTMLButtonElement>
On ToggleGroup, ToggleGroupItemReaches the element. focus() and click() are the element’s own, so no imperative handle is invented for what the platform already ships
useToggleGroup()() => ToggleGroupContextValue
On inside a ToggleGroupWhat an item reads to know whether it is on and how to ask for a change. It throws outside a group rather than rendering a button that toggles nothing
ToggleGroupDefaultsProvider{ attached, deselectable, loop, orientation, size, variant }
On a subtreeThe six decisions an application takes once. A group that states any of them itself still wins
rolestringDefault group
A consumer’s own wins. The family writes group and never toolbar: toolbar is a keyboard model, and it is the toolbar family

Parts

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe group. It carries role="group", data-ui="toggle-group", the axes, and one tab stop for the whole set. It states no frame of its own: a joined bar has one edge, and the items make it between them, which is also what keeps the bar exactly one control tall.
itembutton, or the element ToggleGroupItem’s render returnsYesOne two-state control. It carries aria-pressed in both states and data-pressed when it is on, and its border is always present so that turning it on changes a color and never the geometry.
iconspanNoThe mark an item was given, sized from the control glyph scale and hidden from the reading order. An item with a mark and no words takes its accessible name from the aria-label the consumer writes.
labelspanNoThe words. A box of their own, so a label the bar made too narrow clips at the ellipsis instead of wrapping the item onto a second line — and the string stays whole in the DOM.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every toggle group inside it.

Toggle group tokens

TokenDefault
--slotted-toggle-group-active-backgroundvar(--slotted-tone-secondary-subtle-active, ButtonFace)
--slotted-toggle-group-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-toggle-group-chips-gapvar(--slotted-space-3, 0.375rem)
--slotted-toggle-group-chips-pressed-backgroundvar(--slotted-tone-neutral-solid, CanvasText)
--slotted-toggle-group-chips-pressed-colorvar(--slotted-tone-neutral-on-solid, Canvas)
--slotted-toggle-group-chips-radiusvar(--slotted-radius-full, 9999px)
--slotted-toggle-group-colorvar(--slotted-text, ButtonText)
--slotted-toggle-group-disabled-border-colorvar(--slotted-disabled-border, GrayText)
--slotted-toggle-group-disabled-colorvar(--slotted-disabled-foreground, GrayText)
--slotted-toggle-group-disabled-pressed-backgroundvar(--slotted-disabled-background, ButtonFace)
--slotted-toggle-group-hover-backgroundvar(--slotted-tone-secondary-subtle-hover, ButtonFace)
--slotted-toggle-group-pressed-backgroundvar(--slotted-tone-accent-subtle-active, Highlight)
--slotted-toggle-group-pressed-border-colorvar(--slotted-tone-accent-border, Highlight)
--slotted-toggle-group-pressed-colorvar(--slotted-tone-accent-text, HighlightText)
--slotted-toggle-group-pressed-hover-backgroundvar(--slotted-tone-accent-subtle-hover, Highlight)
--slotted-toggle-group-radiusvar(--slotted-control-radius, 4px)
--slotted-toggle-group-shadowvar(--slotted-control-shadow, none)

Theme tokens it reads

TokenDefault
--slotted-control-border-colorSet per scheme by the theme
--slotted-control-border-widthvar(--slotted-border-width-thin)
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-font-weightvar(--slotted-font-weight-semibold)
--slotted-control-gap-lgcomfortable 8px · compact 6px · dense 6px
--slotted-control-gap-mdcomfortable 8px · compact 6px · dense 4px
--slotted-control-gap-smcomfortable 6px · compact 4px · dense 4px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-letter-spacing0
--slotted-control-line-height1
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-shadowSet per scheme by the theme
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-instant1ms
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-weight-medium500
--slotted-radius-full9999px
--slotted-space-30.375rem
--slotted-textSet per scheme by the theme
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-subtle-activeSet per scheme by the theme
--slotted-tone-accent-subtle-hoverSet per scheme by the theme
--slotted-tone-accent-textSet per scheme by the theme
--slotted-tone-neutral-on-solidSet per scheme by the theme
--slotted-tone-neutral-solidSet per scheme by the theme
--slotted-tone-neutral-solid-hoverSet per scheme by the theme
--slotted-tone-secondary-subtle-activeSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme