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.

Toolbar

A row of related controls with one Tab stop and the arrow keys between them, so a keyboard user passes a long row of formatting buttons with a single Tab. A row of related controls that is one Tab stop. The arrow keys move between the controls, and a control that isn’t available stays reachable, and a screen reader tells the user it can’t be used (each one words it differently).

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

When to use it#

  • Use a toolbar for three or more related controls, such as the formatting buttons above a text editor. It is one Tab stop (a place where the Tab key stops): someone using a keyboard presses Tab once to get in, then the arrow keys to move between the controls.
  • Name the toolbar and each group, so a screen reader (software that reads the screen aloud) says where focus is.
  • Not for two controls or fewer: separate Buttons or a ButtonGroup are easier to find.
  • Not for moving between pages: use Navigation. A row of text buttons that navigates is a nav, not a toolbar.

Example#

Use cases#

Toggles that change a text’s style

A caseworker writes a message to a resident. A style such as bold is on or off, so use a Toolbar.Toggle: a screen reader says which styles are on (aria-pressed) and the name never changes. Point the toolbar at the element it changes with aria-controls.

Your application has been received.

Props in this example:

A toolbar with only icons

A message editor has little room, so each control shows an icon. Give every icon button a name with aria-label from your own translations. Without one a screen reader says only “button”. Name each group too.

Information: Did you know?

Not everyone knows what an icon means. Where your readers might not, also show a tooltip with the same name.

A command that isn’t available now

Redo has nothing to redo yet. The button stays in the arrow-key order and a screen reader says it is unavailable, so users can find it. Pressing it does nothing. Say why in text next to the toolbar, and link it to the button with aria-describedby, as the example does.

Redo is not available yet: you have not undone anything.

Props in this example:

Information: Did you know?

In a toolbar focusableWhenDisabled is on by default, unlike on a plain Button. Turn it off only when the whole toolbar is disabled.

A link or a control that opens something

A control that isn’t a button joins the toolbar through Toolbar.Item and as: a link, a Popover trigger or a Listbox trigger. A plain control placed in the toolbar is a Tab stop of its own and the arrows skip it.

Props in this example:

  • as on Toolbar.Item

Warning: Don't forget

A Listbox in a toolbar needs native="never" on its Listbox.Root. Otherwise a touch device renders a native select with no name, and the item never appears. See What you need to do.

A column of commands

A toolbar beside or below something, such as the actions for a row in a list. Down and Up move between the controls, and the toolbar says it is vertical, so a screen reader can tell.

Props in this example:

Information: Did you know?

The default theme only draws horizontal toolbars, so the column layout is your own CSS. The arrows follow the DOM order whatever the layout.

Focus stops at the first and last control

The arrows wrap from the last control to the first by default, as in the APG example. Set loop to false when the controls run from one end to the other, as in a map or document viewer, so focus stops at the ends.

Home and End still jump to the first and last control.

Props in this example:

  • loop on Toolbar.Root

Accessibility#

