Native Solid 2 fork. Original documentation and designs © WorkOS; historical posts retain their original context.

Radio Group

A set of checkable buttons—known as radio buttons—where no more than one of the buttons can be checked at a time.

Download runnable sandbox
import { RadioGroup } from "@radix-solid/primitives";
import styles from "./styles.module.css";
const RadioGroupDemo = () => <form>
<RadioGroup.Root class={styles.Root} defaultValue="default" aria-label="View density">
<div style={{ "display": "flex", "align-items": "center" }}>
<RadioGroup.Item class={styles.Item} value="default" id="r1">
<RadioGroup.Indicator class={styles.Indicator} />
</RadioGroup.Item>
<label class={styles.Label} for="r1">
Default
</label>
</div>
<div style={{ "display": "flex", "align-items": "center" }}>
<RadioGroup.Item class={styles.Item} value="comfortable" id="r2">
<RadioGroup.Indicator class={styles.Indicator} />
</RadioGroup.Item>
<label class={styles.Label} for="r2">
Comfortable
</label>
</div>
<div style={{ "display": "flex", "align-items": "center" }}>
<RadioGroup.Item class={styles.Item} value="compact" id="r3">
<RadioGroup.Indicator class={styles.Indicator} />
</RadioGroup.Item>
<label class={styles.Label} for="r3">
Compact
</label>
</div>
</RadioGroup.Root>
</form>;
export default RadioGroupDemo;

Features

  • Full keyboard navigation.

  • Supports horizontal/vertical orientation.

  • Can be controlled or uncontrolled.

Anatomy

Import all parts and piece them together.

import { RadioGroup } from "@radix-solid/primitives";
export default () => <RadioGroup.Root>
<RadioGroup.Item value="item">
<RadioGroup.Indicator />
</RadioGroup.Item>
</RadioGroup.Root>;

API Reference

Root

Contains all the parts of a radio group.

PropTypeDefault
asChildbooleanfalse
defaultValuestring—
valuestring—
onValueChangefunction—
disabledboolean—
namestring—
requiredboolean—
orientationenumundefined
direnum—
loopbooleantrue
Data attributeValues
[data-disabled]

Present when disabled

Item

An item in the group that can be checked. An input will also render when used within a form to ensure events propagate correctly.

PropTypeDefault
asChildbooleanfalse
valuestring—
disabledboolean—
requiredboolean—
Data attributeValues
[data-state]"checked" | "unchecked"
[data-disabled]

Present when disabled

Indicator

Renders when the radio item is in a checked state. You can style this element directly, or you can use it as a wrapper to put an icon into, or both.

PropTypeDefault
asChildbooleanfalse
forceMountboolean—
Data attributeValues
[data-state]"checked" | "unchecked"
[data-disabled]

Present when disabled

Examples

Decoupling the hidden input

By default, RadioGroup.Item renders a visually hidden input for form submission. To recompose, move, or exclude that input, you can build each item from its lower-level parts instead.

Important: These parts are unstable and prefixed with unstable_, so their API may change in a future release.

  • RadioGroup.unstable_ItemProvider provides the item state and accepts the item's value and disabled props.
  • RadioGroup.unstable_ItemTrigger is the interactive element that wraps RadioGroup.Indicator.
  • RadioGroup.unstable_ItemBubbleInput is the visually hidden input that RadioGroup.Item renders by default. Omit it when you don't need form submission.
import { RadioGroup } from "@radix-solid/primitives";
export default () => <RadioGroup.Root>
{["one", "two", "three"].map(value => <RadioGroup.unstable_ItemProvider value={value}>
<RadioGroup.unstable_ItemTrigger>
<RadioGroup.Indicator />
</RadioGroup.unstable_ItemTrigger>
<RadioGroup.unstable_ItemBubbleInput />
</RadioGroup.unstable_ItemProvider>)}
</RadioGroup.Root>;

Accessibility

Adheres to the Radio Group WAI-ARIA design pattern and uses roving tabindex to manage focus movement among radio items.

Keyboard Interactions

KeyDescription
Tab
Moves focus to either the checked radio item or the first radio item in the group.
Space
When focus is on an unchecked radio item, checks it.
ArrowDown
Moves focus and checks the next radio item in the group.
ArrowRight
Moves focus and checks the next radio item in the group.
ArrowUp
Moves focus to the previous radio item in the group.
ArrowLeft
Moves focus to the previous radio item in the group.