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.

Toggle

A button that is on or off, and says which. A button that is either on or off, and a screen reader (software that reads the screen aloud) says which. Use it for a choice with a direct, visible effect, such as showing only unread messages.

Package
@kvirn-ui/react
Pattern
APG Button (toggle button)
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a toggle for a choice that changes what the user sees right away: a filter, bold text, a map layer.
  • Give it a name that stays the same when it switches: “Show only unread”, never “Show all” when it is on.
  • Not for an answer in a form: use a Checkbox or a radio group. Not for a setting that is saved at once: use a Switch.
  • Not for an action that happens once: use a Button. For a row of toggles that belong together, put them in a Toolbar.

Example#

Use cases#

A filter on a list

The choice changes the page at once, so you keep the state yourself: pass pressed and update it in onPressedChange. That callback only reports the new value, so with pressed set the toggle changes only when you change it.

Showing 4 of 4 messages
  • Parking permit approved
  • Building permit: more information needed
  • Application received
  • Invoice ready

Props in this example:

Warning: Don't forget

When a toggle changes a list, show how many results are left in a status message (4.1.3 Status Messages). This example uses an output element, which is a status message and is on the page from the start. You can also use useAnnouncer.

A toggle with only an icon

Use it where there is room for an icon but not for a word, such as a button that shows a password. The name comes from an aria-label that you take from your own translations. It stays the same when the toggle switches.

Props in this example:

Warning: Don't forget

Don’t change the label when the state changes: a screen reader reports the state from aria-pressed. See What you need to do.

Disabled with a reason

Try not to disable a toggle. With disabled alone it leaves the Tab order. When the reason matters, for example when there is nothing to filter, add focusableWhenDisabled and link the reason to the toggle with aria-describedby. People using a keyboard or a screen reader can then find the toggle and learn why it is off limits. It still says whether it is on.

There are no messages to filter yet.

Props in this example:

A choice with a limit

A parent compares day-care places, and the comparison holds at most three. Because you keep pressed, you decide what a press does: at the limit, leave the state as it is and say why. The hidden school name gives each toggle its own name, and the name stays the same when it is on.

  • Birch Tree Preschool
  • Harbour Preschool
  • Meadow Preschool
  • Hill Preschool

Comparing 0 of 3 preschools

Props in this example:

Warning: Don't forget

A press that changes nothing needs a reason in words. Show it and announce it, or a screen reader user hears only that the button is still off (4.1.3 Status Messages).

The state read from the hook

A resident can switch a decision letter to an easy-to-read version. When the effect is a change right next to the button, you need no state of your own: useToggle keeps it, and you read isPressed. Spread toggleProps on your own <button>.

Application 2026-0412 is approved in accordance with the municipal parking regulations. The permit is valid for the registered vehicle for twelve months from the date of the decision.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: Button, the toggle button (aria-pressed)
  • Deviations: none
  • Native elements used: <button type="button">. Activation, the Tab stop and the Enter and Space keys are the browser's own.

A Toggle is a Button that is on or off. Use it for a choice with a direct, visible effect on the page: bold text, "show only unread". It is not for an answer in a form (use a Checkbox or a radio group) or a setting that is saved (use a Switch). It builds on Button: everything Button's contract says about disabled, focusableWhenDisabled and the missing-name warning holds here.

PartElement / roleARIANotes
Toggle<button type="button"> → buttonaria-pressed="false" or "true"Always present, so the button is a toggle button even while off. data-pressed while on. The name never changes with the state: a screen reader says it is on from aria-pressed
controlledpressed + onPressedChangeShows the pressed it is given and only reports a press. defaultPressed starts an uncontrolled toggle on
disablednative disabledNot focusable, not activatable. aria-pressed and data-disabled stay, so a disabled pressed toggle still says it is on
disabled and focusablearia-disabled="true", no disabled attributeStays in the Tab order. Click, Enter and Space change nothing and call neither onPressedChange nor onClick. focusableWhenDisabled is Button's
keyboard focusnonedata-focus-visible while the toggle matches :focus-visible
no accessible name–A dev warning when the only content is hidden from assistive technology, such as a decorative Icon (4.1.2).

