Overview
A radio button is a native <input type="radio"> drawn as a round tile, inside a form field with a label and optional info text. The options of one choice share a name and sit in a group under a legend, so the user can pick only one. The forms script starts every group, so a radio needs no script of its own.
Pick another component when:
Markup
Variants
A row on .nds-radio-input (every <input>) or .nds-radio changes every option. Checked goes on one option only: the builder uses #radio-1, the first <input>, but on a page it goes on the option you mean.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Size | SM (default) | — | — | 16px tile. Dense forms and tables |
| Size | MD | .nds-md |
.nds-radio |
20px tile. Most forms |
| Size | LG | .nds-lg |
.nds-radio |
24px tile. Touch-first screens |
| Color | Neutral (demo: + checked) | .nds-neutral |
.nds-radio-input |
Put it on each <input>. The checked tile uses the neutral color, not the primary color. Use it when the choice is not a brand action. The color shows only when an option is checked |
| Checked | Checked (default) (id: checked) | [checked] |
#radio-1 |
This option is chosen when the page loads. Put it on one option of a group only. Leave every option unchecked when the user must decide |
| Required | Required (hint: Turn Checked off: the user must then pick an option) | [data-required] |
.nds-radio-group |
The user must pick an option before the form submits. A required mark shows before the legend. Use it with no option checked |
| Disabled | Disabled | [disabled] |
.nds-radio-input |
The user cannot pick these options now. Put it on one input to disable one option, or use data-state~="disabled" on the group. To keep one option disabled when the group is enabled again, put data-state="disabled" on that option’s container |
| Readonly | Readonly | [data-state~="readonly"] |
.nds-radio |
The group shows its value but cannot change, by pointer or by keyboard. Put it on every option’s container. The tile outline takes the disabled color, and the fill stays |
| Layout | Row | .nds-horizontal |
.nds-radio-group |
The options sit side by side and wrap |
| Field states | Label, info, feedback | — | — | Shared by every form field. See Forms |
Behavior
Readonly
data-state~="readonly" on each option’s container shows the value and stops the change, by pointer or by keyboard. The fill keeps its color, so the choice stays readable.
Required Group
data-required on the group makes the user pick an option. The form checks it on submit, and once it shows an error, each change checks it again. Call NDS.Form.clearStatus(group) to clear it.
Built-in Features
-
Form Integration
The forms script starts every radio. A container follows the disabled state of its input, in both directions. Required belongs to the group, not to one input.
-
Visual Feedback
A ripple grows around the tile on hover. Three sizes (SM, MD and LG) scale the tile and its checked ring together.
-
Responsive Layout
On a phone, the gaps between a radio, its label and the next option are smaller.
Best Practices
- Put every group in a
<fieldset class="nds-form-group nds-radio-group">with a<legend>that names the choice. Screen readers read the legend with each option. - For many short options, such as times of day, put them in a
<div class="nds-grid" style="--min-width: 6.5rem;">inside the group, after the<legend>, so they line up in as many columns as fit. Do not putnds-gridon the<fieldset>itself: it stretches the legend. - Give every radio of a group the same
name, so the browser treats them as one choice. - Offer at least two options. For one yes-or-no setting, use a Switch or a Checkbox.
- Check one option by default when most users will want it. Leave all unchecked when the user must decide.
- Put
data-requiredon the group, not on one input, so the group is validated as a whole. - Use
nds-neutralwhen the choice is not a brand action. - To lock a whole group, put
data-state="readonly"on every option’s container. - Use the default size in dense forms and tables,
nds-mdin most forms, andnds-lgon touch-first screens. - For the label, info text, feedback and the required mark, see Forms. They work the same on every field.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-required |
.nds-radio-group |
The user must pick an option before the form submits. It also adds the required mark to the legend |
data-error-message |
.nds-radio-group |
Replaces the default message, “Please select an option” |
data-state~="disabled" |
.nds-radio-group |
Disables every input in the group, and turns off pointer events. The group’s title takes the disabled color |
data-state~="readonly" |
.nds-radio |
The option cannot change. The tile outline takes the disabled color |
data-feedback-target |
an element inside the group | Receives the group’s feedback message. Without one, the message goes at the end of the group |
CSS Custom Properties
Set these on .nds-radio. The size classes set the first one.
| Property | Default | Controls |
|---|---|---|
--nds-input-size |
16px |
Tile size and label line height. nds-md and nds-lg set 20px and 24px |
--radio-tile-bg |
var(--background-default) |
Tile background |
--radio-tile-border |
var(--controls-border) |
Tile outline color |
Tokens
Set a token at :root, or on a wrapper to reach every radio inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the radio group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--radio-primary-checked | var(--controls-primary-checked)Dark mode var(--colors-primary-500) | |
--radio-primary-hovered | var(--controls-primary-hovered)Dark mode var(--colors-primary-400) | |
--radio-primary-pressed | var(--controls-primary-pressed)Dark mode var(--colors-primary-600) | |
--radio-neutral-checked | var(--controls-neutral-checked)Dark mode var(--colors-neutral-400) | |
--radio-neutral-hovered | var(--controls-neutral-hovered)Dark mode var(--colors-neutral-300) | |
--radio-neutral-pressed | var(--controls-neutral-pressed)Dark mode var(--colors-neutral-300) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_radio.scss.
| Token | Preview | Value |
|---|---|---|
--background-defaultRead by --radio-tile-bg, border | var(--colors-base-white)Dark mode var(--colors-neutral-800) | |
--background-primary | var(--colors-primary-600) | |
--border-disabledRead by --radio-tile-border | var(--colors-neutral-300)Dark mode var(--colors-neutral-600) | |
--focus-outlineRead by outline | var(--colors-base-black)Dark mode var(--colors-base-white) | |
--controls-neutral-hoveredRead by --radio-tile-border | var(--colors-neutral-600)Dark mode var(--colors-neutral-400) | |
--controls-neutral-pressedRead by --radio-tile-border | var(--colors-neutral-600)Dark mode var(--colors-neutral-400) | |
--controls-borderRead by --radio-tile-border | var(--colors-neutral-500)Dark mode var(--colors-neutral-400) | |
--controls-disabledRead by --radio-tile-bg | var(--colors-neutral-400)Dark mode var(--colors-alpha-white-30) |
JavaScript
| Method | Effect |
|---|---|
NDS.Radio.getValue(el) |
Returns the value of the checked radio, or '' when none is checked or the group is not found. el is the .nds-form-group, a radio in it, its id or a selector |
NDS.Radio.setValue(el, value) |
Checks the radio with that value as a user does, and returns true. It returns false, and changes nothing, when no radio has the value |
NDS.Radio.clear(el) |
Unchecks the checked radio |
NDS.Radio.validate(group, options) |
Checks the group and returns { valid, selected, message }: selected is true when an option is checked. Pass { showMessage: false } to check without a message |
NDS.Form.refresh(input) |
Updates the field after you set .checked from code. It fires no event |
NDS.Form.setStatus(group, status, { message }) |
Shows a status and a message on the group, such as an error from the server |
NDS.Form.clearStatus(group) |
Removes the group’s feedback message |
The full API is in the banner of _js/nds-radio.js.