# Radio

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.

- Entry point: `menhir/radio`
- Size: 2.5 kB, minified and gzipped, Solid left out

> To add Radio to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

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

## Example

### Tailwind CSS

`RadioExample.tsx`:

```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>
  );
}
```

### CSS

`RadioExample.tsx`:

```tsx
import { Radio } from "menhir/radio";
import { For } from "solid-js";

import "./radio.css";

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

export default function RadioExample() {
  return (
    <Radio.Root name="size" defaultValue="Medium" class="radio-group">
      <legend>Size</legend>
      <For each={sizes}>
        {(size) => (
          <label>
            <Radio.Item value={size} class="radio">
              <Radio.Indicator class="indicator" />
            </Radio.Item>
            {size}
          </label>
        )}
      </For>
    </Radio.Root>
  );
}
```

`radio.css`:

```css
/* The least CSS that makes the Radio visible: the fieldset's own border and
   padding taken off, and each radio drawn as a circle with a dot shown by
   data-checked. Colours are Tailwind's stone palette; light-dark() picks the
   second one in dark mode (color-scheme: dark). */
.radio-group {
  display: inline-flex;
  flex-direction: column;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  border: 0;
  font-family: system-ui, sans-serif;
}
.radio-group legend {
  margin-bottom: 0.5rem;
  padding: 0;
  font-weight: 600;
}
.radio-group label {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.radio {
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  padding: 0;
  border: 1px solid
    light-dark(
      oklch(70.9% 0.01 56.259) /* stone-400 */,
      oklch(55.3% 0.013 58.071) /* stone-500 */
    );
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}
.radio[data-checked] {
  border-color: light-dark(
    oklch(26.8% 0.007 34.298) /* stone-800 */,
    oklch(86.9% 0.005 56.366) /* stone-300 */
  );
}

.indicator {
  display: none;
  width: 0.625rem;
  height: 0.625rem;
  border-radius: 50%;
  background: light-dark(
    oklch(26.8% 0.007 34.298) /* stone-800 */,
    oklch(86.9% 0.005 56.366) /* stone-300 */
  );
}
.indicator[data-checked] {
  display: block;
}
```

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

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string \| null` | — | The checked radio's value, when controlled. Pair it with onValueChange. None checked is null. |
| `defaultValue` | `string \| null` | `null` | The 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. |
| `name` | `string` | — | 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. |
| `disabled` | `boolean` | `false` | Whether every radio ignores the user. The fieldset is disabled too, with every control inside it, and the form submits nothing. |
| `required` | `boolean` | `false` | Whether the form can be submitted only while a radio is checked. The browser blocks the submission and focuses the first enabled radio. |
| `form` | `string` | — | 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | Identifies the radio in the group's value, and is what the form submits while it is checked. |
| `disabled` | `boolean` | `false` | Whether 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the indicator with your own component. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Tab` | Moves focus into the group, onto the checked radio or the first enabled one, and out of it. |
| `Space` | Checks the focused radio. |
| `ArrowDown` | Moves focus to the next enabled radio and checks it; from the last, to the first. |
| `ArrowRight` | Moves focus to the next enabled radio and checks it, or the previous one right to left; wraps. |
| `ArrowUp` | Moves focus to the previous enabled radio and checks it; from the first, to the last. |
| `ArrowLeft` | Moves 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.

`RadioInField.tsx`:

```tsx
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>
  );
}
```
