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.

CheckboxGroup

One question with several answers that can all be true, such as how a resident wants to be contacted. It names the group with a legend. It keeps no state of its own: the checked values are yours.

Package
@kvirn-ui/react
Pattern
APG Checkbox (in a labelled group)
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use it for one question where the user can choose none, one or several options: contact preferences, which services a notification covers, which documents are attached.
  • Ask the question in the legend, and say how many answers are allowed above the options.
  • Keep the list short. Past about 15 options, a search or another pattern is easier to use, such as a Combobox that takes several choices. Don’t use <select multiple>.
  • Not for one yes or no answer, such as “I confirm the details are correct”: use a single Checkbox in a Field.
  • Not for exactly one answer out of several: use a RadioGroup.

Example#

How should we contact you about your permit? (optional)

Choose all that suit you. We only contact you about the decision.

Use cases#

A plain form with no state

Without value the checkboxes are native and uncontrolled. Set which boxes start checked with defaultValue, and give the group a name. A form submit then sends every checked box under that name.

How should we contact you about your permit? (optional)

Props in this example:

Information: Did you know?

Read all the checked values with FormData.getAll("contact"). Every Checkbox in the group needs its own value.

Keep the choice in your form state

Your form library or reducer owns the values. Give the group the checked values and store the next ones it reports. The group never stores them, so if you don’t update value, the boxes stay as they were.

How should we contact you about your permit? (optional)

You chose: email

Props in this example:

Say what is wrong when nothing is chosen

Check the answer when the user sends the form. Set invalid and render a CheckboxGroup.ErrorMessage that says what to do. The error is read as part of the group’s description. required removes “(optional)” from the legend, but a group can’t be announced as required, so the text above the options must say it, as this example does.

How should we contact you about your permit?

Required. Choose at least one.

Props in this example:

Warning: Don't forget

The group never moves focus. After a failed submit, move it to the first checkbox or to an ErrorSummary. The error is read only when focus enters the group, so without the move a screen reader user isn’t told what went wrong (3.3.1 Error Identification).

Explain one option

A short text about one option goes in that option’s CheckboxGroup.Option as a Field.HelpText, directly under its label, and is read with that checkbox. Text about the whole group goes in CheckboxGroup.HelpText under the options. A description to read before answering goes in CheckboxGroup.Prose above them.

How should we contact you about your permit? (optional)

The decision arrives within a week.

You can change this later under My pages.

A group that can’t be changed now

Disabling the group disables every checkbox natively, so Tab skips them. A screen reader can still reach them when reading the page, and tells the user they can't be used (each screen reader words it differently). So say why in visible help text, with what the person can do.

Contact by letter or text message (optional)

You can’t change this while your application is being processed.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: Checkbox, in a labelled group. The APG's checkbox examples group related boxes with role="group" and a label; the native equivalent is <fieldset> and <legend>, which is what Root renders. There is no composite keyboard model: each checkbox is its own Tab stop.
  • Deviations: none from APG. Choices made: Fieldset wiring, option labels carry no optional marker, a group's invalid doesn't cascade, no form state.
  • Native elements used: <fieldset> and <legend> (CheckboxGroup.Root and CheckboxGroup.Legend), <input type="checkbox"> (Checkbox), <label for> (Field.Label).

A CheckboxGroup is one question with several answers that can all be true ("How do you want to be contacted?": email, text message, letter). CheckboxGroup.Root renders the <fieldset> and acts as a Fieldset.Root with group set, so CheckboxGroup.Legend, a description (CheckboxGroup.Prose), a help text (CheckboxGroup.HelpText) and CheckboxGroup.ErrorMessage work inside it. It holds no form state: the selected values are the value prop, and each change is reported up.

