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>;}
.plans { display: grid; gap: 0.7em; width: min(31rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.plans[ui-horizontal] { grid-template-columns: repeat(3, minmax(0, 1fr));}.plans-label { grid-column: 1 / -1; margin-block-end: 0.15em;}.plan { display: flex; gap: 0.75em; align-items: center; border: 1px solid var(--ink); border-radius: 6px; cursor: pointer;}.plan:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.plan[ui-selected] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.plan:has(:focus-visible) { outline: 2px solid var(--ink); outline-offset: 3px;}.plan[ui-disabled] { cursor: not-allowed; opacity: 0.55;}.plan[ui-disabled]:hover { background: transparent;}.dot { display: grid; flex: none; place-items: center; box-sizing: border-box; width: 1.1em; height: 1.1em; margin-inline-start: 1em; border: 1px solid var(--ink); border-radius: 999px; background: var(--paper);}.dot-fill { display: grid; place-items: center;}.plan[ui-selected] .dot-fill::before { content: ''; width: 0.55em; height: 0.55em; border-radius: 999px; background: currentColor;}.plan-label { display: flex; flex: 1; flex-wrap: wrap; gap: 0 0.75em; justify-content: space-between; padding: 0.85em 1em 0.85em 0; cursor: inherit;}.plan-note { opacity: 0.75;}.plans[ui-horizontal] .plan-label { flex-direction: column; justify-content: center;}
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
The group: a role="radiogroup" element named by radiogroup.label through aria-labelledby.
radiogroup.rootis a<div>withrole="radiogroup". It holds the chosen value.radiogroup.labelis a<label>. The group pointsaria-labelledbyat it.radiogroup.descriptionis help text. The group pointsaria-describedbyat it.radiogroup.erroris an error message. While it is on the page, the group writesaria-invalid.radiogroup.itemis one option. Itsvalueis what the form submits.radiogroup.itemfieldis the hidden radio input for that option.radiogroup.itemtriggeris the shape a person clicks. It has no role of its own.radiogroup.itemindicatorshows its children only while its option is chosen. It isaria-hidden.radiogroup.itemlabelis 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.