What it does for you#

  • APG pattern: Toolbar, with toggle buttons (Button, aria-pressed)
  • Deviations: none. APG makes wrapping, and Home and End, optional: the choices are stated below. A control that owns keys keeps them (a Listbox trigger's Home and End), which APG allows.
  • Native elements used: <div role="toolbar">, and <button> for Toolbar.Button and Toolbar.Toggle. The roving tabindex is the toolbar's.

A Toolbar is one Tab stop for a row of related controls, with the arrow keys between them, so a keyboard user passes a long row of formatting buttons with one Tab. Use it for three or more controls (APG). With fewer, separate buttons or a ButtonGroup are easier to find. The same arrow keys, in the same order, whatever the width: a toolbar that doesn't fit wraps, and the arrows follow the DOM across rows.

PartElement / roleARIANotes
Toolbar.Root<div> (or <section> with as="section") → toolbararia-label or aria-labelledbyA name is required (a dev warning without one; 4.1.2 Name, Role, Value, A). aria-orientation="vertical" only when vertical: horizontal is the role's default. aria-controls (yours) points at the element it acts on, when there is one
Toolbar.Group<div> → group (it is ButtonGroup)aria-label or aria-labelledbyA name is required in a toolbar (a dev warning; 1.3.1 Info and Relationships, A). A group is always attached (it adds kv-button-group--attached, look only). The hairline between groups is drawn by the theme: there is no role="separator" to announce
Toolbar.Button<button> → buttonnone, aria-disabled="true" when disabledA Button, and an item. focusableWhenDisabled defaults to true here, so a disabled button stays reachable and is read as unavailable. Its contract is the Button's
Toolbar.Toggle<button> → buttonaria-pressed, aria-disabled="true" when disabledA Toggle, and an item. The name never changes with the state
Toolbar.Itemany focusable elementthe element's own, aria-disabled="true" when disabledAllowed elements: Root div (default) or section; another tag warns once in development. Item's as is a focusable component or tag, and its props are plain props of the item. Makes a control an item through as: a Listbox trigger, a Popover trigger, a Link, an <input>. Without as it is a <button type="button">. A dev warning when the element isn't focusable by itself (2.1.1 Keyboard, A). disabled works as on Toolbar.Button: aria-disabled="true" and data-disabled, the item stays focusable, and a click, Enter or Space does nothing (focusableWhenDisabled={false} makes it native instead). A dev warning when the rendered element is natively disabled anyway (2.1.1 Keyboard, A; 2.4.3 Focus Order, A)
every item–tabindex="0" on one, -1 on the othersRoving tabindex. DOM focus is on the control, never aria-activedescendant, because a Listbox trigger and a Popover trigger in the toolbar need real focus. The toolbar's tabindex wins over an element's own: Listbox.Trigger honours a tabIndex it is given. Never a natively disabled control, and none when every control is natively disabled.

A Toolbar has no strings of its own: every name is the consumer's, from their translations. It announces nothing: state is in aria-pressed and aria-disabled.

What is an item. Only Toolbar.Button, Toolbar.Toggle and Toolbar.Item are items. The toolbar doesn't query the focusable descendants, so a Popover's form, which sits in the toolbar's DOM right after its trigger, never becomes an item, and its keys are never the toolbar's.

It announces nothing.

What you need to do#

  • Name the toolbar with aria-label from your translations, or aria-labelledby. When two toolbars are on a page, their names differ.
  • Name every group, and every icon-only control (aria-label; 4.1.2 Name, Role, Value, A; 2.5.3 Label in Name, A). Show a tooltip for icon-only controls where the toolbar is for people who might not know the icons.
  • Use a toolbar for three or more controls. A toolbar with only text buttons for navigation is a nav, not a toolbar.
  • Put controls in with Toolbar.Button, Toolbar.Toggle and Toolbar.Item. A plain <Button> inside is an ordinary Tab stop, not an item.
  • A control that renders a popup (Popover, Listbox) goes in with Toolbar.Item and as, and renders its popup right after it. A Listbox in a toolbar sets native="never": with native="auto" (the default) a touch device renders a native <select> that replaces the Listbox's children, so the Toolbar.Item and the trigger's aria-label never render, and the select has no name and is not a toolbar item (4.1.2 Name, Role, Value, A). A dev warning fires when a Listbox renders natively inside a toolbar. Don't put a text field in a toolbar unless it is the point: it keeps the arrow keys.
  • Keep the default for a disabled control (aria-disabled, still focusable), so people can find it and read that it is unavailable. Use focusableWhenDisabled={false} only when the whole toolbar is disabled: then no control can be the Tab stop, and the toolbar leaves the Tab order. A natively disabled control is never the Tab stop (the toolbar picks the first one that can take focus), so mixing is safe, but those controls are skipped by the arrows.
  • Disable a Listbox on its own Root (it stays reachable by the arrows and doesn't open), and a Popover trigger with disabled on the Toolbar.Item.
  • A text field as an item is made read-only with readOnly, not disabled. A disabled Toolbar.Item stops Enter and Space and clicks, but a text <input> is still editable by typing, so use readOnly (and say why) for a field the user may not change.
  • A vertical toolbar and a Listbox: in a vertical toolbar a Listbox item owns ArrowUp and ArrowDown (they open and move in it), so use Tab or a horizontal toolbar for it. The default theme only ships horizontal toolbars.
  • Set aria-controls to the element the toolbar acts on, when there is one, as in APG's example.

Warning: Don't forget

Name the toolbar and every group, and name every control that has only an icon. Without a name a screen reader says only “toolbar”, “group” or “button”, and the user can’t tell what it is for (4.1.2 Name, Role, Value).

Visual and modes#

  • Focus indicator: each control's own ring (2.4.7 Focus Visible, AA; 2.4.13 Focus Appearance, AAA). A Toolbar.Toggle is a Toggle, with the same focus ring on keyboard focus.
  • Target size: each control is at least 24 × 24 CSS px (2.5.8 Target Size (Minimum), AA). The default theme never goes below that threshold, in any density. A story asserts it.
  • forced-colors behaviour: the buttons keep a visible edge, a pressed toggle is filled with the system's highlight colour and its text uses the system's highlighted-text colour, and the hairline between groups uses the system's disabled-text colour. The forced-colours sweep checks that every control keeps a visible boundary and shows a focus indicator when it has focus (1.4.11).
  • reduced-motion behaviour: no motion.
  • Reflow: the toolbar wraps group by group, and an attached group (the default) never wraps, so when one group alone is wider than the row its buttons wrap their own labels. Icon-only buttons keep their minimum size, and nothing scrolls sideways or truncates (1.4.10 Reflow, AA; 2.5.8 Target Size (Minimum), AA). The reflow sweep runs every story at 320 CSS px and fails on horizontal overflow.
  • Right to left: logical properties only, and the arrows flip (above).

Focus management#

  • Initial focus: not moved. Tab enters at the control that last had focus.
  • Trap: no. Tab and Shift+Tab always leave the toolbar.
  • Restore to: not applicable. A command keeps focus on its button. (A component built on the toolbar, such as an editor, decides where focus goes after a command and documents it.)
  • Never obscured by: the toolbar is not sticky by default, and nothing in it overlays the focused control. A popup next to a trigger is placed so it doesn't cover its trigger (2.4.11 Focus Not Obscured, AA).

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): role="toolbar" and role="group" with names.
  • 1.4.1 Use of Color (A), 1.4.11 Non-text Contrast (AA): a pressed toggle is a filled tile, not colour alone (the Toggle contract).
  • 1.4.10 Reflow (AA): wrapping.
  • 2.1.1 Keyboard (A), 2.1.2 No Keyboard Trap (A): every item reachable, Tab always leaves.
  • 2.4.3 Focus Order (A), 2.4.7 Focus Visible (AA): one Tab stop, DOM order, the item that last had focus.
  • 2.4.11 Focus Not Obscured (Minimum) (AA): the toolbar is not sticky by default, and nothing in it covers the focused control.
  • 2.4.13 Focus Appearance (AAA, not counted toward AA): each control's own ring.
  • 2.5.3 Label in Name (A), 2.5.8 Target Size (Minimum) (AA).
  • 4.1.2 Name, Role, Value (A): the toolbar's, groups' and controls' names, roles and states.

Keyboard#

Focus
roving tabindex
Selection follows focus
n/a
Arrows wrap
yes (loop={false} stops at the ends)
Shortcuts
none

The toolbar is one Tab stop:

  • Exactly one item has tabindex="0": the one that last had focus, or the first before any has.
  • The Tab stop is never a natively disabled control. It is the last focused one if that is still enabled, else the next enabled one in DOM order (else the nearest one before it).
  • If that item unmounts, or becomes natively disabled, the next enabled one takes over. Focus moves to it from a control that was disabled while focused, so it is never lost to the page.
  • When every control is natively disabled there is no Tab stop, and the toolbar leaves the Tab order (a disabled editor).
  • Arrow keys flip in right-to-left text, read from the toolbar's computed direction, so a dir on a container works without a provider.
  • Items are ordered by their place in the page, across groups and rows.
KeyWhereWhat happens
Tab / Shift+TabToolbarEnters at the control that last had focus (the first, the first time), and leaves the toolbar: the next Tab moves on, and Shift+Tab goes back to the previous focusable element
Test: toolbar.test.tsx › roving tabindex › Tab enters at the control that last had focus, from before and from after
Tab / Shift+TabToolbar whose first control is natively disabledEnters at the first control that can take focus, and leaves the toolbar. A control that becomes disabled while it has focus hands focus to the new Tab stop
Test: toolbar.test.tsx › the Tab stop skips a natively disabled first control, toolbar.test.tsx › focus moves to the new Tab stop when the focused control becomes disabled
ArrowRight / ArrowLeftToolbar, horizontalNext and previous control, across groups and rows. From the last control to the first, and back
Test: toolbar.test.tsx › arrow keys › ArrowRight and ArrowLeft move across groups, and wrap by default
ArrowLeft / ArrowRightToolbar, horizontal, right to leftThe arrows flip: ArrowLeft is the next control and ArrowRight the previous
Test: toolbar.test.tsx › arrow keys › right to left: the arrows flip, read from the toolbar’s direction
ArrowDown / ArrowUpToolbar, verticalNext and previous control, wraps. They don't flip in right-to-left text
Test: toolbar.test.tsx › arrow keys › vertical: ArrowDown and ArrowUp move and wrap, and ArrowRight does nothing
ArrowDown / ArrowUpToolbar, horizontalNot taken: they stay the page's and the item's own
Test: toolbar.test.tsx › horizontal: ArrowDown and ArrowUp do nothing
Home / EndToolbarFirst and last control
Test: toolbar.test.tsx › arrow keys › Home and End go to the first and the last control
Enter / SpaceToolbar.Button, Toolbar.ToggleActivates it (a Toggle switches aria-pressed). Focus stays on it
Test: toolbar.test.tsx › arrow keys › Enter and Space activate a button and a toggle, and focus stays
(any)Disabled controlIt is focusable with the arrows and read as unavailable (aria-disabled), also a Toolbar.Item. Enter, Space and a click do nothing. A natively disabled control (focusableWhenDisabled={false}) can't take focus, so the arrows skip it
Test: toolbar.test.tsx › disabled controls › a disabled button and toggle are focusable with aria-disabled, reachable by the arrows, and inert, toolbar.test.tsx › a disabled Toolbar.Item stays reachable and inert
(keys the item owns)Toolbar.Item, such as a Listbox triggerThe item's own keys win: ArrowDown opens a Listbox, and Home and End on a closed Listbox open it, as its contract says, instead of going to the toolbar's ends. Left and Right still move to the next and previous control
Test: toolbar.test.tsx › keys the item owns › a Listbox.Trigger keeps its own keys, and Left and Right still move on
(text editing keys)Toolbar.Item that is a text fieldA text <input>, a <textarea> or a contenteditable keeps the arrows, Home and End for its caret. The toolbar takes none of them there
Test: toolbar.test.tsx › events from a text field in the toolbar are left alone
(inside a popup)A popup next to an itemA key from an element that isn't an item (a Popover's form, its Close button) is never the toolbar's. Tab moves from the trigger into the popup, as the popup's contract says
Test: toolbar.test.tsx › a Popover.Trigger as an item works, and keys inside its popup never move the toolbar
(with a modifier key)ToolbarNot taken: Control+End, Alt+ArrowRight and the like stay the browser's and the page's
Test: toolbar.test.tsx › keys with Control, Alt, Meta or Shift are left alone