PartElement / roleARIA / stateNotes
CheckboxGroup.Root<fieldset> → groupAs Fieldset: aria-describedby = descriptions, then the error. Native disabled. data-invalid, data-required, data-disabled. No aria-invalid and no aria-required (not supported on group)Classes kv-checkbox-group kv-fieldset. Props: name, value, defaultValue, onValueChange(value, { reason: 'input', event }), invalid, required, disabled, messages, and every fieldset prop. The root is always a <fieldset>
CheckboxGroup.Legend<legend>, the group's nameAs on the Fieldset pageEnds with the optional marker ("(optional)" in English; other languages have their own word) in a group that isn't required: the marker belongs to the question
CheckboxGroup.Option<div> with an <input type="checkbox"> and labelThe option's name is its label. No "(optional)" on an option. The option's own help text (a Field.HelpText in its Option, outside the label) is in that checkbox's aria-describedbyAn Option has class kv-option, no kv-field, no error message and marker="none"; disabled disables its checkbox. Field.Root around a checkbox still works. The checkbox gets the group's name and checked from value
Checkbox in a group<input type="checkbox">data-invalid and data-disabled from the group, for styling. No aria-invalid: the group's error reaches users through the fieldset's aria-describedbyvalue is required on a Checkbox in a group; a dev warning fires when it's missing, or when checked or defaultChecked are given next to the group's (the group's wins). indeterminate and onCheckedChange still work on a Checkbox in a group. A "select all" box in the group is an option like the others: it needs a value, so its value is in the group's value while it is checked. A click calls the group's onValueChange first, with that value added or removed, then the box's own onCheckedChange (checkbox-group.test.tsx › a "select all" Checkbox in a group: onValueChange first, with its value added, then its own onCheckedChange). A box without a value warns and gets no name or state from the group (an uncontrolled group without a name still reads every checked box in the fieldset, so it reports such a box as "on"): put a select-all box outside the CheckboxGroup if its value shouldn't be in the group's
useCheckboxGroupthe logic, for your own fieldsetreturns name, groupRef, getCheckboxProps(value)Options: name, value, defaultValue, onValueChange, invalid, disabled. Use it with useFieldset({ group: true }) and your own markup. Attach groupRef to your <fieldset> so an uncontrolled group can report its next value

Rules, tested in checkbox-group.test.tsx:

  • The group is named by its legend and described by its help text and error (checkbox-group.test.tsx › the group is named by its legend and described by its help text and error). The legend is the first child.
  • Controlled by value. With value={['email']}, the checkbox with value="email" is checked and the others aren't. A change calls onValueChange with the next array: the current value with the value added at the end, or removed (checkbox-group.test.tsx › onValueChange reports the next array from the value prop). The group never stores it; if the parent doesn't update value, the box stays as it was.
  • Uncontrolled without value. defaultValue sets the initial boxes (defaultChecked), the browser keeps the state, and a form submit sends every checked box under the group's name (FormData.getAll). onValueChange still fires, with the checked values read from the group's own <fieldset> at that moment (checkbox-group.test.tsx › uncontrolled: defaultValue, the form submit and onValueChange).
  • name goes on every checkbox in the group. A name on a Checkbox wins.
  • invalid marks the group's own parts (legend, help text, error: data-invalid) and every option input (data-invalid, styling only). It doesn't cascade to the Fields' invalid, and no checkbox gets aria-invalid from it (checkbox-group.test.tsx › invalid styles every option and sets no aria-invalid).
  • required removes the optional marker ("(optional)" in English; other languages have their own word) from the legend and sets data-required. A group has no aria-required; see Known issues.
  • disabled is native fieldset[disabled]: every checkbox inside is disabled and leaves the Tab sequence (checkbox-group.test.tsx › disabled disables every checkbox natively).
  • No arrow-key model. The root attaches no key handler (checkbox-group.test.tsx › Arrow keys do not move focus between checkboxes). Each checkbox is its own Tab stop, in DOM order (checkbox-group.test.tsx › Tab moves through every checkbox in DOM order).

It announces nothing.

What you need to do#

  • A CheckboxGroup.Legend that asks the question, first in the Root. Say how many answers are allowed in a CheckboxGroup.Prose above the options ("Select all that apply"): it is read before answering, so it is a description. Use a CheckboxGroup.HelpText under the options only for what helps while answering ("You can change your mind later under My pages"). Both are text: the accessible description is the text content, so keep them short and plain, with no link in a help text. A Prose or HelpText directly in the group, not inside an option, describes the group; a Prose that isn't a description goes outside the group. An option's help text is a Field.HelpText in the option's CheckboxGroup.Option. It sits directly under the label and outside it, so it is not part of what you click (checkbox-group.test.tsx › an option help text is a Field.HelpText in that CheckboxGroup.Option, outside its label, and has no axe violations). CheckboxGroup.HelpText is listed after the description and before the error (checkbox-group.test.tsx › CheckboxGroup.HelpText is the group’s help text: after the description, before the error).
  • A CheckboxGroup.Option with a direct-child Checkbox and a Field.Label for each option; value on every Checkbox.
  • Keep the list short. About 15 options is the limit before a search or a different pattern is better. Never <select multiple>.
  • Pass value and onValueChange from your form state, or defaultValue and name for a plain <form>.
  • Set invalid and render a CheckboxGroup.ErrorMessage, then move focus to the first checkbox of the group on submit (or to the error summary).
  • "None of the above" is an option the consumer adds and handles; the group has no exclusive option.

Warning: Don't forget

Start the group with a CheckboxGroup.Legend that asks the question. Without it, a screen reader user hears the options with no question to answer (1.3.1 Info and Relationships; 3.3.2 Labels or Instructions).

Visual and modes#

  • As Fieldset and Checkbox (see their pages). The options are a small gap apart (smaller in the compact density), and the legend sits above the description.
  • forced-colors behaviour: as Checkbox. The group has no border of its own.
  • RTL: the boxes are at the inline start, the right.

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • On error: the group never moves focus. Move focus to the first checkbox of the group, or to an error summary.
  • Never obscured by: nothing around the boxes clips the ring.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): native fieldset and legend, the group's help text and error in its description.
  • 3.3.1 Error Identification (A), 3.3.2 Labels or Instructions (A), 3.3.3 Error Suggestion (AA): the legend asks the question, the help text says how many, the error says what to do.
  • 2.1.1 Keyboard (A), 2.4.3 Focus Order (A), 3.2.2 On Input (A): native keys, nothing moves focus when a box is checked.
  • All the Checkbox criteria, for each option.

