shadcn.io is not affiliated with official shadcn/ui
Shadcn Radio Group for React
Allows single selection from multiple options.
Installation
bunx --bun shadcn@latest add https://kit.dev/r/base/radio-group.jsonnpx shadcn@latest add https://kit.dev/r/base/radio-group.jsonpnpm dlx shadcn@latest add https://kit.dev/r/base/radio-group.jsonyarn shadcn@latest add https://kit.dev/r/base/radio-group.jsonThis component depends on Field. Install it first if you haven't already.
Install the following dependencies:
bun add @base-ui/reactnpm install @base-ui/reactpnpm add @base-ui/reactyarn add @base-ui/reactCopy and paste the following code into your project.
"use client";
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group";
import type React from "react";
import { cn } from "@/lib/utils";
import { FieldLabel } from "@/components/ui/field";
export const RadioGroup = (props: RadioGroupPrimitive.Props) => {
const { className, ...rest } = props;
return (
<RadioGroupPrimitive
className={cn("cn-radio-group w-full", className)}
data-slot="radio-group"
{...rest}
/>
);Update the import paths to match your project setup.
Anatomy
RadioGroup
├── RadioGroupLabel
├── RadioGroupItem
│ ├── RadioGroupText
│ └── RadioGroupItemControl
└── RadioGroupIndicatorUsage
import {
RadioGroup,
RadioGroupItem,
} from "@/components/ui/radio-group";<RadioGroup className="w-fit" defaultValue="comfortable">
<RadioGroupItem value="default">Default</RadioGroupItem>
<RadioGroupItem value="comfortable">Comfortable</RadioGroupItem>
<RadioGroupItem value="compact">Compact</RadioGroupItem>
</RadioGroup>Accessibility
| Key | Description |
|---|---|
Space | Activate the focused control. |
End | Go to the end. |
Demos
Description
Radio group items with a description using the Field component.
Choice Card
Use FieldLabel to wrap the entire Field for a clickable card-style selection.
Fieldset
Use FieldSet and FieldLegend to group radio items with a label and description.
Disabled
Use the disabled prop on RadioGroup to disable all items.
Invalid
Use aria-invalid on RadioGroupItem and data-invalid on Field to show validation errors.
Rtl
Right-to-left radio group. See the RTL configuration guide for document direction.
API Reference
RadioGroup
PropType
AttributeType
RadioGroupItem
span
PropType
AttributeType
RadioGroupText
PropType
AttributeType
RadioGroupLabel
PropType
AttributeType