Escape and the typed characters are not handled. A key an item handled already (defaultPrevented) is not taken again.

Why these choices. Arrows wrap because APG's own example does. Home and End are implemented, because they cost nothing and a long toolbar is slow without them. A Listbox trigger keeps its own Home and End: the user who opens a Listbox expects it to open, and Left and Right are enough to leave it.

API reference#

import { Toolbar, useToolbar } from '@kvirn-ui/react'

Toolbar.Root

Renders <div> with the role toolbar. It takes every attribute of a <div> except role and aria-orientation, and passes ref to it. Name it with aria-label or aria-labelledby, and set aria-controls to the element it acts on. The default theme is described on Theming.

PropTypeDefaultDescription
orientation'horizontal' | 'vertical''horizontal'The axis of the arrow keys. Vertical uses Down and Up and sets aria-orientation="vertical". Horizontal uses Left and Right, which flip in right-to-left text.
loopbooleantrueWhether the arrows wrap from the last control to the first, and back.
as'div' | 'section''div'Changes the element. A section still gets role="toolbar" (so it is a toolbar and not a region), the class and the keys.
Class or attributeValuesMeaning
kv-toolbaralwaysThe part class. The default theme styles it.
aria-orientation"vertical" or absentSet when orientation is vertical. Horizontal is the default of the role.

