# Radio group

> Fieldset with a legend and a list of radios, each with a label and optional description; vertical or horizontal.

- Kind: ui · Category: Forms · Interactivity: native
- Install: `npx shadcn@latest add @haze/radio-group`
- File: `src/components/haze/ui/radio-group.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/radio-group

## Use when
- Choosing exactly one of two to five options that are worth showing at once
- Options that need a short explanation under each label
- Settings such as visibility, delivery speed or export format

## Avoid when
- More than about six options; use select
- A compact segmented switch between views; use toggle-group
- Options presented as large cards; use choice-card

## Alternatives
- [select](https://hazeui.com/ui/select.md)
- [toggle-group](https://hazeui.com/ui/toggle-group.md)
- [choice-card](https://hazeui.com/ui/choice-card.md)
- [checkbox](https://hazeui.com/ui/checkbox.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| name | `string` |  | yes | Shared radio name and id prefix; each radio gets the id <name>-<value>. |
| legend | `string` |  | yes | Visible question or group title. |
| options | `{ value: string; label: string; description?: string; disabled?: boolean }[]` |  | yes | The choices; values must be unique within the group. |
| value | `string` |  | no | Value of the radio that is checked on load. |
| orientation | `"vertical" \| "horizontal"` | "vertical" | no | Stacks the options or lays them in a wrapping row. |
| required | `boolean` |  | no | One option must be picked before the form submits. |
| disabled | `boolean` |  | no | Disables every option. |
| class | `string` |  | no | Extra classes on the fieldset. |
| ...rest | `attributes` |  | no | Any other fieldset attribute, for example form or aria-describedby. |

## Accessibility
- The fieldset and legend give screen readers the group's name when focus enters the first radio.
- Radios share one name, so the browser keeps a single tab stop for the group and moves selection with the arrow keys.
- The dot scales in over 200 ms. The circle border is foreground at 40%, which meets 3:1 against the surface.
- Descriptions are tied to their own radio through aria-describedby, not to the group.
- Key Arrow Up / Arrow Down (or Left / Right): Moves to the next or previous radio and selects it
- Key Tab: Enters or leaves the group at the checked radio
- Key Space: Selects the focused radio if none is checked yet

## Agent prompts

### Install and use

Install the Haze UI component "Radio group" with `npx shadcn@latest add @haze/radio-group`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import RadioGroup from "@/components/haze/ui/radio-group.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/radio-group.md

### Customize with real content

Adapt the Haze radio-group ui for our product. Replace the demo values of these props with our real content: name, legend, options, value, orientation, required, disabled, class, ...rest. Keep the layout, the heading level rules and these accessibility details: The fieldset and legend give screen readers the group's name when focus enters the first radio. Radios share one name, so the browser keeps a single tab stop for the group and moves selection with the arrow keys. The dot scales in over 200 ms. The circle border is foreground at 40%, which meets 3:1 against the surface. Descriptions are tied to their own radio through aria-describedby, not to the group. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using radio-group, check it fits: use it when choosing exactly one of two to five options that are worth showing at once; options that need a short explanation under each label; settings such as visibility, delivery speed or export format. Avoid it when more than about six options; use select; a compact segmented switch between views; use toggle-group; options presented as large cards; use choice-card. Alternatives: @haze/select, @haze/toggle-group, @haze/choice-card, @haze/checkbox.

## Example requests that should lead here
- Add a project visibility radio group with a description under each option
- Make a horizontal radio group for choosing an editor theme

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Option {
  value: string;
  label: string;
  description?: string;
  disabled?: boolean;
}

interface Props {
  /** Shared radio name. Also the id prefix: each radio gets `<name>-<value>`. */
  name: string;
  legend: string;
  options: Option[];
  /** The value that is checked on load. */
  value?: string;
  orientation?: "vertical" | "horizontal";
  /** One option must be picked before the form submits. */
  required?: boolean;
  /** Disables every option. */
  disabled?: boolean;
  class?: string;
  /** Any other fieldset attribute, for example form or aria-describedby. */
  [key: string]: unknown;
}

const { name, legend, options, value, orientation = "vertical", required, disabled, class: className, ...rest } = Astro.props;
---

<fieldset disabled={disabled} class={cn("grid min-w-0 gap-3 border-0 p-0", className)} {...rest}>
  <legend class="mb-1 text-[0.8125rem]/5 font-medium text-foreground">{legend}</legend>
  <div class={cn("flex gap-x-6 gap-y-3", orientation === "horizontal" ? "flex-row flex-wrap" : "flex-col")}>
    {
      options.map((option) => {
        const id = `${name}-${option.value}`;
        return (
          <div class="flex items-start gap-3 has-[:disabled]:[&>span]:opacity-50 has-[:disabled]:[&_label]:opacity-50">
            <span class="relative mt-0.75 inline-grid size-4.5 shrink-0 place-items-center">
              <input
                id={id}
                name={name}
                type="radio"
                value={option.value}
                checked={option.value === value}
                disabled={option.disabled}
                required={required}
                aria-describedby={option.description ? `${id}-description` : undefined}
                class="peer col-start-1 row-start-1 size-4.5 cursor-pointer appearance-none rounded-full border border-border-strong bg-field transition-[background-color,border-color,scale] duration-150 ease-snappy hover:border-foreground active:scale-90 active:duration-75 checked:border-primary checked:hover:border-primary checked:bg-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed disabled:hover:border-border-strong disabled:active:scale-100 aria-invalid:border-destructive! forced-colors:appearance-auto"
              />
              <span class="pointer-events-none col-start-1 row-start-1 size-2 scale-0 rounded-full bg-primary-foreground transition-[scale] duration-200 ease-spring peer-checked:scale-100 forced-colors:hidden" aria-hidden="true" />
            </span>
            <div class="grid gap-1">
              <label for={id} class="text-[0.8125rem]/6 font-medium text-foreground">{option.label}</label>
              {option.description && <p id={`${id}-description`} class="text-[0.8125rem]/5 text-muted-foreground">{option.description}</p>}
            </div>
          </div>
        );
      })
    }
  </div>
</fieldset>
```
