Radio Group
A set of checkable buttons where only one can be selected at a time.
Installation
$
npx shadcn@latest add https://ui.mattdowney.com/r/radio-group.jsonUsage
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"Examples
Default
One choice from a short list. Arrow keys move between the options, so the whole group is a single tab stop rather than one per item.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultValueRadioGroup | string | None | The value of the item that is checked initially, for uncontrolled usage. |
| valueRadioGroup, RadioGroupItem | string | None | On <RadioGroup />, the controlled checked value (use with onValueChange). On <RadioGroupItem />, the value this option submits when checked (required). |
| onValueChangeRadioGroup | (value: string) => void | None | Called with the newly checked item's value. |
| nameRadioGroup | string | None | The name submitted with form data. |
| disabledRadioGroup, RadioGroupItem | boolean | false | Disables every item, or a single option on <RadioGroupItem />. |
| requiredRadioGroup | boolean | false | Requires a selection before the owning form can be submitted. |
| defaultValueRadioGroupstringThe value of the item that is checked initially, for uncontrolled usage. |
valueRadioGroup, RadioGroupItemstringOn <RadioGroup />, the controlled checked value (use with onValueChange). On <RadioGroupItem />, the value this option submits when checked (required). |
| onValueChangeRadioGroup(value: string) => voidCalled with the newly checked item's value. |
| nameRadioGroupstringThe name submitted with form data. |
disabledRadioGroup, RadioGroupItemboolean = falseDisables every item, or a single option on <RadioGroupItem />. |
| requiredRadioGroupboolean = falseRequires a selection before the owning form can be submitted. |
"use client"
import * as React from "react"
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group"
import { Radio as RadioPrimitive } from "@base-ui/react/radio"
import { cn } from "@/registry/lib/utils"
// @use-when a form field choosing exactly one of a few options, all shown at
// once, submitted with the form: a shipping speed, a product size picker (S to
// 2XL). Not Select, which hides them in a list; not
// SegmentedControl, which changes a value in place.
function RadioGroup({
className,
...props
}: React.ComponentProps<typeof RadioGroupPrimitive>) {
return (
<RadioGroupPrimitive
data-slot="radio-group"
className={cn("grid gap-3", className)}
{...props}
/>
)
}
function RadioGroupItem({
className,
...props
}: React.ComponentProps<typeof RadioPrimitive.Root>) {
return (
<RadioPrimitive.Root
// `dark:data-[checked]:bg-accent` restates the checked fill on purpose:
// `dark:bg-neutral-800` ties it on specificity and sorts after it. Hover
// darkens the border one step on an unchecked, enabled, valid radio.
data-slot="radio-group-item"
className={cn(
"peer group/radio tap-target border-border dark:bg-neutral-800 data-[checked]:bg-accent data-[checked]:text-accent-foreground dark:data-[checked]:bg-accent data-[checked]:border-accent not-data-[checked]:not-data-[disabled]:not-aria-invalid:not-focus-visible:hover:border-border-strong focus-visible:border-ring-focus focus-visible:ring-ring-focus aria-invalid:ring-ring-destructive aria-invalid:border-destructive size-4 shrink-0 rounded-full border shadow-xs transition-[transform,color,background-color,border-color,box-shadow] motion-reduce:transition-[color,background-color,border-color,box-shadow] duration-small ease-small rotate-[0.0001deg] active:scale-[0.98] outline-none focus-visible:ring-[3px] data-[disabled]:cursor-not-allowed data-[disabled]:opacity-70",
className
)}
{...props}
>
{/* Kept mounted (`keepMounted`) so the dot never unmounts; its show/hide
is a CSS transition on transform+opacity keyed off the root's checked
state, so enter and exit run the same 150ms path and moving fast
between radios interrupts cleanly instead of leaving the outgoing dot
to linger while Base UI waits to unmount it. Under reduced motion the
scale is pinned and only the fade runs. */}
<RadioPrimitive.Indicator
keepMounted
data-slot="radio-group-indicator"
className="grid size-full place-content-center text-current scale-50 opacity-0 transition-[transform,opacity] duration-small ease-small group-data-[checked]/radio:scale-100 group-data-[checked]/radio:opacity-100 motion-reduce:scale-100 motion-reduce:transition-[opacity]"
>
<span className="size-1.5 rounded-full bg-current" />
</RadioPrimitive.Indicator>
</RadioPrimitive.Root>
)
}
export { RadioGroup, RadioGroupItem }