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#
import { Toolbar } from '@kvirn-ui/react'
import { useToolbarTexts } from './texts.ts'
export function DefaultToolbar() {
const { texts } = useToolbarTexts()
return (
<Toolbar.Root aria-label={texts.formatting}>
<Toolbar.Group aria-label={texts.history}>
<Toolbar.Button>{texts.undo}</Toolbar.Button>
<Toolbar.Button>{texts.redo}</Toolbar.Button>
</Toolbar.Group>
<Toolbar.Group aria-label={texts.textStyle}>
<Toolbar.Toggle defaultPressed>{texts.bold}</Toolbar.Toggle>
<Toolbar.Toggle>{texts.italic}</Toolbar.Toggle>
<Toolbar.Toggle>{texts.underline}</Toolbar.Toggle>
</Toolbar.Group>
<Toolbar.Group aria-label={texts.insert}>
<Toolbar.Button>{texts.link}</Toolbar.Button>
<Toolbar.Button>{texts.image}</Toolbar.Button>
</Toolbar.Group>
</Toolbar.Root>
)
}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.
import { Toolbar } from '@kvirn-ui/react'
import { useId, useState } from 'react'
import type { ReactNode } from 'react'
import { useToolbarTexts } from './texts.ts'
export function Toggles() {
const { texts } = useToolbarTexts()
const sampleId = useId()
const [isBold, setIsBold] = useState(false)
const [isItalic, setIsItalic] = useState(false)
const [isUnderline, setIsUnderline] = useState(false)
let sample: ReactNode = texts.sampleText
if (isUnderline) sample = <u>{sample}</u>
if (isItalic) sample = <em>{sample}</em>
if (isBold) sample = <strong>{sample}</strong>
return (
<div>
<Toolbar.Root aria-label={texts.textStyle} aria-controls={sampleId}>
<Toolbar.Toggle pressed={isBold} onPressedChange={setIsBold}>
{texts.bold}
</Toolbar.Toggle>
<Toolbar.Toggle pressed={isItalic} onPressedChange={setIsItalic}>
{texts.italic}
</Toolbar.Toggle>
<Toolbar.Toggle pressed={isUnderline} onPressedChange={setIsUnderline}>
{texts.underline}
</Toolbar.Toggle>
</Toolbar.Root>
<p id={sampleId}>{sample}</p>
</div>
)
}Props in this example:
pressedon Toolbar.ToggleonPressedChangeon Toolbar.Toggle
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.
import { bold, image, italic, link, underline } from '@kvirn-ui/icons'
import { Icon, Toolbar } from '@kvirn-ui/react'
import { useToolbarTexts } from './texts.ts'
export function IconOnly() {
const { texts } = useToolbarTexts()
return (
<Toolbar.Root aria-label={texts.formatting}>
<Toolbar.Group aria-label={texts.textStyle}>
<Toolbar.Toggle className="kv-button--icon-only" aria-label={texts.bold} defaultPressed>
<Icon icon={bold} />
</Toolbar.Toggle>
<Toolbar.Toggle className="kv-button--icon-only" aria-label={texts.italic}>
<Icon icon={italic} />
</Toolbar.Toggle>
<Toolbar.Toggle className="kv-button--icon-only" aria-label={texts.underline}>
<Icon icon={underline} />
</Toolbar.Toggle>
</Toolbar.Group>
<Toolbar.Group aria-label={texts.insert}>
<Toolbar.Button className="kv-button--icon-only" aria-label={texts.link}>
<Icon icon={link} />
</Toolbar.Button>
<Toolbar.Button className="kv-button--icon-only" aria-label={texts.image}>
<Icon icon={image} />
</Toolbar.Button>
</Toolbar.Group>
</Toolbar.Root>
)
}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.
import { Toolbar } from '@kvirn-ui/react'
import { useId } from 'react'
import { useToolbarTexts } from './texts.ts'
export function Unavailable() {
const { texts } = useToolbarTexts()
const reasonId = useId()
return (
<div>
<Toolbar.Root aria-label={texts.history}>
<Toolbar.Button>{texts.undo}</Toolbar.Button>
<Toolbar.Button disabled aria-describedby={reasonId}>
{texts.redo}
</Toolbar.Button>
<Toolbar.Button>{texts.link}</Toolbar.Button>
</Toolbar.Root>
<p id={reasonId}>{texts.redoReason}</p>
</div>
)
}Props in this example:
disabledon Toolbar.Button
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.
import { Link, Popover, Toolbar } from '@kvirn-ui/react'
import { useToolbarTexts } from './texts.ts'
export function OtherControls() {
const { texts } = useToolbarTexts()
return (
<Toolbar.Root aria-label={texts.formatting}>
<Toolbar.Button>{texts.undo}</Toolbar.Button>
<Toolbar.Toggle>{texts.bold}</Toolbar.Toggle>
<Popover.Root>
<Toolbar.Item as={Popover.Trigger} className="kv-button">
{texts.addLink}
</Toolbar.Item>
<Popover.Popup aria-label={texts.addLink}>
<form onSubmit={(event) => event.preventDefault()}>
<label>
{texts.address}
<br />
<input name="address" type="url" className="kv-input" />
</label>
<p>
<button type="submit" className="kv-button kv-button--primary">
{texts.add}
</button>{' '}
<Popover.Close className="kv-button">{texts.cancel}</Popover.Close>
</p>
</form>
</Popover.Popup>
</Popover.Root>
<Toolbar.Item as={Link.Root} href="#accessibility">
{texts.guide}
</Toolbar.Item>
</Toolbar.Root>
)
}Props in this example:
ason 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.
import { Toolbar } from '@kvirn-ui/react'
import { useToolbarTexts } from './texts.ts'
export function Vertical() {
const { texts } = useToolbarTexts()
return (
<Toolbar.Root
aria-label={texts.rowActions}
orientation="vertical"
style={{ flexDirection: 'column', alignItems: 'flex-start' }}
>
<Toolbar.Button>{texts.moveUp}</Toolbar.Button>
<Toolbar.Button>{texts.moveDown}</Toolbar.Button>
<Toolbar.Button>{texts.duplicate}</Toolbar.Button>
<Toolbar.Button>{texts.remove}</Toolbar.Button>
</Toolbar.Root>
)
}Props in this example:
orientationon Toolbar.Root
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.
import { Toolbar } from '@kvirn-ui/react'
import { useToolbarTexts } from './texts.ts'
export function NoLoop() {
const { texts } = useToolbarTexts()
return (
<div>
<Toolbar.Root aria-label={texts.viewer} loop={false}>
<Toolbar.Button>{texts.zoomOut}</Toolbar.Button>
<Toolbar.Button>{texts.zoomIn}</Toolbar.Button>
<Toolbar.Button>{texts.resetView}</Toolbar.Button>
<Toolbar.Button>{texts.fullScreen}</Toolbar.Button>
</Toolbar.Root>
<p>{texts.endsNote}</p>
</div>
)
}Props in this example:
loopon 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 rovingtabindexis 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.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Toolbar.Root | <div> (or <section> with as="section") → toolbar | aria-label or aria-labelledby | A 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-labelledby | A 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> → button | none, aria-disabled="true" when disabled | A 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> → button | aria-pressed, aria-disabled="true" when disabled | A Toggle, and an item. The name never changes with the state |
| Toolbar.Item | any focusable element | the element's own, aria-disabled="true" when disabled | Allowed 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 others | Roving 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-labelfrom your translations, oraria-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.ToggleandToolbar.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.Itemandas, and renders its popup right after it. A Listbox in a toolbar setsnative="never": withnative="auto"(the default) a touch device renders a native<select>that replaces the Listbox's children, so theToolbar.Itemand the trigger'saria-labelnever 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. UsefocusableWhenDisabled={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
disabledon theToolbar.Item. - A text field as an item is made read-only with
readOnly, notdisabled. A disabledToolbar.Itemstops Enter and Space and clicks, but a text<input>is still editable by typing, so usereadOnly(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-controlsto 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"androle="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 adiron a container works without a provider. - Items are ordered by their place in the page, across groups and rows.
| Key | Where | What happens |
|---|---|---|
| Tab / Shift+Tab | Toolbar | Enters 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+Tab | Toolbar whose first control is natively disabled | Enters 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 / ArrowLeft | Toolbar, horizontal | Next 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 / ArrowRight | Toolbar, horizontal, right to left | The 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 / ArrowUp | Toolbar, vertical | Next 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 / ArrowUp | Toolbar, horizontal | Not taken: they stay the page's and the item's own Test: toolbar.test.tsx › horizontal: ArrowDown and ArrowUp do nothing |
| Home / End | Toolbar | First and last control Test: toolbar.test.tsx › arrow keys › Home and End go to the first and the last control |
| Enter / Space | Toolbar.Button, Toolbar.Toggle | Activates it (a Toggle switches aria-pressed). Focus stays on itTest: toolbar.test.tsx › arrow keys › Enter and Space activate a button and a toggle, and focus stays |
| (any) | Disabled control | It 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 itTest: 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 trigger | The 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 field | A text <input>, a <textarea> or a contenteditable keeps the arrows, Home and End for its caret. The toolbar takes none of them thereTest: toolbar.test.tsx › events from a text field in the toolbar are left alone |
| (inside a popup) | A popup next to an item | A 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) | Toolbar | Not 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.
| Prop | Type | Default | Description |
|---|---|---|---|
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. |
loop | boolean | true | Whether 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 attribute | Values | Meaning |
|---|---|---|
kv-toolbar | always | The part class. The default theme styles it. |
aria-orientation | "vertical" or absent | Set 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 attribute | Values | Meaning |
|---|---|---|
kv-button-group | always | The part class. The default theme draws a hairline between groups. |
kv-button-group--attached | always | Added 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.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Blocks activation. The button stays focusable, with aria-disabled="true". |
focusableWhenDisabled | boolean | true | With 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. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on click, Enter and Space, and never while the button is disabled. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-button | always | The Button part class, and its variants. |
aria-disabled | "true" or absent | Set 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-disabled | present or absent | The button is disabled, natively or focusable. |
data-focus-visible | present or absent | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
pressed | boolean | – | Controlled: whether the toggle is on. Pair it with onPressedChange. |
defaultPressed | boolean | false | Uncontrolled: 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. |
disabled | boolean | false | Blocks switching. The toggle stays focusable, with aria-disabled="true". |
focusableWhenDisabled | boolean | true | With disabled, keeps the toggle reachable by the arrows. false makes disabled native: the arrows skip it. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on click, Enter and Space, and never while the toggle is disabled. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-button kv-toggle | always | The Toggle part classes. The default theme fills a pressed toggle. |
aria-pressed | "true" or "false" | Whether the toggle is on. |
data-pressed | present or absent | The toggle is on. |
aria-disabled | "true" or absent | Set 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-disabled | present or absent | The toggle is disabled, natively or focusable. |
data-focus-visible | present or absent | The 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.
| Prop | Type | Default | Description |
|---|---|---|---|
as | ElementType | '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">. |
disabled | boolean | false | Blocks click, Enter and Space, also for the rendered element’s own handlers. A text input stays editable: make it readOnly instead. |
focusableWhenDisabled | boolean | true | With disabled, keeps the item focusable with aria-disabled="true" and data-disabled. false makes disabled native: the arrows skip the item. |
| Class or attribute | Values | Meaning |
|---|---|---|
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 absent | Set when disabled and focusableWhenDisabled are both on. |
data-disabled | present or absent | The item is disabled, natively or focusable. |
useToolbar
Options
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | 'horizontal' | 'vertical' | 'horizontal' | The axis of the arrow keys. |
loop | boolean | true | Whether the arrows wrap from the last control to the first, and back. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
toolbarProps | ToolbarRootPartProps | – | 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. |
itemCount | number | – | 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 keeps0and 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 + 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 | toolbar.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | toolbar.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) | pass | n/a | pass | 13 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 management | pass | n/a | pass | toolbar.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) | 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 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.