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.

RadioGroup

One question with exactly one answer, such as how long a permit should last. It names the group with a legend, and the browser’s own radio keys do the rest.

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

When to use it#

  • Use it for one question where the user must choose exactly one option and the options fit on the screen together: a period, a delivery method, a yes or no that needs more than a checkbox.
  • Ask the question in the legend. Offer an answer for everyone: if “none” or “I don’t know” is valid, make it an option.
  • Preselect an option only when one answer is overwhelmingly common and harmless, because residents rarely change a default.
  • Not for a long list. From about 7 options a Listbox is shorter to scan.
  • Not for answers that can all be true: use a CheckboxGroup.

Example#

How long do you need the permit? (optional)

Choose the period that matches your project.

Use cases#

Keep the choice in your form state

Your form library or reducer owns the value. Give the group the chosen value and store the one it reports. The group never stores it. Pass null, not undefined, to say the group is controlled and nothing is chosen.

How long do you need the permit? (optional)

You chose: nothing

Props in this example:

Information: Did you know?

The arrow keys check the radio they move to, so nothing slow or harmful may happen on change. Leave the page change, the save and the submit to a button.

A plain form with no state

Without value the radios are native and uncontrolled. Set which one starts checked with defaultValue, and give the group a name. A form submit then sends the chosen value under that name.

How long do you need the permit? (optional)

Props in this example:

Explain one option

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

How long do you need the permit? (optional)

The most common choice for a building project.

You can apply for an extension later.

One question on a page

When a page asks one question, the legend can be the page’s heading. Put the heading inside the legend and add the class kv-fieldset-legend--heading, which gives it the size of a page heading. Use h1 if it is the page’s main heading. This example is inside a page that already has one, so it uses h2. When the person presses Continue with nothing chosen, show an error, as in Say what is wrong when nothing is chosen.

Who are you applying for?

You must answer this question to continue. We use your answer to show the right form.

Props in this example:

Information: Did you know?

Set required on the group, as here. Without it the legend adds “(optional)” after your heading, so a question the person must answer looks optional (3.3.2 Labels or Instructions).

Say what is wrong when nothing is chosen

Check the answer when the user sends the form. Set invalid and render a RadioGroup.ErrorMessage that says what to do. The error is read as part of the group’s description, because a radio can’t be marked invalid. required removes “(optional)” from the legend, but a group can’t be announced as required, so say once, before the first question, that every question must be answered unless it says “(optional)”.

Every question must be answered unless it says (optional).

How long do you need the permit?

Props in this example:

Warning: Don't forget

The group never moves focus. After a failed submit, move it to the first radio or to an ErrorSummary, as in What you need to do.

Say why an answer was not accepted

Sometimes a person answers, but you can’t accept the answer, and you only find out after they send the form. Here the chosen way to get a decision is not set up. After the check, set invalid on the group and render a RadioGroup.ErrorMessage that names the reason and says what to do next. Also show an ErrorSummary whose link goes to the group’s first option: give that option a controlId and use the same id in the link. As above, the error is read with the group’s description, because a radio can’t be marked invalid. If you know before the person chooses, disable the option and say why, as in One option isn’t available.

Every question must be answered unless it says (optional).

How do you want to get your decision?

Props in this example:

Warning: Don't forget

Use the same words in the summary and under the group. Name the reason and say what to do next (3.3.1 Error Identification, 3.3.3 Error Suggestion).

One option isn’t available

Tab and the arrow keys skip a disabled radio (the browser does this). A screen reader can still reach it when reading the page, and tells the user it can't be used (each screen reader words it differently). So say why in visible help text next to it, with what the person can do. To disable every option, set disabled on the group.

How long do you need the permit? (optional)

Not available for this kind of permit.

Props in this example:

Let people answer “Other” in their own words

A free-text answer belongs to the option it qualifies, and it is only asked when it is needed. The text field appears when “Other” is chosen, so people who pick another option don’t see it.

How did you hear about the service? (optional)

Props in this example:

  • value on RadioGroup.Reveal
  • value on RadioGroup.Radio

When “Other” is checked, the text field appears right after it. A screen reader says An input field has appeared below. once, politely, and reads the radio with 1 control. That sentence is the default text of radioGroup.revealShown (see Strings). Everything else it says is under What a screen reader reads.

Warning: Don't forget

