Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

ButtonGroup

A row of related buttons, such as the actions of a form or a card. A row of related buttons. When you give the row a name, a screen reader (software that reads the screen aloud) tells people which buttons belong together.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a button group for buttons that act on the same thing: the actions of a form, or the footer of a card.
  • Keep one main button on the screen, for the main next step. The group doesn’t style or order buttons by importance: you do.
  • Name the group when the buttons need a context their own labels don’t give. In a Toolbar the name is required.
  • The joined look (kv-button-group--attached) changes only how it looks. It doesn’t say which button is chosen, so for a choice use a RadioGroup or a Toggle.
  • Each button is its own Tab stop (a place where the Tab key stops). If the row should be one Tab stop, with the arrow keys moving between the buttons, use a Toolbar.
  • Not for a list of links to pages: use Navigation.

Example#

Use cases#

The buttons’ own labels say enough, so this group needs no name. Without a name it is a plain div, and a screen reader hears nothing extra.

Waste collection

The next collection is on Tuesday. Put the bin out by 6 am.

A group named by the card’s heading

When the buttons need the context of the card, point aria-labelledby at the id of its visible heading. A screen reader then says the heading and “group” as focus enters the group.

Waste collection

The next collection is on Tuesday. Put the bin out by 6 am.

Two related actions joined in a strip

A staff member edits a decision letter and needs undo and redo side by side. Add the class kv-button-group--attached and the buttons touch and share borders. Each button is still its own Tab stop. The name tells a screen reader user what the pair is for.

You are editing the decision letter.

Props in this example:

A strip of filters for a list of cases

A caseworker narrows a list with filters that can be on together. The group holds no state: each Toggle is on or off for itself, and a screen reader says which are pressed. The name says what the strip does, and the count under it tells everyone how many cases match.

Showing 4 of 4 cases
  • Building permit, Storgatan 4
  • Parking permit, Hamngatan 12
  • Noise complaint, Skolgatan 7
  • Home care application, Bokgatan 2

Props in this example:

Groups of case actions in a toolbar

With three or more actions, a Toolbar is one Tab stop and the arrow keys move between the buttons, across groups. Name each group so a screen reader says which one focus has entered. Use Toolbar.Button for the buttons. Toolbar.Group is the same group, always joined into one strip.

Warning: Don't forget

Name every group in a Toolbar. Without a name, a screen reader user hears no group as focus moves between them (1.3.1 Info and Relationships, 4.1.2 Name, Role, Value). In development, a warning tells you when a name is missing.

Accessibility#

What it does for you#

  • APG pattern: none of its own. The WAI-ARIA group role, for related controls (the Toolbar pattern uses it to group controls). Outside a toolbar it is layout, and the buttons keep the Button's pattern.
  • Deviations: none
  • Native elements used: <div>. role="group" only when it has a name.

A ButtonGroup is a row of related Buttons: the footer of a Card, the actions of a form, or the groups in a Toolbar (where it is Toolbar.Group). It holds no state and handles no keys.

PartElement / roleARIANotes
ButtonGroup<div> → grouparia-label or aria-labelledbyrole="group" only when it has a name, so a screen reader says "Case actions, group" as focus enters it.
without a nameno roleA plain <div>: an unnamed Card footer adds no empty group to the accessibility tree
in a Toolbarname requiredA dev warning when a group in a Toolbar.Root has no name (1.3.1, 4.1.2).
classNameadd kv-button-group--attachedJoins the buttons into one strip. Look only: no role, ARIA or key changes.

useButtonGroup({ isNamed }) gives the same groupProps for your own element.

It announces nothing.

What you need to do#

  • In a Toolbar, always name the group, from your translations (aria-label) or by pointing aria-labelledby at visible text. A name that repeats the toolbar's adds nothing: say what the buttons are ("Text style"), not "Group".
  • Outside a toolbar a name is optional. A group is worth naming when the buttons need a shared context that their own labels don't give. A Card footer that wants it uses aria-labelledby pointing at the card's heading.
  • Keep one main (primary) button per view. The group doesn't order or style by importance.
  • A group that must be one Tab stop with the arrow keys is a Toolbar, not a ButtonGroup.