Toolbar.Group

Renders <div> with the role group once it has a name. It is a ButtonGroup that is always attached: its buttons join into one strip. It takes every attribute of a <div> except role, and passes ref to it. In a toolbar it needs aria-label or aria-labelledby.

Class or attributeValuesMeaning
kv-button-groupalwaysThe part class. The default theme draws a hairline between groups.
kv-button-group--attachedalwaysAdded next to your own class. The buttons touch and share borders.

Toolbar.Button

Renders <button type="button"> with the role button. It is a Button that is also a toolbar item, takes the same attributes and passes ref to the button.

PropTypeDefaultDescription
disabledbooleanfalseBlocks activation. The button stays focusable, with aria-disabled="true".
focusableWhenDisabledbooleantrueWith disabled, keeps the button reachable by the arrows. false makes disabled native: the arrows skip the button and it is never the Tab stop.
type'button' | 'submit' | 'reset''button'The button type.
onClickMouseEventHandler<HTMLButtonElement>–Called on click, Enter and Space, and never while the button is disabled.
Class or attributeValuesMeaning
kv-buttonalwaysThe Button part class, and its variants.
aria-disabled"true" or absentSet when disabled and focusableWhenDisabled are both on, which is the default in a toolbar.
tabindex"0" or "-1"Roving: "0" on the one item the Tab key reaches, "-1" on the others.
data-disabledpresent or absentThe button is disabled, natively or focusable.
data-focus-visiblepresent or absentThe button has keyboard focus (:focus-visible).

