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/radix/radio-group.jsonnpx shadcn@latest add https://kit.dev/r/radix/radio-group.jsonpnpm dlx shadcn@latest add https://kit.dev/r/radix/radio-group.jsonyarn shadcn@latest add https://kit.dev/r/radix/radio-group.jsonThis component depends on Field. Install it first if you haven't already.
Install the following dependencies:
bun add radix-uinpm install radix-uipnpm add radix-uiyarn add radix-uiCopy and paste the following code into your project.
"use client";
import { RadioGroup as RadioGroupPrimitive } from "radix-ui";
import type React from "react";
import { cn } from "@/lib/utils";
import { FieldLabel } from "@/components/ui/field";
export const RadioGroup = (
props: React.ComponentProps<typeof RadioGroupPrimitive.Root>
) => {
const { className, ...rest } = props;
return (
<RadioGroupPrimitive.Root
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
div
PropType
defaultValue?stringdir?RovingFocusGroupProps["dir"]loop?RovingFocusGroupProps["loop"]name?RadioGroupContextValue["name"]AttributeType
RadioGroupItem
label
PropType
valuestringdefaultValue?stringdir?RovingFocusGroupProps["dir"]loop?RovingFocusGroupProps["loop"]name?RadioGroupContextValue["name"]AttributeType
RadioGroupIndicator
div
PropType
defaultValue?stringdir?RovingFocusGroupProps["dir"]loop?RovingFocusGroupProps["loop"]name?RadioGroupContextValue["name"]AttributeType
CSS variableType
--heightstring--leftstring--topstring--transition-propertystring--widthstringRadioGroupText
PropType
defaultValue?stringdir?RovingFocusGroupProps["dir"]loop?RovingFocusGroupProps["loop"]name?RadioGroupContextValue["name"]AttributeType
RadioGroupLabel
PropType
defaultValue?stringdir?RovingFocusGroupProps["dir"]loop?RovingFocusGroupProps["loop"]name?RadioGroupContextValue["name"]AttributeType