Keyboard#

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

Checkboxes are independent, so each is its own Tab stop; the group is not a composite widget and arrow keys do nothing. The keys of one box are on the Checkbox page.

KeyWhereWhat happens
Tabbefore the groupMoves focus to the first checkbox. Each checkbox in the group is a Tab stop, in DOM order
Test: checkbox-group.test.tsx › Tab moves through every checkbox in DOM order
Shift+Tabon a checkboxMoves focus to the previous checkbox, then out of the group
Test: checkbox-group.test.tsx › Shift+Tab moves back through the checkboxes
Spaceon a checkboxToggles that checkbox only, and reports the next value (native)
Test: checkbox-group.test.tsx › Space toggles the focused checkbox and reports the next value
ArrowDown / ArrowUp / ArrowLeft / ArrowRighton a checkboxNothing: focus and the checked boxes stay (native, not a composite)
Test: checkbox-group.test.tsx › Arrow keys do not move focus between checkboxes
Tabdisabled groupSkips every checkbox of a disabled group (native fieldset[disabled])
Test: checkbox-group.test.tsx › Tab skips the checkboxes of a disabled group (native)

API reference#

import { Checkbox, CheckboxGroup, Field, useCheckboxGroup } from '@kvirn-ui/react'

CheckboxGroup.Root

Renders <fieldset>, which a screen reader announces as a group. It is a Fieldset.Root with group set, takes every attribute of a <fieldset> and passes ref to it. Its options are CheckboxGroup.Option parts, each with a Checkbox and its label. The default theme is described on Theming.

PropTypeDefaultDescription
namestring–The name every checkbox in the group submits under. A Checkbox’s own name wins.
valuereadonly string[]–Controlled: the values of the checked boxes, from your form state. The group never stores it.
defaultValuereadonly string[]–Uncontrolled: the values checked at the start. The browser keeps the state after that.
onValueChange(value: string[], details: { reason: "input"; event }) => void–Called when a box changes. Controlled, it is value with the box’s value added at the end or removed. Uncontrolled, it is read from the checked boxes in DOM order.
invalidbooleanfalseMarks the group and every checkbox with data-invalid, and shows the ErrorMessage. It sets no aria-invalid and does not make the Fields inside invalid.
requiredbooleanfalseSets data-required and removes the optional text from the legend. A group has no aria-required, so say what is required in the legend.
disabledbooleanfalseNative fieldset disabled: every checkbox is disabled and skipped by Tab. Every checkbox also gets data-disabled.
messagesPartial<KvirnMessages["field"]>–Overrides the legend’s optional text and the error prefix for this group.
Class or attributeValuesMeaning
kv-checkbox-groupalwaysThe part class. It sits next to kv-fieldset.
kv-fieldsetalwaysThe fieldset class the default theme styles.
data-invalidpresent or absentThe group is invalid. Also on the legend, the help text, the error and every checkbox.
data-requiredpresent or absentThe group is required. Also on the legend.
data-disabledpresent or absentThe group is disabled. Also on every checkbox.
aria-describedbyidsSet for you: every description and help text in DOM order, then the error. Your own ids come after.

CheckboxGroup.Option

Renders <div> (or <li>) around one answer: its checkbox, its Field.Label and its Field.HelpText. It has no “(optional)” marker and no error message of its own: those belong to the question.

PropTypeDefaultDescription
disabledbooleanfalseDisables this option’s control. Its label and help text get data-disabled. To disable every option, set disabled on the group.
controlIdstringgeneratedThe control’s id. Set it to link to the option from an error summary; the label, help text and control stay wired to it.
as"div" | "li""div"The element to render. Use li when the options are a list, and then put the options in a <ul role="list"> inside the group, after the legend, so a screen reader announces the list.
Class or attributeValuesMeaning
kv-optionalwaysThe part class. The default theme styles it.
data-disabledpresent or absentThe option is disabled.

CheckboxGroup.Legend

