Radio
One of a few mutually exclusive options, with roving focus and arrow-key movement.
@luc/uiExamples
A vertical group
One choice from a few options. Arrow keys move the selection and only the selected item is in the tab order — the roving-focus contract a `div` full of radios would have to reimplement.
The default cadence.
<RadioGroup defaultValue="monthly" aria-label="Rebalance">
<Radio value="weekly" label="Weekly" />
<Radio value="monthly" label="Monthly" description="The default cadence." />
<Radio value="quarterly" label="Quarterly" />
</RadioGroup>Horizontal
The layout is the caller's: the group stays a real `radiogroup`, and a row is a flex container around it.
<RadioGroup defaultValue="1d" aria-label="Aggregation" className="flex flex-wrap gap-5">
<Radio value="1h" label="1h" />
<Radio value="1d" label="1d" />
<Radio value="1w" label="1w" />
</RadioGroup>Invalid and disabled
`invalid` sits on the item, because that is where the treatment is rendered; `disabled` on the group keeps the current choice readable while the question is closed.
<RadioGroup aria-label="Rebalance" required>
<Radio value="weekly" label="Weekly" invalid />
<Radio value="monthly" label="Monthly" invalid />
</RadioGroup>
<RadioGroup aria-label="Rebalance" disabled>…</RadioGroup>Usage
import { Radio, RadioGroup } from "@luc/ui";<RadioGroup defaultValue="monthly" aria-label="Rebalance">
<Radio value="weekly" label="Weekly" />
<Radio value="monthly" label="Monthly" />
</RadioGroup>Package setup — peer requirements, the stylesheet and the provider — is on installation.
API
Read from the published TypeScript API, so a prop that is not listed here does not exist and a default that is listed is the one the compiler sees.
| Export | Kind | Signature |
|---|---|---|
Radio | 7 documented props | ({ className, label, description, id, disabled, invalid, ...props }: RadioProps) => import("@types/react").JSX.Element |
RadioGroup | 11 documented props | import("@types/react").ForwardRefExoticComponent<RadixRadioGroup.RadioGroupProps & import("@types/react").RefAttributes<HTMLDivElement>> |
Radio props
Type: RadioProps
Values the compiler reads from the declaration.
| Prop | Type | Default | Notes |
|---|---|---|---|
asChild?inherited | boolean | undefined | — | |
checked?inherited | boolean | undefined | — | |
description? | ReactNode | — | |
invalid? | boolean | undefined | — | |
label? | ReactNode | — | |
required?inherited | boolean | undefined | — | |
valueinherited | string | — |
RadioGroup props
Type: RadioGroupProps & RefAttributes<HTMLDivElement>
Values the compiler reads from the declaration.
| Prop | Type | Default | Notes |
|---|---|---|---|
asChild?inherited | boolean | undefined | — | |
defaultValue?inherited | string | undefined | — | |
dir?inherited | Direction | undefined | — | |
disabled?inherited | boolean | undefined | — | |
form?inherited | string | undefined | — | |
loop?inherited | boolean | undefined | — | |
name?inherited | string | undefined | — | |
onValueChange?inherited | ((value: string) => void) | undefined | — | |
orientation?inherited | "horizontal" | "vertical" | undefined | — | |
required?inherited | boolean | undefined | — | |
value?inherited | string | null | undefined | — |
Guidelines
Do
- Use a radio group for one answer out of a few when the options should all stay visible; a `Select` hides them behind a click.
- Let the group own the value and the layout be yours: the component is the Radix root, so a row is a flex container around it.
- Give the item its `description` when the options need a sentence each, and `invalid` when the form rejected the answer.
Don't
- Use a radio group for an on/off setting — that is a `Switch`, and for a submitted yes/no it is a `Checkbox`.
- Build the group out of a `div` and separate radios: the roving focus, the arrow keys and the single tab stop are the contract you would have to reimplement.
- Set `invalid` on the group: the treatment lives on the item, which is what the compiler's prop table says.
Accessibility
The group is a real `radiogroup` with roving focus: one tab stop for the whole set, and the arrow keys move the selection. `disabled` on the group keeps the current answer visible while closing the question, and an item's `description` is wired to that item rather than to the group.
Family
Forms
Inputs, their validation surfaces and the form lifecycle around them.
24 documented entries in this family. Open the family index
Related