Visual and modes#

  • Focus indicator: each button's own (2.4.7 Focus Visible (AA); 2.4.13 Focus Appearance (AAA)). In an attached group the focused button is raised above its neighbours, and a halo in the page colour fills the gap between the ring and the button, so the ring never sits on a neighbouring pressed fill (in forced colours the ring uses the system's text colour). Two pressed buttons in a row keep a divider between them. A story asserts these cases.
  • Target size: each button's own. The default theme keeps the buttons' targets separate: each keeps at least 24 × 24 CSS px and does not overlap its neighbour's target. An attached group has no gap: each button keeps its own size, and only the borders overlap (2.5.8 Target Size (Minimum) (AA)).
  • forced-colors behaviour: the buttons draw their own edges. The group draws none, and the thin line between groups in a toolbar uses the system's disabled-text colour and is decoration only. The shared edges of an attached group are the buttons' own edges in the system's button-text colour (a pressed toggle uses the system's highlight colour). The forced-colours sweep checks that every button keeps a visible boundary and shows a focus indicator when it has focus (1.4.11).
  • reduced-motion behaviour: no motion.
  • Reflow: an attached group never stacks or wraps, and a button wraps its own label (1.4.10). A spaced group outside a toolbar stacks its buttons at full width below 40rem. In a toolbar the groups wrap one by one, so there is no horizontal scrolling at 320 CSS px (1.4.10).

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • Never obscured by: ButtonGroup renders no overlay.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): the group role and its name say which buttons belong together.
  • 2.1.1 Keyboard (A), 2.4.3 Focus Order (A): every button is a Tab stop in DOM order (the Tab rows above).
  • 4.1.2 Name, Role, Value (A): role="group" with a name.

Keyboard#

ButtonGroup has no keys of its own. What you put in it keeps its own keys.

API reference#

import { ButtonGroup, useButtonGroup } from '@kvirn-ui/react'

ButtonGroup

Renders <div>, with the role group only when it has a name. It takes every attribute of a <div> except role, and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
classNamestring–Your own classes. They join the part’s class and never replace it. Add kv-button-group--attached to join the buttons into one strip. It changes the look only: it does not choose a button.
Class or attributeValuesMeaning
kv-button-groupalwaysThe part class. The default theme lays the buttons in a row. Outside a toolbar, a group that is not joined stacks its buttons below 40rem.
kv-button-group--attachedyou add itThe buttons touch and share borders. Only the outer corners are rounded.
role="group"only with a nameSet when aria-label or aria-labelledby is given. Not passed directly.
aria-label, aria-labelledbyyou set oneThe name of the group. Required in a Toolbar.

useButtonGroup

Options

PropTypeDefaultDescription
isNamedbooleanfalseWhether the group has a name (aria-label or aria-labelledby). A named group gets role="group".

Result

PropTypeDefaultDescription
groupPropsButtonGroupPartProps–Spread on a <div>: the class and role="group" when named.

Known issues#

  • Keyboard tests run in Chromium only. The keyboard rows run in Vitest browser mode on Chromium, not in WebKit. Test in Safari with your own assistive technology.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassbutton-group.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassbutton-group.test.tsx (axe and role, name and state): with aria-label it is a group with that name
Keyboard contract rows (Vitest browser)passn/apass2 rows, each names a test in button-group.test.tsx
Focus order and managementpassn/apassbutton-group.test.tsx asserts focus
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Tab into a named group: its name and "group" are read with the first button.
  • Each button is one stop, in reading order.
  • An unnamed group (a card footer) adds no extra "group" announcement.
  • At 320px or 400% zoom an attached group's labels wrap and nothing is cut off.
  • In a Toolbar each group's name is read as focus enters it.