A plain form still sends what was typed as source-other, so ignore it on the server. Give the field a label of its own, and make it required only while “Other” is chosen.

Tell several “Other” questions apart by their announcement

When someone checks “Other”, a screen reader says that a field has appeared, right after it reads the radio. In English the default text is “An input field has appeared below.”, and it follows the language of the page. That is enough for one group. When a page has several, give each Reveal its own text that names its field, as below, so the person hears which one opened. Every group announces, also when you check them one after the other. See What a screen reader reads.

How did you hear about the service? (optional)
How should we reply to you? (optional)

Props in this example:

Information: Implement like this

To use one text for the whole site, pass it to the provider, as below. Write it in the language of the page: for another language, start from that language’s catalog instead of en. An empty text is ignored, so the default is used. Locales explains the rest.

import { defineMessages } from '@kvirn-ui/i18n'
import { en } from '@kvirn-ui/i18n/en'

// The text it replaces: en.radioGroup.revealShown
const messages = defineMessages(en, {
  radioGroup: { revealShown: 'A box for your own words has opened below.' },
})

<KvirnProvider messages={messages}>{/* your site */}</KvirnProvider>

A denser group for staff screens

A caseworker who records many decisions a day can fit more on the screen. Put the class kv-compact on the form, on any container around the group, or on <html> for a whole staff tool. It is a class, not a prop. From a window 64rem wide (1024 pixels at the browser’s default text size), option rows are 32px high instead of 44px, the legend and option labels are 14px instead of 16px, options are 4px apart instead of 8px, and questions are 16px apart instead of 24px. In a narrower window nothing changes, because touch is likely there. The circle stays at least 24 by 24 pixels, and the description and error text stay 16px.

What is your decision on this application?

You must choose one before you save the decision.

Information: Did you know?

Compact rows meet 2.5.8 Target Size (Minimum) (AA), but not the 44px of 2.5.5 Target Size (Enhanced) (AAA). Keep services for residents at the default size.

Accessibility#

What it does for you#

  • APG pattern: Radio Group. Native: <input type="radio"> elements that share a name already give the pattern's keys (one Tab stop, arrows move and check, Space checks) and its roles, so no ARIA and no key handler are added. The group is a <fieldset> with a <legend> instead of role="radiogroup".
  • Deviations: none from APG. Design decisions: the group is a Fieldset, option labels carry no optional marker, no aria-invalid on radios, and the group holds no form state.
  • Native elements used: <fieldset> and <legend> (RadioGroup.Root and RadioGroup.Legend), <input type="radio"> (RadioGroup.Radio), <label for> (Field.Label).

A RadioGroup is one question with exactly one answer ("How long do you need the permit?": 1, 6 or 12 months). RadioGroup.Root renders the <fieldset> and acts as a Fieldset.Root with group set; RadioGroup.Radio is a native radio. It holds no form state: the selected value is the value prop, and each change is reported up.

