Radio Group
Native radio buttons, one of which is checked.
Also uses Label.
radio-group-demo.tsx
import { Label } from './ui/label.tsx'
import { RadioGroup, RadioGroupItem } from './ui/radio-group.tsx'
export default () => (
<RadioGroup aria-label="Spacing">
{['Default', 'Comfortable', 'Compact'].map((option) => (
<Label>
<RadioGroupItem
name="spacing"
value={option.toLowerCase()}
checked={option === 'Comfortable'}
/>
{option}
</Label>
))}
</RadioGroup>
)
Installation
dui add radio-groupOr copy ui/radio-group.tsx and ui/radio-group.css into your project, with lib/jsx/jsx-runtime.ts, lib/jsx/jsx-dev-runtime.ts, lib/css.d.ts, and lib/theme.css.
Examples
With descriptions
Also uses Field.
radio-group-description.tsx
import {
Field,
FieldContent,
FieldDescription,
FieldLabel,
FieldLegend,
FieldSet,
} from './ui/field.tsx'
import { RadioGroup, RadioGroupItem } from './ui/radio-group.tsx'
export default () => (
<FieldSet style="max-width: 24rem">
<FieldLegend variant="label">Subscription plan</FieldLegend>
<RadioGroup>
<Field orientation="horizontal">
<RadioGroupItem id="plan-monthly" name="plan" value="monthly" checked />
<FieldContent>
<FieldLabel for="plan-monthly">Monthly ($9.99/month)</FieldLabel>
<FieldDescription>Pay by the month. Cancel any time.</FieldDescription>
</FieldContent>
</Field>
<Field orientation="horizontal">
<RadioGroupItem id="plan-yearly" name="plan" value="yearly" />
<FieldContent>
<FieldLabel for="plan-yearly">Yearly ($99.99/year)</FieldLabel>
<FieldDescription>Save 17% by paying for the year.</FieldDescription>
</FieldContent>
</Field>
</RadioGroup>
</FieldSet>
)
With Datastar
Size: M
Also uses Label.
radio-group-datastar.tsx
import { Label } from './ui/label.tsx'
import { RadioGroup, RadioGroupItem } from './ui/radio-group.tsx'
// Radios bound to one signal set it to the checked one's value.
export default () => (
<div data-signals:size="'m'" style="display: grid; gap: 1rem">
<RadioGroup aria-label="Size" style="grid-auto-flow: column">
{['s', 'm', 'l'].map((size) => (
<Label>
<RadioGroupItem name="size" value={size} data-bind:size checked={size === 'm'} />
{size.toUpperCase()}
</Label>
))}
</RadioGroup>
<p style="margin: 0">
Size: <strong data-text="$size.toUpperCase()">M</strong>
</p>
</div>
)
Compared to shadcn/ui
- Native radio buttons, with the same
name, instead of Radix's: the browser moves between them with the arrow keys, and sends the checked one with the form. - The group's
valueandonValueChangearecheckedon an item, and adata-bindon each to one signal.