useToggle gives the same toggleProps for your own <button>. It is useButton plus the pressed state, so a disabled toggle blocks activation there too.

It announces nothing.

What you need to do#

  • Give the toggle a visible text label, or an aria-label from your own translations for an icon-only toggle (4.1.2 Name, Role, Value, A; 2.5.3 Label in Name, A). The name must not change with the state: "Show map" stays "Show map" and never becomes "Hide map" when it is on (APG).
  • Use a Toggle for a choice with a direct, visible effect. A setting that turns something on or off for good is a Switch (saved at once) or a Checkbox, and an answer in a form is a Checkbox or a radio group.
  • With pressed, update it in onPressedChange, or the toggle never changes.
  • A toggle that changes a list or a result (a filter) needs a status message with the result count, such as "12 results", announced through useAnnouncer (4.1.3 Status Messages, AA). The press alone tells a screen reader user only that the button is pressed.
  • Style [data-pressed] and [aria-pressed='true']. The pressed state must never be shown by colour alone (1.4.1 Use of Color, A): the default theme fills the button and changes the icon or label colour.

Visual and modes#

  • Focus indicator: as Button. The default theme draws a focus ring that is at least 2px at 3:1 (2.4.7 Focus Visible, AA; 2.4.13 Focus Appearance, AAA).
  • Target size: as Button, at least 24 × 24 CSS px in the default theme (2.5.8 Target Size (Minimum), AA).
  • Pressed (default theme): a solid fill in the primary colour with a light icon or label, flat in a toolbar. The fill reaches 3:1 against its surroundings and the icon 4.5:1 or 3:1 on the fill (1.4.1 Use of Color, A; 1.4.11 Non-text Contrast, AA). The theme's contrast check measures the pairs.
  • forced-colors behaviour: pressed uses the system's highlight colours for its fill, text and edge, so it differs from an unpressed button without colour alone (the forced-colours sweep also checks that every toggle keeps a visible boundary and shows a focus indicator when it has focus).
  • reduced-motion behaviour: the fill changes instantly.
  • Reflow: a long label wraps inside the button. No horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA): the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow.

Warning: Don't forget

If you restyle the pressed state, don’t rely on colour alone. Change the fill, the icon or the label as well (1.4.1 Use of Color).

Focus management#

  • Initial focus: not moved. Toggle never moves focus.
  • Trap: no.
  • Restore to: not applicable. Switching it keeps focus on the toggle.
  • Never obscured by: Toggle renders no overlay.

WCAG success criteria#

  • 1.4.1 Use of Color (A), 1.4.11 Non-text Contrast (AA): the pressed state is a filled tile and a changed icon or label colour, measured by the theme's contrast check.
  • 2.1.1 Keyboard (A): native <button>, Enter and Space (keyboard rows above).
  • 2.4.3 Focus Order (A), 2.4.7 Focus Visible (AA): native Tab order, data-focus-visible.
  • 2.4.13 Focus Appearance (AAA, not counted toward AA): the default theme's focus ring.
  • 2.5.3 Label in Name (A): the visible label is the name.
  • 2.5.8 Target Size (Minimum) (AA): consumer and default theme.
  • 4.1.2 Name, Role, Value (A): role button, aria-pressed, aria-disabled exposed (toggle.test.tsx).

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none

A native <button>: one Tab stop, in DOM order, with no tabindex. Enter and Space are native, and both switch it. In a Toolbar it is one of the toolbar's items instead: the toolbar's contract owns the keys then.

KeyWhereWhat happens
Tabbefore the toggleMoves focus to the toggle
Test: toggle.test.tsx › Tab moves to the toggle and Shift+Tab moves off it
Shift+Tabon the toggleMoves focus to the previous focusable element, and off the toggle
Test: toggle.test.tsx › Tab moves to the toggle and Shift+Tab moves off it
Enter / SpaceToggleSwitches it on or off: aria-pressed changes. The name is the same, and focus stays on it
Test: toggle.test.tsx › Enter and Space switch it, the name stays and focus stays
Enter / SpaceToggle, disabled and focusableNothing happens. Focus stays on the toggle
Test: toggle.test.tsx › a focusable disabled toggle uses aria-disabled, stays in the Tab order, and ignores press, Enter and Space
No keyPointerA click on a disabled toggle, focusable or not, calls no handler
Test: toggle.test.tsx › a focusable disabled toggle uses aria-disabled, stays in the Tab order, and ignores press, Enter and Space