Toolbar.Toggle

Renders <button type="button"> with the role button and aria-pressed. It is a Toggle that is also a toolbar item, takes the same attributes and passes ref to the button.

PropTypeDefaultDescription
pressedboolean–Controlled: whether the toggle is on. Pair it with onPressedChange.
defaultPressedbooleanfalseUncontrolled: whether the toggle starts on.
onPressedChange(pressed: boolean, details: TogglePressedChangeDetails) => void–Called with the new value and { event } when the user switches the toggle, and never while it is disabled. It only reports: with pressed set, you change pressed yourself.
disabledbooleanfalseBlocks switching. The toggle stays focusable, with aria-disabled="true".
focusableWhenDisabledbooleantrueWith disabled, keeps the toggle reachable by the arrows. false makes disabled native: the arrows skip it.
onClickMouseEventHandler<HTMLButtonElement>–Called on click, Enter and Space, and never while the toggle is disabled.
Class or attributeValuesMeaning
kv-button kv-togglealwaysThe Toggle part classes. The default theme fills a pressed toggle.
aria-pressed"true" or "false"Whether the toggle is on.
data-pressedpresent or absentThe toggle is on.
aria-disabled"true" or absentSet when disabled and focusableWhenDisabled are both on, which is the default in a toolbar.
tabindex"0" or "-1"Roving: "0" on the one item the Tab key reaches, "-1" on the others.
data-disabledpresent or absentThe toggle is disabled, natively or focusable.
data-focus-visiblepresent or absentThe toggle has keyboard focus (:focus-visible).

