A radio group (WAI-ARIA Radio Group pattern, native radio variant): a <fieldset> of real <input type="radio"> elements. The platform supplies the single Tab stop, arrow keys (wrapping, and swapped under direction: rtl, except in Safari), forms and validation. The library adds one thing: the checked value is changed through valueChangeAction in a transition, with optimistic feedback and data-pending.
"use client";
import { RadioGroup } from "@funstack/ui/radio-group";
import { fakeSave } from "./fake-api.js";
const themes = [
{ value: "light", label: "Light" },
{ value: "dark", label: "Dark" },
{ value: "system", label: "System" },
];
// Uncontrolled with an async save. The clicked radio checks at once (its DOM `checked` is the
// optimistic value); the Root and that radio carry data-pending for the 800 ms the save takes.
// The legend reads `pending` through the Label's function children; the row spinner is CSS on
// `:has([data-pending])`. Arrow keys are native: each press checks and saves, the latest wins.
export function RadioGroupTheme() {
return (
<RadioGroup.Root
className="demo-radio-group"
defaultValue="system"
valueChangeAction={(next) => fakeSave(`theme=${next}`)}
>
<RadioGroup.Label>
{({ pending }) => (
<>Theme {pending && <span className="demo-radio-saving">saving…</span>}</>
)}
</RadioGroup.Label>
{themes.map((theme) => (
<label key={theme.value} className="demo-field">
<RadioGroup.Item value={theme.value} className="demo-radio" />
{theme.label}
<span className="demo-spinner demo-radio-spinner" aria-hidden />
</label>
))}
</RadioGroup.Root>
);
}/* RadioGroup: the Root is a <fieldset>, the Label its <legend>, each Item the visible radio
(appearance: none + ::before). `:checked` and `[data-checked]` agree: the DOM `checked` is the
optimistic value. */
.demo-radio-group {
display: grid;
gap: 0.4rem;
justify-items: start;
margin: 0;
padding: 0.5rem 1rem 0.75rem;
border: 1px solid var(--color-border);
border-radius: 8px;
}
.demo-radio-group legend {
padding-inline: 0.3rem;
font-weight: 600;
color: var(--color-text);
}
.demo-radio-group[data-pending] {
cursor: progress;
}
.demo-radio-inline {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
}
.demo-radio {
appearance: none;
flex: none;
margin: 0;
inline-size: 1.25em;
block-size: 1.25em;
border: 2px solid var(--color-text-secondary);
border-radius: 50%;
display: inline-grid;
place-content: center;
cursor: inherit;
transition: border-color 120ms;
}
.demo-radio::before {
content: "";
inline-size: 0.6em;
block-size: 0.6em;
border-radius: 50%;
background: var(--color-primary);
scale: 0;
transition: scale 120ms;
}
.demo-radio:checked {
border-color: var(--color-primary);
}
/* Optimistic: flips on click, reverts on rejection or veto. */
.demo-radio:checked::before {
scale: 1;
}
.demo-radio:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-radio:user-invalid {
border-color: #dc2626;
}
.demo-field:has(.demo-radio:disabled) {
opacity: 0.5;
cursor: not-allowed;
}
/* The row spinner shows only next to the radio the user is waiting on. */
.demo-radio-spinner {
display: none;
}
.demo-field:has(.demo-radio[data-pending]) .demo-radio-spinner {
display: inline-block;
}
.demo-radio-saving {
font-weight: normal;
font-size: 0.85em;
color: var(--color-text-secondary);
}
@media (prefers-reduced-motion: reduce) {
.demo-radio,
.demo-radio::before {
transition: none;
}
}import { RadioGroup } from "@funstack/ui/radio-group";
<RadioGroup.Root name="plan" defaultValue="free" valueChangeAction={savePlan}> {/* fieldset */}
<RadioGroup.Label>Plan</RadioGroup.Label> {/* legend: optional, first child */}
<label>
<RadioGroup.Item value="free" /> Free {/* input type=radio */}
</label>
<label>
<RadioGroup.Item value="pro" /> Pro
</label>
</RadioGroup.Root><fieldset role="radiogroup">: the legend names it, disabled reaches every radio natively, and it finds the form for resets. A render element must stay a <fieldset>.<legend> and must be the Root's first child. Its function children receive the Root's state, so a group-level pending indicator goes there.appearance: none and ::before, or use accent-color. Each Item's text label is your own <label>.RadioGroup.Root| Prop | Type | Default | Description |
|---|---|---|---|
value | V | null | – | Controlled checked value; null checks nothing. undefined means uncontrolled. V is the item value type: a declared union you pass is kept, a literal is string (Typed values). |
defaultValue | V | null | null | Initial value in uncontrolled mode. Also what a form reset returns to: the latest value is mirrored into the radios' defaultChecked. An unchecked group is the HTML default, so leaving both out is fine. |
valueChangeAction | ChangeAction<V | null, RadioGroupValueChangeDetails> | – | Runs in the Root's transition whenever the checked radio changes; sync, async or a Server Function. Rejections reach the nearest Error Boundary and roll the selection back. next is a string for every user interaction; it is null only with reason: "platform", when a form reset empties the group. Every arrow key press dispatches; the latest wins. details.valid: the control would be valid with the requested value (native constraints and the Field's validate); information only, the change is dispatched anyway. |
name | string | useId() | The name shared by every radio: the form data key. Pass a real name inside a form you submit. Must be unique within the form. |
required | boolean | false | Marks every radio required, which makes the group required; one checked radio satisfies it. |
disabled | boolean | false | Renders <fieldset disabled>: every radio is disabled, unfocusable and skipped by the arrow keys. |
readOnly | boolean | false | The checked radio stays checked, or the enclosing Field.Root's: every radio stays focusable and the group is submitted, but no click, Space, label or arrow checks another radio (each click is cancelled, so no input or change fires) and nothing dispatches. The arrows still move focus, and the checked radio stays the Tab stop. Renders aria-readonly="true" on the radiogroup (the radios have no such state) and data-readonly on every part; native required is not rendered. A radio checked before hydration is unchecked again at mount. |
form | string | – | The form attribute, applied to the fieldset and to every radio. |
className | string | ((state: RadioGroupRootState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: RadioGroupRootState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: RadioGroupRootState) => ReactNode) | – | The Label and Items. The function form receives the optimistic value; must not suspend. |
render | ReactElement | ((props, state: RadioGroupRootState) => ReactNode) | – | Replace the default <fieldset> (element form) or take over rendering (function form). |
RadioGroupRootState is { value, pending, disabled, invalid, required, readOnly }; value is optimistic; invalid is true while the enclosing Field.Root shows an error (data-invalid), false outside a Field; required is the prop or the Field's.role ("radiogroup"), aria-required, aria-readonly (while read-only), disabled, form, data-pending, data-disabled, data-readonly. Inside a Field.Root also aria-labelledby, aria-describedby, aria-invalid and data-invalid.Other fieldset props pass through (aria-label for a group without a Label, for example). In development a group with no accessible name (no Label, Field.Label, aria-label or aria-labelledby) logs a warning. A disabled group (its own prop or the Field's) is not submitted.
RadioGroup.Label| Prop | Type | Default | Description |
|---|---|---|---|
className | string | ((state: RadioGroupLabelState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: RadioGroupLabelState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: RadioGroupLabelState) => ReactNode) | – | Content, or a function of the Root's state. |
render | ReactElement | ((props, state: RadioGroupLabelState) => ReactNode) | – | Replace the default <legend> (element form) or take over rendering (function form). |
RadioGroupLabelState, an alias of RadioGroupRootState.RadioGroup.Item| Prop | Type | Default | Description |
|---|---|---|---|
value | V | – | Required. The value this radio checks, unique within the Root; submitted as the form value. V is string unless you pass a type argument, which checks the value (Typed values). |
disabled | boolean | false | Native disabled: unfocusable, skipped by the arrow keys. |
className | string | ((state: RadioGroupItemState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: RadioGroupItemState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: RadioGroupItemState) => ReactNode) | – | Replace the default <input> (element form) or take over rendering (function form). It must keep an <input>; the library sets type="radio". |
RadioGroupItemState is { value, checked, pending, disabled, readOnly }; checked is optimistic, pending is true on the radio the user is waiting on, and disabled includes Root disabled.type, name, value, checked, defaultChecked (never rendered), disabled, form, required (not rendered while read-only), readOnly (removed), data-checked, data-pending, data-disabled, data-readonly; onChange is merged (a render element's own handler runs first).id and other input props pass through. readOnly is not accepted (a type error): read-only is the Root's, and one choice that cannot be picked is disabled. There is no transition prop: animate the check with CSS on :checked.
| Attribute | On | Pseudo-class | Meaning |
|---|---|---|---|
data-pending | Root | – | valueChangeAction is in flight. |
data-pending | Item | – | The radio the in-flight dispatch is checking (the optimistic one). |
data-checked | Item | :checked | Optimistic checked state. Identical, because the DOM holds the optimistic value. |
data-disabled | Root, Item | :disabled | Disabled by prop; on an Item, by its own prop or Root disabled. |
data-readonly | Root, Item | – | The Root's readOnly (or the Field's). :read-only matches every radio, so it tells nothing. |
| – | Item | :required, :user-invalid, :focus-visible | Platform only; no attribute. |
| – | Item | :indeterminate | Every radio of a group with nothing checked (platform). |
.radio:checked::before { scale: 1; } /* flips on click, reverts on rejection */
.radio[data-pending] { animation: pulse 1s infinite alternate; }
.option:has(.radio:checked) { font-weight: 600; }
.group[data-pending] { cursor: progress; }Everything here is the browser's; the library installs no key handler.
| Key | Behaviour | Dispatches |
|---|---|---|
| Tab / Shift+Tab | Enters the group on the checked radio (the first, with nothing checked) and leaves it on the next press. | no |
| Space | Checks the focused radio. | yes |
| ArrowDown / ArrowRight | Focuses and checks the next enabled radio, wrapping (not in Safari). Swapped under direction: rtl (not in Safari). | yes, once per press |
| ArrowUp / ArrowLeft | Focuses and checks the previous enabled radio, wrapping (not in Safari). Swapped under direction: rtl (not in Safari). | yes, once per press |
| Enter | Submits the form (implicit submission). | no |
| Read-only | Space, a click and a label click check nothing; the arrows move focus and check nothing. The checked radio stays the Tab stop. | no |
orientation prop: native radios honour all four arrows whatever the layout.aria-live region if a slow action may fail.aria-checked (forbidden on native inputs) and no tabindex (the browser does the roving)."use client";
import { useState } from "react";
import { RadioGroup } from "@funstack/ui/radio-group";
const alignments = [
{ value: "right", label: "يمين (right)" },
{ value: "center", label: "وسط (center)" },
{ value: "left", label: "يسار (left)" },
];
// Inside dir="rtl" the browser mirrors the arrow keys: ArrowLeft moves to the next radio, which
// is visually to the left (Safari does not mirror). The library has no keyboard code of its own.
// Controlled with local state: the setState runs in the action's synchronous part, so it is a
// transition update.
export function RadioGroupRtl() {
const [value, setValue] = useState<string | null>("right");
return (
<>
<div dir="rtl">
<RadioGroup.Root
className="demo-radio-group demo-radio-inline"
value={value}
valueChangeAction={(next) => {
setValue(next);
}}
>
<RadioGroup.Label>المحاذاة (alignment)</RadioGroup.Label>
{alignments.map((option) => (
<label key={option.value} className="demo-field">
<RadioGroup.Item value={option.value} className="demo-radio" />
{option.label}
</label>
))}
</RadioGroup.Root>
</div>
<output className="demo-readout">
value: <code>{JSON.stringify(value)}</code>
</output>
</>
);
}/* RadioGroup: the Root is a <fieldset>, the Label its <legend>, each Item the visible radio
(appearance: none + ::before). `:checked` and `[data-checked]` agree: the DOM `checked` is the
optimistic value. */
.demo-radio-group {
display: grid;
gap: 0.4rem;
justify-items: start;
margin: 0;
padding: 0.5rem 1rem 0.75rem;
border: 1px solid var(--color-border);
border-radius: 8px;
}
.demo-radio-group legend {
padding-inline: 0.3rem;
font-weight: 600;
color: var(--color-text);
}
.demo-radio-group[data-pending] {
cursor: progress;
}
.demo-radio-inline {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
}
.demo-radio {
appearance: none;
flex: none;
margin: 0;
inline-size: 1.25em;
block-size: 1.25em;
border: 2px solid var(--color-text-secondary);
border-radius: 50%;
display: inline-grid;
place-content: center;
cursor: inherit;
transition: border-color 120ms;
}
.demo-radio::before {
content: "";
inline-size: 0.6em;
block-size: 0.6em;
border-radius: 50%;
background: var(--color-primary);
scale: 0;
transition: scale 120ms;
}
.demo-radio:checked {
border-color: var(--color-primary);
}
/* Optimistic: flips on click, reverts on rejection or veto. */
.demo-radio:checked::before {
scale: 1;
}
.demo-radio:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-radio:user-invalid {
border-color: #dc2626;
}
.demo-field:has(.demo-radio:disabled) {
opacity: 0.5;
cursor: not-allowed;
}
/* The row spinner shows only next to the radio the user is waiting on. */
.demo-radio-spinner {
display: none;
}
.demo-field:has(.demo-radio[data-pending]) .demo-radio-spinner {
display: inline-block;
}
.demo-radio-saving {
font-weight: normal;
font-size: 0.85em;
color: var(--color-text-secondary);
}
@media (prefers-reduced-motion: reduce) {
.demo-radio,
.demo-radio::before {
transition: none;
}
}Concept: Transitions & ViewTransition
fsui-radio-group (no kind: every change checks one radio). No part has a transition prop: a consumer's own <ViewTransition> around dependent content keys a type map on it.
Concept: Pending & optimistic state
The checked radio is the optimistic value, rendered into the native inputs. data-pending is on the Root while valueChangeAction is in flight, and on the Item being checked. Animate the check with CSS on :checked; your own <ViewTransition> around dependent content animates the commit that settles an action or rolls it back.
valueChangeAction("dark", { reason: "activate" }) in the Root's transition. The Root and the Dark radio get data-pending.checked back to the old radio, and the error reaches the nearest Error Boundary.(next) => { void save(next); }), or leave the value to the form submit.The radios show the optimistic value because react-dom restores controlled inputs after every event (Native inputs), so details.event.preventDefault() is not a veto; a controlled parent vetoes by not adopting the value.
Concept: Suspense
No content region; nothing the RadioGroup renders suspends.
Concept: Browser-owned state
Native radios: the browser checks one before any JavaScript runs. A form reset and clicks before hydration are dispatched with reason: "platform", once per group.
Concept: Server Components
Native radios: clicking, Space, arrow keys between radios, labels, required and form submission work. On hydration a changed value is reconciled with one valueChangeAction call (reason: "platform").
If a group only posts with its form, a Server Component can render the same native markup itself, with no library code for it:
// A Server Component: no import from @funstack/ui.
<fieldset role="radiogroup" aria-required="true">
<legend>Size</legend>
<label>
<input type="radio" name="size" value="s" defaultChecked={order.size === "s"} required /> Small
</label>
<label>
<input type="radio" name="size" value="m" defaultChecked={order.size === "m"} required /> Medium
</label>
</fieldset>The browser still provides the single Tab stop, the arrow keys, required and submission. For a required group, do what the Root does: put required on every radio and aria-required="true" on the fieldset, so the radiogroup itself is announced as required. Compared with RadioGroup, every radio needs the shared name written out, and there is no valueChangeAction, so no transition, data-pending or rollback on rejection; no data-checked / data-disabled (use :checked and :disabled); no React state that tracks it (clicks before hydration and form resets live only in the DOM); and no Field integration.
"use client";
import { startTransition, useState } from "react";
import { useFormStatus } from "react-dom";
import { RadioGroup } from "@funstack/ui/radio-group";
import type { RadioGroupValueChangeDetails } from "@funstack/ui/radio-group";
import { fakeSave } from "./fake-api.js";
const sizes = [
{ value: "s", label: "Small" },
{ value: "m", label: "Medium" },
{ value: "l", label: "Large" },
{ value: "xl", label: "Extra large (sold out)" },
];
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" className="demo-button demo-primary">
{pending && <span className="demo-spinner" aria-hidden />}
Order
</button>
);
}
// The radios post `size=…` with the form (a real `name`), and `required` blocks the submit until
// one is checked. After the form action succeeds React resets the form; the ordered size is the
// new `defaultValue`, so the reset returns to it. The Reset button returns to the latest default
// too. `valueChangeAction` here only records what the group was told: user changes arrive with
// reason "activate", resets with reason "platform" (`null` when the reset empties the group).
export function RadioGroupForm() {
const [ordered, setOrdered] = useState<string | null>(null);
const [log, setLog] = useState<string[]>([]);
const record = (next: string | null, { reason, event }: RadioGroupValueChangeDetails) => {
const entry = `${JSON.stringify(next)} (${reason}, ${event?.type ?? "no event"})`;
setLog((prev) => [entry, ...prev].slice(0, 4));
};
return (
<form
className="demo-stack"
action={async (formData) => {
await fakeSave("the order");
const size = formData.get("size");
startTransition(() => setOrdered(typeof size === "string" ? size : null));
}}
>
<RadioGroup.Root
className="demo-radio-group"
name="size"
required
defaultValue={ordered}
valueChangeAction={record}
>
<RadioGroup.Label>T-shirt size</RadioGroup.Label>
{sizes.map((size) => (
<label key={size.value} className="demo-field">
<RadioGroup.Item
value={size.value}
className="demo-radio"
disabled={size.value === "xl"}
/>
{size.label}
</label>
))}
</RadioGroup.Root>
<div className="demo-row">
<SubmitButton />
<button type="reset" className="demo-button">
Reset
</button>
</div>
<output className="demo-readout">
ordered: <code>{JSON.stringify(ordered)}</code>
<br />
valueChangeAction calls, latest first: <code>{log.join(" · ") || "(none yet)"}</code>
</output>
</form>
);
}/* RadioGroup: the Root is a <fieldset>, the Label its <legend>, each Item the visible radio
(appearance: none + ::before). `:checked` and `[data-checked]` agree: the DOM `checked` is the
optimistic value. */
.demo-radio-group {
display: grid;
gap: 0.4rem;
justify-items: start;
margin: 0;
padding: 0.5rem 1rem 0.75rem;
border: 1px solid var(--color-border);
border-radius: 8px;
}
.demo-radio-group legend {
padding-inline: 0.3rem;
font-weight: 600;
color: var(--color-text);
}
.demo-radio-group[data-pending] {
cursor: progress;
}
.demo-radio-inline {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1.5rem;
}
.demo-radio {
appearance: none;
flex: none;
margin: 0;
inline-size: 1.25em;
block-size: 1.25em;
border: 2px solid var(--color-text-secondary);
border-radius: 50%;
display: inline-grid;
place-content: center;
cursor: inherit;
transition: border-color 120ms;
}
.demo-radio::before {
content: "";
inline-size: 0.6em;
block-size: 0.6em;
border-radius: 50%;
background: var(--color-primary);
scale: 0;
transition: scale 120ms;
}
.demo-radio:checked {
border-color: var(--color-primary);
}
/* Optimistic: flips on click, reverts on rejection or veto. */
.demo-radio:checked::before {
scale: 1;
}
.demo-radio:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-radio:user-invalid {
border-color: #dc2626;
}
.demo-field:has(.demo-radio:disabled) {
opacity: 0.5;
cursor: not-allowed;
}
/* The row spinner shows only next to the radio the user is waiting on. */
.demo-radio-spinner {
display: none;
}
.demo-field:has(.demo-radio[data-pending]) .demo-radio-spinner {
display: inline-block;
}
.demo-radio-saving {
font-weight: normal;
font-size: 0.85em;
color: var(--color-text-secondary);
}
@media (prefers-reduced-motion: reduce) {
.demo-radio,
.demo-radio::before {
transition: none;
}
}name the group posts the checked value; required blocks the submit until a radio is checked and every radio matches :user-invalid after the attempt.form.reset(), or React's automatic reset after the action succeeds) is one dispatch for the whole group, with reason: "platform" and the reset event. Uncontrolled, the target is the latest defaultValue (here the ordered size). Controlled, the mount-time value is offered to the parent, which adopts it or not. A disabled group is reset too, since the form's author reset it.// Autosave: a per-group action with data-pending, passed directly from a Server Component.
<RadioGroup.Root defaultValue={user.plan} valueChangeAction={setPlan}>...</RadioGroup.Root>
// Native submission: no action on the group; pending is the form's.
<form action={placeOrder}>
<RadioGroup.Root name="size" required>...</RadioGroup.Root>
<SubmitButton />
</form>The two channels are independent and may be combined. An action-less group is a working native radio group before hydration and with JavaScript off.