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#
import { Toggle } from '@kvirn-ui/react'
import { useToggleTexts } from './texts.ts'
export function DefaultToggle() {
const { texts } = useToggleTexts()
return <Toggle>{texts.unreadOnly}</Toggle>
}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.
- Parking permit approved
- Building permit: more information needed
- Application received
- Invoice ready
import { Toggle } from '@kvirn-ui/react'
import { useState } from 'react'
import { useToggleTexts } from './texts.ts'
export function FilterList() {
const { texts } = useToggleTexts()
const [isUnreadOnly, setIsUnreadOnly] = useState(false)
const shown = texts.messages.filter((message) => !isUnreadOnly || message.isUnread)
return (
<div>
<Toggle pressed={isUnreadOnly} onPressedChange={setIsUnreadOnly}>
{texts.unreadOnly}
</Toggle>
<output>{texts.showing(shown.length, texts.messages.length)}</output>
<ul>
{shown.map((message) => (
<li key={message.id}>{message.text}</li>
))}
</ul>
</div>
)
}Props in this example:
pressedon ToggleonPressedChangeon Toggle
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.
import { Icon, Toggle } from '@kvirn-ui/react'
import { useId, useState } from 'react'
import { useToggleTexts } from './texts.ts'
export function IconOnly() {
const { texts } = useToggleTexts()
const [isShown, setIsShown] = useState(false)
const inputId = useId()
return (
<div>
<label htmlFor={inputId}>{texts.passwordLabel}</label>
<input id={inputId} type={isShown ? 'text' : 'password'} autoComplete="current-password" />
<Toggle
className="kv-button--icon-only"
aria-label={texts.showPassword}
pressed={isShown}
onPressedChange={setIsShown}
>
<Icon name="eye" />
</Toggle>
</div>
)
}Props in this example:
pressedon ToggleonPressedChangeon Toggle
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.
import { Toggle } from '@kvirn-ui/react'
import { useId } from 'react'
import { useToggleTexts } from './texts.ts'
export function DisabledWithReason() {
const { texts } = useToggleTexts()
const reasonId = useId()
return (
<>
<p id={reasonId}>{texts.noMessages}</p>
<Toggle disabled focusableWhenDisabled aria-describedby={reasonId}>
{texts.unreadOnly}
</Toggle>
</>
)
}Props in this example:
disabledon TogglefocusableWhenDisabledon Toggle
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
import { Toggle, VisuallyHidden, useAnnouncer } from '@kvirn-ui/react'
import { useState } from 'react'
import { useToggleTexts } from './texts.ts'
export function CompareLimit() {
const { texts } = useToggleTexts()
const { announce } = useAnnouncer()
const [chosenIds, setChosenIds] = useState<string[]>([])
const [limitMessage, setLimitMessage] = useState('')
function change(id: string, isOn: boolean) {
if (isOn && chosenIds.length >= texts.compareLimit) {
// pressed is yours, so the toggle stays off until you change it. Say why nothing happened:
// as text for everyone, and announced for people who can't see the page change.
const message = texts.limitReached(texts.compareLimit)
setLimitMessage(message)
announce(message)
return
}
setLimitMessage('')
setChosenIds(isOn ? [...chosenIds, id] : chosenIds.filter((chosenId) => chosenId !== id))
}
return (
<div>
<ul>
{texts.preschools.map((preschool) => (
<li key={preschool.id}>
{preschool.name}{' '}
<Toggle
pressed={chosenIds.includes(preschool.id)}
onPressedChange={(isOn) => change(preschool.id, isOn)}
>
{texts.compare}
<VisuallyHidden> {preschool.name}</VisuallyHidden>
</Toggle>
</li>
))}
</ul>
<p>{limitMessage || texts.comparing(chosenIds.length, texts.compareLimit)}</p>
</div>
)
}Props in this example:
pressedon ToggleonPressedChangeon Toggle
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.
import { useToggle } from '@kvirn-ui/react'
import { useToggleTexts } from './texts.ts'
export function OwnButton() {
const { texts } = useToggleTexts()
// The hook keeps the state, so nothing else here does. Read isPressed to choose what to show.
const toggle = useToggle()
return (
<div>
<button {...toggle.toggleProps}>{texts.easyToRead}</button>
<p>{toggle.isPressed ? texts.easyToReadText : texts.decisionText}</p>
</div>
)
}Props in this example:
togglePropson useToggle (result)isPressedon useToggle (result)
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.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Toggle | <button type="button"> → button | aria-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 |
| controlled | pressed + onPressedChange | Shows the pressed it is given and only reports a press. defaultPressed starts an uncontrolled toggle on | |
| disabled | native disabled | Not focusable, not activatable. aria-pressed and data-disabled stay, so a disabled pressed toggle still says it is on | |
| disabled and focusable | aria-disabled="true", no disabled attribute | Stays in the Tab order. Click, Enter and Space change nothing and call neither onPressedChange nor onClick. focusableWhenDisabled is Button's | |
| keyboard focus | none | data-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-labelfrom 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 inonPressedChange, 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-disabledexposed (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.
| Key | Where | What happens |
|---|---|---|
| Tab | before the toggle | Moves focus to the toggle Test: toggle.test.tsx › Tab moves to the toggle and Shift+Tab moves off it |
| Shift+Tab | on the toggle | Moves 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 / Space | Toggle | Switches it on or off: aria-pressed changes. The name is the same, and focus stays on itTest: toggle.test.tsx › Enter and Space switch it, the name stays and focus stays |
| Enter / Space | Toggle, disabled and focusable | Nothing 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 key | Pointer | A 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.
| 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: { 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. |
disabled | boolean | false | Blocks switching. Without focusableWhenDisabled the toggle is natively disabled and skipped by Tab. |
focusableWhenDisabled | boolean | false | With disabled, keeps the toggle in the Tab order with aria-disabled="true" so users can find it and read why. Switching stays blocked. |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on every activation, after onPressedChange, and never while the toggle is disabled. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-button | always | The button class. The default theme styles it. |
kv-toggle | always | The toggle part class. The default theme fills a pressed toggle with the primary colour and a light label or icon. |
kv-button--icon-only | class you add | A 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-pressed | present or absent | The toggle is on. |
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). |
aria-disabled | "true" or absent | Set when disabled and focusableWhenDisabled are both on. Not passed directly. |
useToggle
Options
| Prop | Type | Default | Description |
|---|---|---|---|
pressed | boolean | – | Controlled: whether the toggle is on. |
defaultPressed | boolean | false | Uncontrolled: whether the toggle starts on. |
onPressedChange | (pressed: boolean, details: { event }) => void | – | Called with the new value. Never called while disabled. |
disabled | boolean | false | Mirrors HTML disabled. |
focusableWhenDisabled | boolean | false | Keeps a disabled toggle in the Tab order with aria-disabled="true". |
onClick | MouseEventHandler<HTMLButtonElement> | – | Called on every activation, and never while disabled. Pass it here, not on your own <button>, so a disabled toggle blocks it. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
toggleProps | TogglePartProps | – | Spread on a <button>: classes, type, aria-pressed, disabled state, handlers. |
isPressed | boolean | – | Whether the toggle is on. |
isDisabled | boolean | – | Whether the toggle is disabled. |
isFocusVisible | boolean | – | 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-pressedtell 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 + 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 | toggle.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | toggle.test.tsx › is a native button with aria-pressed, off by default, and the part classes; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 5 rows, each names a test (toggle.test.tsx › Tab moves to the toggle and Shift+Tab moves off it) |
| Focus order and management | pass | n/a | pass | toggle.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) | 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 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.