Escape, the arrow keys and Home and End are not handled.

API reference#

import { Toggle, useToggle } from '@kvirn-ui/react'

Toggle

Renders <button type="button" aria-pressed> with the role button. It takes every attribute of a <button> except type, aria-pressed and aria-disabled, and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
pressedboolean–Controlled: whether the toggle is on. Pair it with onPressedChange.
defaultPressedbooleanfalseUncontrolled: whether the toggle starts on.
onPressedChange(pressed: boolean, details: { event }) => void–Called with the new value and the click behind it. It only reports: with pressed set, you change pressed yourself. Never called while disabled.
disabledbooleanfalseBlocks switching. Without focusableWhenDisabled the toggle is natively disabled and skipped by Tab.
focusableWhenDisabledbooleanfalseWith disabled, keeps the toggle in the Tab order with aria-disabled="true" so users can find it and read why. Switching stays blocked.
onClickMouseEventHandler<HTMLButtonElement>–Called on every activation, after onPressedChange, and never while the toggle is disabled.
Class or attributeValuesMeaning
kv-buttonalwaysThe button class. The default theme styles it.
kv-togglealwaysThe toggle part class. The default theme fills a pressed toggle with the primary colour and a light label or icon.
kv-button--icon-onlyclass you addA square toggle with only an icon. Give it an aria-label. Theme option.
aria-pressed"true" or "false"Always set, from pressed or defaultPressed. Not passed directly.
data-pressedpresent or absentThe toggle is on.
data-disabledpresent or absentThe toggle is disabled, natively or focusable.
data-focus-visiblepresent or absentThe toggle has keyboard focus (:focus-visible).
aria-disabled"true" or absentSet when disabled and focusableWhenDisabled are both on. Not passed directly.

useToggle

Options

PropTypeDefaultDescription
pressedboolean–Controlled: whether the toggle is on.
defaultPressedbooleanfalseUncontrolled: whether the toggle starts on.
onPressedChange(pressed: boolean, details: { event }) => void–Called with the new value. Never called while disabled.
disabledbooleanfalseMirrors HTML disabled.
focusableWhenDisabledbooleanfalseKeeps a disabled toggle in the Tab order with aria-disabled="true".
onClickMouseEventHandler<HTMLButtonElement>–Called on every activation, and never while disabled. Pass it here, not on your own <button>, so a disabled toggle blocks it.

Result

PropTypeDefaultDescription
togglePropsTogglePartProps–Spread on a <button>: classes, type, aria-pressed, disabled state, handlers.
isPressedboolean–Whether the toggle is on.
isDisabledboolean–Whether the toggle is disabled.
isFocusVisibleboolean–Whether the toggle has keyboard (:focus-visible) focus.

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.
  • The pressed look in the high-contrast themes is the same as a primary button at rest (they have no depth there). The context and aria-pressed tell them apart.

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/apasstoggle.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasstoggle.test.tsx › is a native button with aria-pressed, off by default, and the part classes; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass5 rows, each names a test (toggle.test.tsx › Tab moves to the toggle and Shift+Tab moves off it)
Focus order and managementpassn/apasstoggle.test.tsx asserts focus (toHaveFocus); first rule: Tab moves to the toggle and Shift+Tab moves off it
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 to the toggle: its name, "toggle button" and the pressed state are read.
  • Press Enter, then Space: the state change is read, the name stays the same, and focus stays.
  • An icon-only toggle reads its label, not just "button".
  • A disabled toggle with a reason (focusableWhenDisabled): Tab finds it, it is read as unavailable, the reason is read, and pressing changes nothing.
  • A toggle that filters a list: switch it and check that the new result count is read.
  • In forced colours or a high-contrast theme, pressed and not pressed differ without relying on colour.