Toolbar.Item

Renders <button type="button">, or the component you give as. It makes any control that can take focus by itself a toolbar item. It takes every attribute of a <button> and passes ref to the element. The toolbar’s tabindex wins over the element’s own.

PropTypeDefaultDescription
asElementType'button'The component that becomes an item: Link.Root, Popover.Trigger, Listbox.Trigger or an input of your own, with its props set on the Item: as={Link.Root} href="#guide". It must be focusable by itself, forward its ref and spread its props on a DOM node. Without as the item is a <button type="button">.
disabledbooleanfalseBlocks click, Enter and Space, also for the rendered element’s own handlers. A text input stays editable: make it readOnly instead.
focusableWhenDisabledbooleantrueWith disabled, keeps the item focusable with aria-disabled="true" and data-disabled. false makes disabled native: the arrows skip the item.
Class or attributeValuesMeaning
tabindex"0" or "-1"Roving: "0" on the one item the Tab key reaches, "-1" on the others. It wins over the rendered element’s own.
aria-disabled"true" or absentSet when disabled and focusableWhenDisabled are both on.
data-disabledpresent or absentThe item is disabled, natively or focusable.

useToolbar

Options

PropTypeDefaultDescription
orientation'horizontal' | 'vertical''horizontal'The axis of the arrow keys.
loopbooleantrueWhether the arrows wrap from the last control to the first, and back.

Result

PropTypeDefaultDescription
toolbarPropsToolbarRootPartProps–Spread on a <div>: class, role, aria-orientation, ref and the key handler.
getItemProps(key: string) => ToolbarItemPartProps–The roving tabindex, ref and focus handlers of one control. The key names the item and is the same on every render, such as a useId().
orientation'horizontal' | 'vertical'–The orientation in use.
itemCountnumber–How many items are mounted. It lags the first render by one commit, because items register after it.

Known issues#

  • Before the script runs every item is a Tab stop. Items register after the first render, so until then every item has tabindex="0" and a toolbar in server-rendered HTML works as a row of ordinary buttons (no arrow keys yet). After registration one item keeps 0 and the others get -1.
  • Items are registered, not queried. An element that is a control but not an item (a plain <Button> placed in the toolbar) is a Tab stop of its own and is skipped by the arrows.
  • 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.

Information: Did you know?

If an issue here affects your service, list it in your accessibility statement with its workaround.

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/apasstoolbar.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasstoolbar.test.tsx › is a div with role="toolbar", its name and the part class, and no aria-orientation when horizontal; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass13 rows, each names a test (toolbar.test.tsx › roving tabindex › Tab enters at the control that last had focus, from before and from after)
Focus order and managementpassn/apasstoolbar.test.tsx asserts focus (toHaveFocus); first rule: roving tabindex › Tab enters at the control that last had focus, from before and from after
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 in: the toolbar name, then the group name, then the first control are read. Tab again leaves the toolbar.
  • Right Arrow and Left Arrow through every control: each is read with its name and state. Home and End go to the ends.
  • Tab out and Shift+Tab back returns to the last control.
  • An unavailable control is reached and read as unavailable. Enter and Space do nothing.
  • Space on a toggle reads pressed or not pressed.
  • On a Popover or Listbox control in the toolbar its own keys work, and Left and Right still move on.