radiogroup

onChange called 0 time(s)

import { radiogroup } from '@markless/ui';export default function RadiogroupBasic() @{	const plans = [		{ value: 'monthly', label: 'Monthly', note: '£8 a month' },		{ value: 'annual', label: 'Annual', note: '£80 a year' },		{ value: 'lifetime', label: 'Lifetime', note: '£240 once' },	];	<radiogroup.root class="plans" value="annual">		<radiogroup.label class="plans-label">Billing period</radiogroup.label>		@for (const plan of plans; key plan.value) {			<radiogroup.item class="plan" value={plan.value}>				<radiogroup.itemfield />				<radiogroup.itemtrigger class="dot">					<radiogroup.itemindicator class="dot-fill" />				</radiogroup.itemtrigger>				<radiogroup.itemlabel class="plan-label">					{plan.label}					<span class="plan-note">{plan.note}</span>				</radiogroup.itemlabel>			</radiogroup.item>;		}	</radiogroup.root>;}

radiogroup is a set of choices where a person picks exactly one. Use it for a short list that fits on screen, such as a billing period or a shipping speed.

import { radiogroup } from '@markless/ui';

export default function Billing() @{
	<radiogroup.root name="billing">
		<radiogroup.label>Billing period</radiogroup.label>
		<radiogroup.item value="monthly">
			<radiogroup.itemfield />
			<radiogroup.itemtrigger>
				<radiogroup.itemindicator>Chosen</radiogroup.itemindicator>
			</radiogroup.itemtrigger>
			<radiogroup.itemlabel>Monthly</radiogroup.itemlabel>
		</radiogroup.item>
		<radiogroup.item value="annual">
			<radiogroup.itemfield />
			<radiogroup.itemtrigger>
				<radiogroup.itemindicator>Chosen</radiogroup.itemindicator>
			</radiogroup.itemtrigger>
			<radiogroup.itemlabel>Annual</radiogroup.itemlabel>
		</radiogroup.item>
	</radiogroup.root>
}

Each option holds a real <input type="radio">, hidden from sight. That input is what a person focuses, and it is what a form submits.

Anatomy

radiogroup anatomy
Anatomy perspectiveFlat
Layers
Choose your rhythmWeeklyMonthly
radiogroup.root

The group: a role="radiogroup" element named by radiogroup.label through aria-labelledby.

  • radiogroup.root is a <div> with role="radiogroup". It holds the chosen value.
  • radiogroup.label is a <label>. The group points aria-labelledby at it.
  • radiogroup.description is help text. The group points aria-describedby at it.
  • radiogroup.error is an error message. While it is on the page, the group writes aria-invalid.
  • radiogroup.item is one option. Its value is what the form submits.
  • radiogroup.itemfield is the hidden radio input for that option.
  • radiogroup.itemtrigger is the shape a person clicks. It has no role of its own.
  • radiogroup.itemindicator shows its children only while its option is chosen. It is aria-hidden.
  • radiogroup.itemlabel is a <label> for the option's input. A click on it chooses the option.

radiogroup.item and radiogroup.itemtrigger write ui-selected on the chosen option. They write ui-disabled when the option or the group is disabled. The root writes ui-horizontal, ui-disabled and ui-required from its props.

Keyboard

The whole group is one Tab stop. The chosen option holds it. With nothing chosen, Tab still reaches the group.

Key What it does
Down arrow or Up arrow Moves focus to the next or previous option, and chooses it.
Right arrow or Left arrow The same, in a group with orientation="horizontal".
Home or End Chooses the first or the last option.
Space Chooses the focused option.

The arrows skip a disabled option. At the last option, the next arrow goes back to the first, unless loop is false. The arrows of the other axis do nothing. An arrow with Alt, Ctrl or Cmd held stays with the browser.

In a form

Set name once on the root. Every option's input submits under it, and only the chosen option is sent. Set required on the root when the form needs a choice.

<radiogroup.root name="plan" required>

Props on radiogroup.root

Prop Type What it does
value string The value of the chosen option. Omit it and nothing is chosen.
orientation 'horizontal' | 'vertical' Which arrows move between options. The default is 'vertical'.
loop boolean Wraps from the last option to the first. The default is true.
disabled boolean Stops every option from changing.
name string The name every option submits under.
required boolean The form needs a choice before it submits.
onChange (value) => void Gets the new value when a person picks a different option.

The root passes anything a <div> accepts to its element. radiogroup.item takes value, which is required, and disabled.