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#
import { Button, ButtonGroup } from '@kvirn-ui/react'
import { useButtonGroupTexts } from './texts.ts'
export function DefaultButtonGroup() {
const { texts } = useButtonGroupTexts()
return (
<ButtonGroup aria-label={texts.groupName}>
<Button className="kv-button--primary">{texts.send}</Button>
<Button>{texts.saveDraft}</Button>
</ButtonGroup>
)
}Use cases#
The actions in a card footer
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.
import { Button, ButtonGroup, Card } from '@kvirn-ui/react'
import { useButtonGroupTexts } from './texts.ts'
export function CardFooter() {
const { texts } = useButtonGroupTexts()
return (
<Card.Root>
<Card.Body className="kv-prose">
<h4>{texts.cardTitle}</h4>
<p>{texts.cardBody}</p>
</Card.Body>
<Card.Footer>
<ButtonGroup>
<Button className="kv-button--primary">{texts.orderExtra}</Button>
<Button>{texts.pause}</Button>
</ButtonGroup>
</Card.Footer>
</Card.Root>
)
}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.
import { Button, ButtonGroup, Card } from '@kvirn-ui/react'
import { useId } from 'react'
import { useButtonGroupTexts } from './texts.ts'
export function NamedByHeading() {
const { texts } = useButtonGroupTexts()
const headingId = useId()
return (
<Card.Root>
<Card.Body className="kv-prose">
<h4 id={headingId}>{texts.cardTitle}</h4>
<p>{texts.cardBody}</p>
</Card.Body>
<Card.Footer>
<ButtonGroup aria-labelledby={headingId}>
<Button className="kv-button--primary">{texts.orderExtra}</Button>
<Button>{texts.pause}</Button>
</ButtonGroup>
</Card.Footer>
</Card.Root>
)
}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.
import { Button, ButtonGroup } from '@kvirn-ui/react'
import { useButtonGroupTexts } from './texts.ts'
export function Attached() {
const { texts } = useButtonGroupTexts()
return (
<>
<p>{texts.letterIntro}</p>
<ButtonGroup className="kv-button-group--attached" aria-label={texts.historyName}>
<Button>{texts.undo}</Button>
<Button>{texts.redo}</Button>
</ButtonGroup>
</>
)
}Props in this example:
classNameon ButtonGroup
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.
- Building permit, Storgatan 4
- Parking permit, Hamngatan 12
- Noise complaint, Skolgatan 7
- Home care application, Bokgatan 2
import { ButtonGroup, Toggle } from '@kvirn-ui/react'
import { useState } from 'react'
import { useButtonGroupTexts } from './texts.ts'
export function FilterStrip() {
const { texts } = useButtonGroupTexts()
const [isMine, setIsMine] = useState(false)
const [isOverdue, setIsOverdue] = useState(false)
const [needsReply, setNeedsReply] = useState(false)
const shown = texts.cases.filter(
(item) =>
(!isMine || item.isMine) &&
(!isOverdue || item.isOverdue) &&
(!needsReply || item.needsReply),
)
return (
<div>
<ButtonGroup className="kv-button-group--attached" aria-label={texts.filterName}>
<Toggle pressed={isMine} onPressedChange={setIsMine}>
{texts.assignedToMe}
</Toggle>
<Toggle pressed={isOverdue} onPressedChange={setIsOverdue}>
{texts.overdue}
</Toggle>
<Toggle pressed={needsReply} onPressedChange={setNeedsReply}>
{texts.needsReply}
</Toggle>
</ButtonGroup>
<output>{texts.showing(shown.length, texts.cases.length)}</output>
<ul>
{shown.map((item) => (
<li key={item.id}>{item.text}</li>
))}
</ul>
</div>
)
}Props in this example:
classNameon ButtonGroup
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.
import { ButtonGroup, Toolbar } from '@kvirn-ui/react'
import { useButtonGroupTexts } from './texts.ts'
export function InAToolbar() {
const { texts } = useButtonGroupTexts()
return (
<Toolbar.Root aria-label={texts.toolbarName}>
<ButtonGroup aria-label={texts.handlingName}>
<Toolbar.Button>{texts.assignToMe}</Toolbar.Button>
<Toolbar.Button>{texts.handOver}</Toolbar.Button>
</ButtonGroup>
<ButtonGroup aria-label={texts.documentsName}>
<Toolbar.Button>{texts.addNote}</Toolbar.Button>
<Toolbar.Button>{texts.print}</Toolbar.Button>
</ButtonGroup>
</Toolbar.Root>
)
}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
grouprole, 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.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| ButtonGroup | <div> → group | aria-label or aria-labelledby | role="group" only when it has a name, so a screen reader says "Case actions, group" as focus enters it. |
| without a name | no role | A plain <div>: an unnamed Card footer adds no empty group to the accessibility tree | |
| in a Toolbar | name required | A dev warning when a group in a Toolbar.Root has no name (1.3.1, 4.1.2). | |
className | add kv-button-group--attached | Joins 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 pointingaria-labelledbyat 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-labelledbypointing 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.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | – | 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 attribute | Values | Meaning |
|---|---|---|
kv-button-group | always | The 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--attached | you add it | The buttons touch and share borders. Only the outer corners are rounded. |
role="group" | only with a name | Set when aria-label or aria-labelledby is given. Not passed directly. |
aria-label, aria-labelledby | you set one | The name of the group. Required in a Toolbar. |
useButtonGroup
Options
| Prop | Type | Default | Description |
|---|---|---|---|
isNamed | boolean | false | Whether the group has a name (aria-label or aria-labelledby). A named group gets role="group". |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
groupProps | ButtonGroupPartProps | – | 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 + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | button-group.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | button-group.test.tsx (axe and role, name and state): with aria-label it is a group with that name |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 2 rows, each names a test in button-group.test.tsx |
| Focus order and management | pass | n/a | pass | button-group.test.tsx asserts focus |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
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.