Radio group
Fieldset with a legend and a list of radios, each with a label and optional description; vertical or horizontal.
pnpm dlx shadcn@latest add @haze/radio-group---
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>When to use
- 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
When not to use
- More than about six options; use select
- A compact segmented switch between views; use toggle-group
- Options presented as large cards; use choice-card
Installation
Add the Haze registry to components.json once, then install Radio group with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/radio-groupnpx shadcn@latest add @haze/radio-groupyarn dlx shadcn@latest add @haze/radio-groupbunx --bun shadcn@latest add @haze/radio-groupFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| name* | string | – | Shared radio name and id prefix; each radio gets the id <name>-<value>. |
| legend* | string | – | Visible question or group title. |
| options* | { value: string; label: string; description?: string; disabled?: boolean }[] | – | The choices; values must be unique within the group. |
| value | string | – | Value of the radio that is checked on load. |
| orientation | "vertical" | "horizontal" | "vertical" | Stacks the options or lays them in a wrapping row. |
| required | boolean | – | One option must be picked before the form submits. |
| disabled | boolean | – | Disables every option. |
| class | string | – | Extra classes on the fieldset. |
| ...rest | attributes | – | Any other fieldset attribute, for example form or aria-describedby. |
Keyboard interactions
- Arrow UpArrow Down (or LeftRight)
- Moves to the next or previous radio and selects it
- Tab
- Enters or leaves the group at the checked radio
- Space
- Selects the focused radio if none is checked yet
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.