Renders <legend>, the question and the group’s name. Put it first.

PropTypeDefaultDescription
marker'optional' | 'none''optional' in a group that isn’t required, else 'none'Whether the legend ends with the optional text, for example “(optional)”.
Class or attributeValuesMeaning
kv-fieldset-legendalwaysThe part class.
data-invalidpresent or absentThe group is invalid.
data-requiredpresent or absentThe group is required.
data-disabledpresent or absentThe group is disabled.

CheckboxGroup.Prose

Renders <div> with the class kv-prose: the description, read before answering, above the options. It is listed in the group’s aria-describedby.

Class or attributeValuesMeaning
kv-prosealwaysThe part class. The default theme sets it at 16px.
data-invalidpresent or absentThe group is invalid.
data-disabledpresent or absentThe group is disabled.

CheckboxGroup.HelpText

Renders <p>: a short instruction under the options. It is listed in the group’s aria-describedby. A help text for one option is a Field.HelpText in that option’s CheckboxGroup.Option.

Class or attributeValuesMeaning
kv-field-help-textalwaysThe part class. The default theme sets it at 14px.
data-invalidpresent or absentThe group is invalid.
data-disabledpresent or absentThe group is disabled.

CheckboxGroup.ErrorMessage

Renders <p> that starts with the error icon and the hidden prefix. It renders only while the group is invalid, is the last item in the group’s aria-describedby, and is not a live region. Render one per group.

Class or attributeValuesMeaning
kv-field-error-messagealwaysThe part class.
data-invalidalwaysIt renders only while the group is invalid.
data-disabledpresent or absentThe group is disabled.

useCheckboxGroup

Options

PropTypeDefaultDescription
namestring–The name every checkbox in the group submits under.
valuereadonly string[]–Controlled: the values of the checked boxes.
defaultValuereadonly string[]–Uncontrolled: the values checked at the start.
onValueChange(value: string[], details: { reason: "input"; event }) => void–Called with the next values when a box changes. It only reports.
invalidbooleanfalsedata-invalid on every box, for styling. No aria-invalid.
disabledbooleanfalsedata-disabled on every box. A <fieldset disabled> does the disabling.

Result

PropTypeDefaultDescription
namestring | undefined–The name, as given.
groupRefRefCallback<HTMLFieldSetElement>–Attach it to your <fieldset>, so an uncontrolled group can read its checked boxes when it reports.
getCheckboxProps(value: string) => CheckboxGroupItemProps–Spread on the <input type="checkbox"> with this value: name, checked or defaultChecked, onChange and the data attributes.

Strings

These are the default texts in @kvirn-ui/i18n.

For your whole app, pass messages to KvirnProvider. For one CheckboxGroup, pass its messages prop.

These are the Field strings, because the legend and the error use them.

field.optional

The legend’s marker when the group is not required.

LanguageDefault text
Svenska(valfritt)
Suomi(vapaaehtoinen)
Norsk bokmål(valgfritt)
Norsk nynorsk(valfritt)
English(optional)

field.errorPrefix

Read before the error message, so it is not mistaken for help.

LanguageDefault text
SvenskaFel:
SuomiVirhe:
Norsk bokmålFeil:
Norsk nynorskFeil:
EnglishError:

Known issues#

  • A required group isn't announced as required. A screen reader user is not told that an answer is needed. aria-required isn't supported on group (ARIA 1.2), and KvirnUI chooses aria-required over native required, so a required group has only two signs: no optional marker in its legend, and data-required for styling. Say that an answer is needed in text: in the legend, or in a CheckboxGroup.Prose before the options ("Select at least one"). Prefer the legend, because TalkBack may not read the group's description (3.3.2 Labels or Instructions (A), 1.3.1 Info and Relationships (A)).
  • aria-describedby on a <fieldset> isn't announced consistently by TalkBack. On TalkBack (Android's screen reader) the help text and the error may not be read when the user enters the group. See the Fieldset page.
  • Uncontrolled onValueChange reads the DOM. It reads the checked boxes of the group's <fieldset> when a box changes.

Warning: Don't forget

A required group is not announced as required. Write that an answer is needed in the legend or in the text above the options (3.3.2 Labels or Instructions).

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/apasscheckbox-group.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasscheckbox-group.test.tsx (axe and role, name and state): the group is named by its legend and described by its help text and error
Keyboard contract rows (Vitest browser)passn/apass5 rows, each names a test in checkbox-group.test.tsx
Focus order and managementpassn/apasscheckbox-group.test.tsx: Tab moves through every checkbox in DOM order
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 into the group: the legend and "group" are read, then the help text and the error.
  • Each box is one Tab stop, in reading order. The arrow keys move nothing.
  • Space changes only the focused box.
  • On an invalid group the error is read when focus enters.