PartElement / roleARIA / stateNotes
RadioGroup.Root<fieldset> → groupAs Fieldset: aria-describedby = descriptions, then the error. Native disabled. data-invalid, data-required, data-disabled. No aria-invalid and no aria-requiredClasses kv-radio-group kv-fieldset. Props: name (generated when missing), value, defaultValue, onValueChange(value, { reason: 'input', event }), invalid, required, disabled, messages, and every fieldset prop. The root is always a <fieldset>
RadioGroup.Legend<legend>, the group's nameAs in the Fieldset pageEnds with the optional text in a group that isn't required: "(optional)" in the default locale en, and each language has its own
RadioGroup.Option<div> with an <input type="radio"> and its labelThe option's name is its label. No optional marker on an option. The option's own help text (a Field.HelpText in its Option, outside the label) is in that radio's aria-describedby. Native checked, native grouping by name. data-state when controlledAn Option has class kv-option, no kv-field, no error message and marker="none"; disabled disables its radio. Field.Root around a radio still works. The radio gets the group's name, checked from value, and value from its prop
RadioGroup.Radio<input type="radio"> → radiodata-state="checked" | "unchecked" only when the radio is controlled (the group has a value, so the state is known from props; an uncontrolled radio has none: style :checked), data-invalid and data-disabled (styling only). No data-focus-visible: focus is the native :focus-visible. Never aria-invalid, not even when its Field is invalid. aria-controls = the id of its RadioGroup.Reveal while one is mounted. No aria-requiredClass kv-radio. Props: value (required), disabled, onCheckedChange is not offered (use the group's onValueChange), and every native input prop. It must be a direct child of RadioGroup.Option (or Field.Root), before the label
RadioGroup.Reveal<div> (or <section> via as), no roleStable id, hidden until the radio with its value is checked, data-state="open" | "closed". Its Fields keep their own marker and required state, and their help text is not in the group's descriptionClass kv-radio-group-reveal. Props: value (required), as, messages (radioGroup.revealShown) and native div props. Put it right after its radio's RadioGroup.Option. Never unmounted
useRadioGroupthe logic, for your own fieldsetreturns name, getRadioProps(value), getRevealProps(value), registerReveal(value), consumeUserChoice(value), resetSelection()Options: name, value, defaultValue, onValueChange, invalid, disabled. It holds UI state only (the value that decides which reveal shows). Use it with useFieldset({ group: true }) and your own markup
useRadiothe same attributes, for your own elementsinputProps, isChecked, isInvalid, isDisabled. No isFocusVisibleSpread inputProps on your <input type="radio">

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

  • The group is named by its legend and described by its help text and error (radio-group.test.tsx › the group is named by its legend and described by its help text and error). The description is a RadioGroup.Prose above the options and the help text a RadioGroup.HelpText under them (the Fieldset.HelpText under the group's name): the description is the text content, so keep both short and plain, and a help text has no links. A Prose or HelpText directly in the group describes the group, one inside an option's Option describes that radio, and a Prose that isn't a description goes outside the group. An option's help text is a Field.HelpText in the option's RadioGroup.Option, in the small text size, directly under the label and outside the click target (radio-group.test.tsx › an option help text is a Field.HelpText in that RadioGroup.Option, outside its label, and has no axe violations). RadioGroup.HelpText is listed after the description and before the error (radio-group.test.tsx › RadioGroup.HelpText is the group’s help text: after the description, before the error).
  • One name, native grouping. Every radio has the group's name, so the browser groups them. Without name, the group generates one (useId) (radio-group.test.tsx › every radio gets the group’s name, generated when missing).
  • Controlled by value. The radio whose value equals the group's value is checked. null means "controlled, nothing selected". A change calls onValueChange(value, { reason: 'input', event }) with the radio's value. The group never stores it (radio-group.test.tsx › a controlled group checks the radio whose value it is given).
  • Uncontrolled without value. defaultValue sets defaultChecked on one radio; the browser keeps the state and a form submit sends it (radio-group.test.tsx › uncontrolled: defaultValue and the form submit).
  • No aria-invalid on radios (ARIA 1.2 doesn't support it on radio). An invalid group sets data-invalid on the fieldset, its parts and every radio, so the theme can draw a thicker edge that does not rely on colour; the error reaches users through the fieldset's aria-describedby (radio-group.test.tsx › invalid styles every radio and sets no aria-invalid).
  • required removes the optional text (in en, "(optional)") from the legend and sets data-required. No aria-required (not supported on group or radio); see Known issues.
  • disabled on the group is native fieldset[disabled]. A single disabled Radio is skipped by Tab and by the arrow keys (native) (radio-group.test.tsx › a disabled radio is natively disabled). This is native radio behaviour, as the APG keyboard practice expects: an aria-disabled radio would be checked by the browser's own arrow keys. Put the reason in the option's help text.
  • RadioGroup.Reveal (the "Other, please specify" field). <RadioGroup.Reveal value="other"> follows its radio's RadioGroup.Option in the DOM and is hidden until the radio with that value is checked, so the field is next in the reading and Tab order (1.3.2) (radio-group.test.tsx › the reveal is hidden until its radio is checked, radio-group.test.tsx › the reveal follows its radio’s option in DOM order). It is never unmounted: the typed text survives switching options (radio-group.test.tsx › checking another radio hides the reveal again and keeps what was typed) and a plain form still submits it, so ignore or clear it on the server when its radio is not the chosen one.
  • The radio points at the reveal. While a Reveal for it is mounted the radio has aria-controls with the Reveal's id; the id is stable, so it never points at nothing. No aria-expanded: not supported on radio (radio-group.test.tsx › the radio points at the reveal with aria-controls, the others do not).
  • Focus never moves. Choosing the radio does not move focus into the field (3.2.2); the arrow keys keep moving through the radios (radio-group.test.tsx › focus stays on the radio when the field appears).
  • Selected value for the reveal. Controlled: the group's value. Uncontrolled: the group mirrors the radio the user chose (starting from defaultValue) as UI state only, and goes back to defaultValue when the surrounding <form> resets (radio-group.test.tsx › a form reset removes the field from the reading order).
  • No key handler. Roving focus, wrapping and checking on arrow keys are the browser's, and tested in the Keyboard table below (for example radio-group.test.tsx › Tab leaves the group after one stop and radio-group.test.tsx › ArrowDown and ArrowRight move to the next radio and check it, wrapping). No page handler cancels a key.

It announces nothing.

What you need to do#

  • A RadioGroup.Legend that asks the question, first in the Root; a RadioGroup.Option with a direct-child RadioGroup.Radio and a Field.Label per option; a value on every Radio.
  • Offer an answer for everyone. If "none" or "I don't know" is valid, it's an option; the group has no deselect.
  • Preselecting an option is a decision with consequences (residents rarely change a default): do it only when one answer is overwhelmingly common and harmless.
  • Pass value and onValueChange from your form state, or defaultValue and name for a plain <form>.
  • Set invalid and render a RadioGroup.ErrorMessage. On submit, move focus to the checked radio or the first one of an invalid group (or to the ErrorSummary).
  • With a RadioGroup.Reveal, put it right after its radio's RadioGroup.Option, give its field a label of its own, and make the field required only while its radio is chosen. A hidden field is still submitted: ignore or clear it when another option is chosen.
  • Don't use a radio group for a long list: from about 7 options, a Listbox is shorter to scan.
  • Say once, before the first question, that every question must be answered unless it says it is optional (in en: "Every question must be answered unless it says (optional)"), per 3.3.2 Labels or Instructions (A).

Warning: Don't forget

Ask the question in the legend, and give every radio a value and a label. Without the legend a screen reader reads the options with no question (1.3.1 Info and Relationships, 3.3.2 Labels or Instructions).

Visual and modes#

  • Focus indicator: the default theme draws an outline around the circle, at least 2px thick, with at least 3:1 contrast against its background (2.4.7 Focus Visible, AA). Keep it if you restyle.
  • Target size: the circle is at least 24 by 24 CSS pixels (2.5.8 Target Size (Minimum), AA). The whole row is the click target: the label spans the row, and the row is taller in comfortable density than in compact density.
  • Colour: the circle has an edge with at least 3:1 contrast against the page (1.4.11 Non-text Contrast, AA). Checked is a dot inside a ring, so the shape carries the state, not only colour (1.4.1 Use of Color, A). Invalid is a thicker edge plus the error message. Disabled is a dashed edge, and a checked disabled radio keeps its dot in the muted text colour.
  • forced-colors behaviour (a mode where the system chooses the colours): the dot is drawn as a filled shape, and the colours are set explicitly: a checked radio uses the system's highlight colour for its edge and dot, an invalid one a thicker edge in the system's text colour, and a disabled one the system's greyed-out text colour.
  • The Reveal has an inline-start bar and indent that tie it to its radio; a border stays visible in forced colours. It does not animate.
  • reduced-motion behaviour: the edge and fill change with a transition only when motion is allowed; the dot appears instantly.
  • RTL (right-to-left text): the circles are at the right of their labels; the arrow keys are mirrored by the browser (see Keyboard).

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • On error: the group never moves focus. An ErrorSummary moves focus to itself; without one, the consumer moves focus to the checked radio, or the first one, of the invalid group.
  • Never obscured by: nothing around the circles clips the focus ring (2.4.11 Focus Not Obscured (Minimum), AA).

WCAG success criteria#

  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): native radios grouped by name, in a fieldset with a legend.
  • 1.3.2 Meaningful Sequence (A): a RadioGroup.Reveal follows its radio in the DOM. 4.1.3 Status Messages (AA): the field's appearance is announced politely.
  • 1.4.1 Use of Color (A), 1.4.11 Non-text Contrast (AA): the dot's shape, the edge, the invalid width.
  • 2.1.1 Keyboard (A), 2.4.3 Focus Order (A), 3.2.2 On Input (A): one Tab stop, arrows, Space, no context change.
  • 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA), 2.4.13 Focus Appearance (AAA, not counted toward the AA bar).
  • 2.5.3 Label in Name (A), 2.5.8 Target Size (Minimum) (AA).
  • 3.3.1 Error Identification (A), 3.3.2 Labels or Instructions (A), 3.3.3 Error Suggestion (AA): the legend asks, the help text helps, the error says what to do.

Keyboard#

Focus
native
Selection follows focus
yes (native: an arrow key moves focus and checks the radio; instant and harmless, as the APG allows)
Arrows wrap
yes (native: from the last radio back to the first, and the reverse)
Shortcuts
none

The group is one Tab stop (a composite widget). The browser's own roving is used instead of tabindex: the checked radio, or the first (last, going backwards) when none is checked, is the stop. Home and End do nothing; they are not part of the APG radio group.

KeyWhereWhat happens
Tabbefore the group, none checkedMoves focus to the first radio. The rest of the group is skipped
Test: radio-group.test.tsx › Tab enters the group at the first radio when none is checked
Tabbefore the group, one checkedMoves focus to the checked radio
Test: radio-group.test.tsx › Tab enters the group at the checked radio
Tabon a radioLeaves the group, to the next focusable element. A controlled group stays one Tab stop after an arrow key
Test: radio-group.test.tsx › Tab leaves the group after one stop, radio-group.test.tsx › a controlled group is still one Tab stop after an arrow key
Shift+Tabafter the group, none checkedMoves focus to the last radio
Test: radio-group.test.tsx › Shift+Tab enters the group at the last radio when none is checked
Shift+Tabon a radioLeaves the group, to the previous focusable element
Test: radio-group.test.tsx › Shift+Tab leaves the group after one stop
ArrowDown / ArrowRighton a radioMoves focus to the next radio and checks it. From the last it wraps to the first. Right is the next in left-to-right text, and Left in right-to-left (the browser mirrors it)
Test: radio-group.test.tsx › ArrowDown and ArrowRight move to the next radio and check it, wrapping, radio-group.test.tsx › right to left: ArrowLeft moves to the next radio and ArrowRight to the previous
ArrowUp / ArrowLefton a radioMoves focus to the previous radio and checks it. From the first it wraps to the last. Left is the previous in left-to-right text, and Right in right-to-left
Test: radio-group.test.tsx › ArrowUp and ArrowLeft move to the previous radio and check it, wrapping, radio-group.test.tsx › right to left: ArrowLeft moves to the next radio and ArrowRight to the previous
ArrowDown / ArrowUpnext radio is disabledSkips the disabled radio (native)
Test: radio-group.test.tsx › Arrow keys skip a disabled radio
ArrowDown / ArrowUpa revealed field follows the radioMoves to the next or previous radio, not into the revealed field
Test: radio-group.test.tsx › ArrowDown moves from the radio to the next radio, not into the revealed field, radio-group.test.tsx › ArrowUp moves from the second radio back to the first, past the revealed field
Tabon a radio, its reveal hiddenLeaves the group; the hidden field is skipped
Test: radio-group.test.tsx › Tab from the checked radio skips the hidden reveal field and leaves the group
Tabon a radio with a revealed fieldMoves focus into the revealed field; the next Tab continues after the group
Test: radio-group.test.tsx › Tab from the checked radio reaches the revealed field, radio-group.test.tsx › Tab from the revealed field continues after the group
Shift+Tabin the revealed fieldMoves focus back to the checked radio
Test: radio-group.test.tsx › Shift+Tab from the revealed field returns to the radio
Spaceon an unchecked radioChecks the focused radio. On a checked radio it does nothing (native)
Test: radio-group.test.tsx › Space checks the focused radio

Arrow keys check the radio they move to, so the page must not do anything harmful or slow on change (3.2.2 On Input, A). A native radio's change event is instant. Clicking the label, or the padding around the circle, checks and focuses the radio (radio-group.test.tsx › clicking the label text selects the radio).

Announcements#

What a screen reader reads#

State or actionRead aloudPolitenessTest
Enter the groupgroup, How did you hear about us? (optional), radio, Other, not checked, 1 control, …noneradio-group.test.tsx › entering the group reads the radios, the controlling one with its control, and no field
Check Otherradio, Other, checked, 1 control, position 2, set size 2, then An input field has appeared below.politeradio-group.test.tsx › it is announced politely, exactly once, when the user checks the radio, radio-group.test.tsx › after checking Other the field is read straight after its radio, radio-group.test.tsx › the field reads its label and optional marker as a textbox (the checked radio's phrase)
Tab into the fieldtextbox, Describe (optional)noneradio-group.test.tsx › the field reads its label and optional marker as a textbox
Check another optionnothing; the field leaves the reading ordernoneradio-group.test.tsx › checking another option removes the field from the reading order
Form resetnothing; the field leaves the reading ordernoneradio-group.test.tsx › a form reset removes the field from the reading order
Mount with Other already checkedtextbox, Describe (optional), and nothing livenoneradio-group.test.tsx › it is not announced on mount with a checked radio

API reference#

import { Field, RadioGroup, useRadio, useRadioGroup } from '@kvirn-ui/react'

RadioGroup.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 RadioGroup.Option parts, each with a RadioGroup.Radio and its label. The default theme is described on Theming.

PropTypeDefaultDescription
namestringgeneratedThe name every radio shares, so the browser groups them and a form submit sends the choice.
valuestring | null–Controlled: the value of the checked radio, from your form state. null means controlled with nothing chosen.
defaultValuestring–Uncontrolled: the value checked at the start. The browser keeps the state after that.
onValueChange(value: string, details: { reason: "input"; event }) => void–Called with the value of the radio the user chose. The group never stores it.
invalidbooleanfalseMarks the group and every radio with data-invalid, and shows the ErrorMessage. A radio never gets aria-invalid.
requiredbooleanfalseSets data-required and removes the optional text from the legend. Neither a group nor a radio has aria-required, so say what is required in the legend.
disabledbooleanfalseNative fieldset disabled: every radio is disabled and skipped by Tab and the arrow keys. Every radio also gets data-disabled.
messagesPartial<KvirnMessages["field"]>–Overrides the legend’s optional text and the error prefix for this group.
Class or attributeValuesMeaning
kv-radio-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 radio.
data-requiredpresent or absentThe group is required. Also on the legend.
data-disabledpresent or absentThe group is disabled. Also on every radio.
aria-describedbyidsSet for you: every description and help text in DOM order, then the error. Your own ids come after.

RadioGroup.Option

Renders <div> (or <li>) around one answer: its radio, 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.

RadioGroup.Radio

Renders <input type="radio"> with the role radio. It takes every attribute of an <input> and passes ref to it. Put it directly in a RadioGroup.Option, before the label. It never has aria-invalid or aria-required: ARIA doesn’t support them on a radio.

PropTypeDefaultDescription
valuestring–What a form submit sends when this radio is checked, and its value inside the group. Give every radio one.
checkedboolean–Outside a RadioGroup only. In a group, the group’s value sets it.
defaultCheckedboolean–Outside a RadioGroup only. In a group, the group’s defaultValue sets it.
disabledbooleanfalseNative disabled: this radio is skipped by Tab and the arrow keys.
Class or attributeValuesMeaning
kv-radioalwaysThe part class. The default theme styles it.
data-state"checked" or "unchecked"Only when the checked state is known from props. For an uncontrolled radio, style :checked.
data-invalidpresent or absentThe group or the Field is invalid.
data-disabledpresent or absentThe radio, the group or the Field is disabled.

RadioGroup.Reveal

Renders <div> (or <section>) that holds what belongs to one radio, such as an “Other, please specify” field. Put it right after that radio’s RadioGroup.Option. It is hidden until the radio is checked, is never unmounted, and never moves focus. The radio gets aria-controls. When someone checks the radio, a screen reader says “An input field has appeared below.” politely (see Strings to change the text). The typed text is still submitted by a plain form: ignore or clear it on the server.

PropTypeDefaultDescription
valuestringRequiredThe value of the radio this content belongs to. It is shown when that radio is checked.
messagesPartial<KvirnMessages["radioGroup"]>RequiredOverrides the announcement ("an input field has appeared below") for this instance.
as"div" | "section""div"The element to render.
Class or attributeValuesMeaning
kv-radio-group-revealalwaysThe part class. The default theme styles it.
idalwaysStable, and the radio’s aria-controls points at it.
hiddenpresent or absentPresent until its radio is checked.
data-state"open" or "closed"Whether its radio is checked.

RadioGroup.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.

RadioGroup.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 styles it.
data-invalidpresent or absentThe group is invalid.
data-disabledpresent or absentThe group is disabled.

RadioGroup.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 RadioGroup.Option.

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

RadioGroup.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.

useRadioGroup

Options

PropTypeDefaultDescription
namestringgeneratedThe name every radio shares.
valuestring | null–Controlled: the value of the checked radio. null means nothing is checked.
defaultValuestring–Uncontrolled: the value checked at the start.
onValueChange(value: string, details: { reason: "input"; event }) => void–Called with the value of the radio the user chose. It only reports.
invalidbooleanfalsedata-invalid on every radio, for styling. No aria-invalid.
disabledbooleanfalsedata-disabled on every radio. A <fieldset disabled> does the disabling.

Result

PropTypeDefaultDescription
namestring–The shared name: the one given, else a generated one.
getRadioProps(value: string) => RadioGroupItemProps–The props for the radio with this value: name, checked or defaultChecked, onChange, aria-controls while a reveal is mounted, and the data attributes.
getRevealProps(value: string) => RadioGroupRevealItemProps–The props for the content that belongs to the radio with this value: id, hidden unless that radio is checked, and data-state.
registerReveal(value: string) => () => void–Call from an effect while a reveal for this value is mounted, so the radio gets aria-controls. Returns the cleanup.
consumeUserChoice(value: string) => boolean–True once when the user, not your value, just checked this radio: the moment to announce that a field appeared.
resetSelection() => void–Call when the surrounding form resets: an uncontrolled group goes back to its defaultValue.

useRadio

A hook that gives your own <input type="radio"> the same attributes, wired to the nearest Option and RadioGroup. It returns inputProps to spread on the input, and isChecked (known from props only), isInvalid and isDisabled. It has no focus state: style focus with :focus-visible.

Options

PropTypeDefaultDescription
valuestring–What a form submit sends when checked, and the option’s value inside a RadioGroup.
namestring–The name that groups the radios. Inside a RadioGroup, the group’s name is the default.
disabledbooleanfalseNative disabled: skipped by Tab and by the arrow keys.
checkedboolean–Controlled outside a RadioGroup.
defaultCheckedboolean–Uncontrolled outside a RadioGroup.

Strings

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

For your whole app, pass messages to KvirnProvider. For one RadioGroup, 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:

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

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

This string belongs to RadioGroup.Reveal, so a messages prop on the Root does not change it.

radioGroup.revealShown

Said politely once, when a user checks a radio whose Reveal opens. Not said when the page loads, when the form resets or when your code sets the value.

LanguageDefault text
SvenskaEtt fält har visats nedanför.
SuomiAlle on tullut näkyviin kenttä.
Norsk bokmålEt felt har dukket opp nedenfor.
Norsk nynorskEit felt har dukka opp nedanfor.
EnglishAn input field has appeared below.

Known issues#

  • A required group isn't announced as required. aria-required isn't supported on group or radio, and KvirnUI prefers aria-required over native required. The group's legend loses its optional text and gets data-required. If the AT run shows a gap: role="radiogroup" on the fieldset (allowed on <fieldset>, and it supports aria-required and aria-invalid), or native required on the radios.
  • A radio has no focus state of its own. useRadio returns no isFocusVisible and RadioGroup.Radio sets no data-focus-visible: a radio's focus state would make the browser stop treating the group as one Tab stop. The theme styles focus with the native :focus-visible.
  • aria-describedby on a <fieldset> isn't announced consistently by TalkBack. See the Fieldset page.
  • Shift+Tab into an unchecked group. Browsers differ on which radio gets focus: Chrome and Firefox focus the last, Safari the first. The keyboard tests run on Chromium; the optional engines run in CI.

Information: Did you know?

Check these against your own service. Where one applies, name it in your accessibility statement with the workaround, so users know before they meet it.

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/apassradio-group.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassradio-group.test.tsx › a Radio is a native <input type="radio"> with its class, no ARIA role; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass13 rows, each names a test (radio-group.test.tsx › Tab enters the group at the first radio when none is checked)
Focus order and managementpassn/apassradio-group.test.tsx asserts focus (toHaveFocus); first rule: Tab enters the group at the first radio when none is checked
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.
  • One Tab lands on the checked radio, or on the first if none is checked.
  • The arrow keys move focus and check the radio, and wrap. A disabled option is skipped. In a right-to-left page the arrows are mirrored.
  • Checking "Other" reads "An input field has appeared below." once, and Tab reaches the field.