# RadioGroup

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

## Anatomy

```tsx
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>
```

- **Root is a `<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>`.
- **Label is the `<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.
- **Item is the radio itself**, a void element. There is no Indicator part: style the input with `appearance: none` and `::before`, or use `accent-color`. Each Item's text label is your own `<label>`.

```tsx title="RadioGroupTheme.tsx"
"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>
  );
}
```

```css title="radio-group.css"
/* 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;
  }
}
```

## Props

### `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](../concepts/action-props.md#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`](field.md#details-valid)); 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). |

- **State**: `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.
- **Owned**: `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). |

- **State**: `RadioGroupLabelState`, an alias of `RadioGroupRootState`.
- **Owned**: none.

### `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](../concepts/action-props.md#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"`. |

- **State**: `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`.
- **Owned**: `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`.

## Data attributes

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

```css
.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; }
```

## Keyboard and ARIA

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 |

- No Home/End (not in the APG Radio Group pattern) and no `orientation` prop: native radios honour all four arrows whatever the layout.
- Safari (WebKit) neither wraps at the ends of the group nor mirrors ArrowLeft / ArrowRight under RTL: Right and Down always go to the next radio in document order, Left and Up to the previous one, and a press past the end does nothing (and dispatches nothing). This is the platform's behaviour; the library deliberately adds no key handler to change it (see [Direction and locales](../concepts/direction-and-locales.md)).
- The checked state assistive technology reads is the optimistic one, so a new choice is announced at once. A rollback is silent; add an `aria-live` region if a slow action may fail.
- The library never moves focus, not even on rollback: focus stays on the radio the user reached, and the checked radio is the group's Tab stop again.
- No `aria-checked` (forbidden on native inputs) and no `tabindex` (the browser does the roving).

```tsx title="RadioGroupRtl.tsx"
"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>
    </>
  );
}
```

`radio-group.css`: the same file as above.

## Transition types

Concept: [Transitions & ViewTransition](../concepts/transitions.md#transition-types)

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

## Optimistic and pending

Concept: [Pending & optimistic state](../concepts/pending-and-optimistic.md)

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.

### Walkthrough

1. Click Dark (or arrow to it): the browser checks it and unchecks the old radio; the group dispatches `valueChangeAction("dark", { reason: "activate" })` in the Root's transition. The Root and the Dark radio get `data-pending`.
2. Resolve: the value is committed (uncontrolled) and the attributes go.
3. Reject: the optimistic value reverts, React writes `checked` back to the old radio, and the error reaches the nearest Error Boundary.
4. Arrow keys pressed quickly dispatch once per press; only the latest dispatch commits. For a preference that needs no wait, do not return the promise (`(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](../concepts/browser-owned-state.md#native-inputs)), so `details.event.preventDefault()` is not a veto; a controlled parent vetoes by not adopting the value.

## Suspense

Concept: [Suspense](../concepts/suspense.md)

No content region; nothing the RadioGroup renders suspends.

## Browser-owned state

Concept: [Browser-owned state](../concepts/browser-owned-state.md)

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.

## Before hydration

Concept: [Server Components](../concepts/server-components.md#before-hydration)

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"`).

### Plain HTML

If a group only posts with its form, a Server Component can render the same native markup itself, with no library code for it:

```tsx
// 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](field.md) integration.

## Forms

```tsx title="RadioGroupForm.tsx"
"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>
  );
}
```

`radio-group.css`: the same file as above.

- With a real `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** (the Reset button, `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.
- **Clicks before hydration** (and Firefox restoring form state on reload) work natively; on hydration the Root finds the checked radio differs from its state and dispatches once, with no event. Items that hydrate later, inside a Suspense boundary, trigger one more check.

### Server Functions and forms

```tsx
// 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](../concepts/action-props.md#form-channel) are independent and may be combined. An action-less group is a working native radio group before hydration and with JavaScript off.
