Menhir

Radio

View as Markdown

A group of radio buttons that holds one value: the user checks one, and checking another unchecks it. It renders a native <fieldset role="radiogroup">, named by its <legend>, and each radio is a native button with a hidden radio input, so the form receives the checked value under the group's name. The group is one tab stop, and the arrow keys move between the radios and check them.

Size
2.5 kB

Example

Styling
Size
RadioExample.tsx
import { Radio } from "menhir/radio";
import { For } from "solid-js";

const sizes = ["Small", "Medium", "Large"];

const radio =
  "inline-flex size-5 cursor-pointer items-center justify-center rounded-full border border-stone-400 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-checked:border-stone-800 dark:border-stone-500 dark:data-checked:border-stone-300";
const dot =
  "hidden size-2.5 rounded-full bg-stone-800 data-checked:block dark:bg-stone-300";

export default function RadioExample() {
  return (
    <Radio.Root
      name="size"
      defaultValue="Medium"
      class="inline-flex flex-col gap-2"
    >
      <legend class="mb-2 font-semibold">Size</legend>
      <For each={sizes}>
        {(size) => (
          <label class="inline-flex items-center gap-2">
            <Radio.Item value={size} class={radio}>
              <Radio.Indicator class={dot} />
            </Radio.Item>
            {size}
          </label>
        )}
      </For>
    </Radio.Root>
  );
}

Anatomy

<Radio.Root>
  <legend />
  <Radio.Item>
    <Radio.Indicator />
  </Radio.Item>
</Radio.Root>

API reference

Root

The group: a <fieldset role="radiogroup">, named by a <legend> inside it, with aria-required, data-disabled and data-required. Render it as a <div> with aria-labelledby when the group's name is outside it.

PropTypeDefaultDescription
valuestring | null—The checked radio's value, when controlled. Pair it with onValueChange. None checked is null.
defaultValuestring | nullnullThe radio checked at first, when uncontrolled. A form reset goes back to it.
onValueChange(value: string | null) => void—Called with the new value when the user checks a radio, or with defaultValue when the form is reset. A controlled group changes only if value does.
namestring—The name the checked radio's value is submitted under. The form's elements then hold the radios as a radio group: form.elements[name].value is the checked value.
disabledbooleanfalseWhether every radio ignores the user. The fieldset is disabled too, with every control inside it, and the form submits nothing.
requiredbooleanfalseWhether the form can be submitted only while a radio is checked. The browser blocks the submission and focuses the first enabled radio.
formstring—The id of the form the radios belong to, when the group is not inside it.
render(props) => JSX.Element—Renders the group as another element, such as a <div>. Read once.

Item

One radio: a <button type="button" role="radio"> with aria-checked, data-checked or data-unchecked, and data-disabled, then a visually hidden <input type="radio"> for the form. Only the checked radio, or the first enabled one while none is checked, is in the tab order.

PropTypeDefaultDescription
valuestring—Identifies the radio in the group's value, and is what the form submits while it is checked.
disabledbooleanfalseWhether this radio ignores the user. The arrow keys skip it. A disabled radio that is checked keeps the value, but the form does not submit it.
render(props) => JSX.Element—Renders the radio with your own component, which must render a <button>. Read once.

Indicator

The dot: a <span> with its radio's data-checked or data-unchecked and data-disabled, always rendered, so CSS shows it by state. Place it inside an Item.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the indicator with your own component.

Keyboard

KeysAction
TabMoves focus into the group, onto the checked radio or the first enabled one, and out of it.
SpaceChecks the focused radio.
ArrowDownMoves focus to the next enabled radio and checks it; from the last, to the first.
ArrowRightMoves focus to the next enabled radio and checks it, or the previous one right to left; wraps.
ArrowUpMoves focus to the previous enabled radio and checks it; from the first, to the last.
ArrowLeftMoves focus to the previous enabled radio and checks it, or the next one right to left; wraps.

In a Field

The group joins a Field as Field.Control's render: the fieldset gets the Field's description and error in aria-describedby, aria-invalid while the error shows, and the Field's name and disabled, which the group passes to its radios. Name the group with a <legend>, not Field.Label, which cannot name a fieldset, and put the Error inside it. With required, the browser blocks a submission while none is checked and focuses the first radio; the error shows once focus has left the group, a radio was checked or a submission was attempted. The browser's message asks to select one of the options; give the Error match="valueMissing" and your own words to say which.

import { Field } from "menhir/field";
import { Radio } from "menhir/radio";
import { For } from "solid-js";

const plans = ["Monthly", "Yearly"];

export default function RadioInField() {
  return (
    <Field.Root name="plan">
      <Field.Control render={Radio.Root} required>
        <legend>Plan</legend>
        <Field.Description>You can change it at any time.</Field.Description>
        <For each={plans}>
          {(plan) => (
            <label>
              <Radio.Item value={plan}>
                <Radio.Indicator />
              </Radio.Item>
              {plan}
            </label>
          )}
        </For>
        <Field.Error match="valueMissing">Choose a plan to go on.</Field.Error>
      </Field.Control>
    </Field.Root>
  );
}