Radio Group
An animated React radio group built on shadcn/ui and Radix: the drop-in RadioGroup scales its dot in and turns mint when checked — plus Radio Cards, selectable plan and pricing cards whose outline glides to the chosen card with Motion.
- Drop-in shadcn API
- Radix Radio Group
- CSS transitions
- Motion layoutId
- prefers-reduced-motion
"use client";
import * as React from "react";
import { Building2, Rocket, Sparkles } from "lucide-react";
import { RadioCard, RadioCards } from "@/components/dusk/radio-group/radio-cards";
export default function RadioCardsDemo() {
const [plan, setPlan] = React.useState("pro");
return (
<RadioCards
value={plan}
onValueChange={setPlan}
name="plan"
aria-label="Plan"
className="w-full max-w-md"
>
<RadioCard
value="hobby"
icon={<Sparkles />}
title="Hobby"
aside="$0"
description="Personal projects and experiments."
/>
<RadioCard
value="pro"
icon={<Rocket />}
title="Pro"
aside="$24/mo"
description="Unlimited projects, preview deployments and analytics."
/>
<RadioCard
value="enterprise"
icon={<Building2 />}
title="Enterprise"
aside="Custom"
description="SSO, audit logs and a dedicated support engineer."
/>
</RadioCards>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/radio-group.json1. Install the dependencies.
npm install @radix-ui/react-radio-group2. Copy the source into
components/ui/radio-group.tsx.radio-group.tsx "use client"; import * as React from "react"; import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"; import { cn } from "@/lib/utils"; /** * DuskUI Radio Group — a drop-in upgrade of the shadcn/ui Radio Group. * * Same parts, props and data-slot attributes as shadcn (RadioGroup, * RadioGroupItem) plus: * - The dot scales in quickly (CSS only, no overshoot) and shrinks away when * another option is picked. * - The ring turns mint when checked. * - Works uncontrolled, in forms and with form reset (pure CSS, no extra state). * - Under `prefers-reduced-motion` the dot appears instantly. * Powered by Radix for roving focus (arrow keys), form submission and aria. */ function RadioGroup({ className, ...props }: React.ComponentProps<typeof RadioGroupPrimitive.Root>) { return ( <RadioGroupPrimitive.Root data-slot="radio-group" className={cn("grid gap-3", className)} {...props} /> ); } /** One option. Pair it with a `<label htmlFor>` so the text is clickable and announced. */ function RadioGroupItem({ className, ...props }: React.ComponentProps<typeof RadioGroupPrimitive.Item>) { return ( <RadioGroupPrimitive.Item data-slot="radio-group-item" className={cn( "peer group/radio relative grid aspect-square size-4 shrink-0 place-items-center rounded-full border border-input bg-input/30 text-primary shadow-xs outline-none", "transition-[border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "data-[state=checked]:border-primary", "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50", "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40", "disabled:cursor-not-allowed disabled:opacity-50", className )} {...props} > {/* forceMount keeps the dot in the DOM so it can shrink away as well as scale in. */} <RadioGroupPrimitive.Indicator forceMount data-slot="radio-group-indicator" className="pointer-events-none grid place-items-center" > <span aria-hidden className={cn( "block size-2 rounded-full bg-current", "scale-0 opacity-0 group-data-[state=checked]/radio:scale-100 group-data-[state=checked]/radio:opacity-100", // Out: quick ease-in. In: short ease-out, no overshoot. "[transition:scale_120ms_ease-in,opacity_120ms_ease-in]", "group-data-[state=checked]/radio:[transition:scale_180ms_ease-out,opacity_150ms_ease-out]", "motion-reduce:transition-none motion-reduce:group-data-[state=checked]/radio:transition-none" )} /> </RadioGroupPrimitive.Indicator> </RadioGroupPrimitive.Item> ); } export { RadioGroup, RadioGroupItem };
Replaces components/ui/radio-group.tsx with the same parts and data-slot attributes. The dot animates with a CSS transition, so there is no JavaScript per frame.
Usage
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
export function Density() {
return (
<RadioGroup defaultValue="comfortable" aria-label="Density">
<div className="flex items-center gap-3">
<RadioGroupItem value="comfortable" id="comfortable" />
<label htmlFor="comfortable">Comfortable</label>
</div>
<div className="flex items-center gap-3">
<RadioGroupItem value="compact" id="compact" />
<label htmlFor="compact">Compact</label>
</div>
</RadioGroup>
)
}Examples
Default
Pick an option to watch the dot scale in while the previous one shrinks away. Arrow keys move the selection.
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
const options = [
{ value: "default", label: "Default" },
{ value: "comfortable", label: "Comfortable" },
{ value: "compact", label: "Compact" },
];
export default function RadioGroupBasicDemo() {
return (
<RadioGroup defaultValue="comfortable" aria-label="Density">
{options.map((option) => (
<div key={option.value} className="flex items-center gap-3">
<RadioGroupItem value={option.value} id={`density-${option.value}`} />
<label htmlFor={`density-${option.value}`} className="text-sm font-medium">
{option.label}
</label>
</div>
))}
</RadioGroup>
);
}With descriptions
Wrap the group in a fieldset with a legend, and link each hint with aria-describedby. name submits the value with a form.
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
const options = [
{ value: "all", label: "All new messages", hint: "Every message in every channel." },
{ value: "mentions", label: "Direct messages and mentions", hint: "Only when someone needs you." },
{ value: "none", label: "Nothing", hint: "Mute notifications on this device." },
];
export default function RadioGroupDescriptionDemo() {
return (
<fieldset className="grid w-full max-w-sm gap-3">
<legend className="mb-3 text-sm font-medium">Notify me about…</legend>
<RadioGroup defaultValue="mentions" name="notify" className="gap-4">
{options.map((option) => (
<div key={option.value} className="flex items-start gap-3">
<RadioGroupItem
value={option.value}
id={`notify-${option.value}`}
aria-describedby={`notify-${option.value}-hint`}
className="mt-0.5"
/>
<div className="grid gap-1">
<label htmlFor={`notify-${option.value}`} className="text-sm leading-none font-medium">
{option.label}
</label>
<p id={`notify-${option.value}-hint`} className="text-xs text-muted-foreground">
{option.hint}
</p>
</div>
</div>
))}
</RadioGroup>
</fieldset>
);
}Horizontal, disabled & invalid
Lay items out in a row with orientation="horizontal" (Left / Right arrows), disable single items, and mark errors with aria-invalid.
Please choose an option.
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
export default function RadioGroupStatesDemo() {
return (
<div className="flex flex-wrap gap-10">
<RadioGroup defaultValue="card" orientation="horizontal" aria-label="Payment method" className="flex gap-5">
{["card", "paypal", "invoice"].map((value) => (
<div key={value} className="flex items-center gap-2">
<RadioGroupItem value={value} id={`pay-${value}`} disabled={value === "invoice"} />
<label htmlFor={`pay-${value}`} className="text-sm font-medium capitalize peer-disabled:opacity-50">
{value}
</label>
</div>
))}
</RadioGroup>
<div className="grid gap-2">
<RadioGroup aria-label="Agreement" aria-describedby="agree-error" className="flex gap-5">
{["yes", "no"].map((value) => (
<div key={value} className="flex items-center gap-2">
<RadioGroupItem value={value} id={`agree-${value}`} aria-invalid />
<label htmlFor={`agree-${value}`} className="text-sm font-medium capitalize">
{value}
</label>
</div>
))}
</RadioGroup>
<p id="agree-error" className="text-xs text-destructive">
Please choose an option.
</p>
</div>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Radio Cards
Selectable cards for plans, pricing tiers or shipping speeds. A 1px primary outline glides from card to card via a shared Motion layoutId. The whole card is the click target, and it is still a Radix radio group, so arrow keys and forms work.
"use client";
import * as React from "react";
import { Building2, Rocket, Sparkles } from "lucide-react";
import { RadioCard, RadioCards } from "@/components/dusk/radio-group/radio-cards";
export default function RadioCardsDemo() {
const [plan, setPlan] = React.useState("pro");
return (
<RadioCards
value={plan}
onValueChange={setPlan}
name="plan"
aria-label="Plan"
className="w-full max-w-md"
>
<RadioCard
value="hobby"
icon={<Sparkles />}
title="Hobby"
aside="$0"
description="Personal projects and experiments."
/>
<RadioCard
value="pro"
icon={<Rocket />}
title="Pro"
aside="$24/mo"
description="Unlimited projects, preview deployments and analytics."
/>
<RadioCard
value="enterprise"
icon={<Building2 />}
title="Enterprise"
aside="Custom"
description="SSO, audit logs and a dedicated support engineer."
/>
</RadioCards>
);
}$ npx shadcn@latest add https://duskui.com/r/radio-cards.json1. Install the dependencies.
npm install motion @radix-ui/react-radio-group2. Copy the source into
components/dusk/radio-group/radio-cards.tsx.radio-cards.tsx "use client"; import * as React from "react"; import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"; import { LayoutGroup, motion, useReducedMotion, type Transition } from "motion/react"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { cn } from "@/lib/utils"; const outlineSpring: Transition = { type: "spring", bounce: 0.1, duration: 0.3 }; type RadioCardsContextValue = { value: string; layoutId: string; reduceMotion: boolean }; const RadioCardsContext = React.createContext<RadioCardsContextValue | null>(null); function useRadioCards() { const context = React.useContext(RadioCardsContext); if (!context) throw new Error("RadioCard must be used inside <RadioCards>."); return context; } export interface RadioCardsProps extends Omit<React.ComponentProps<typeof RadioGroupPrimitive.Root>, "value" | "defaultValue" | "onValueChange"> { value?: string; defaultValue?: string; onValueChange?: (value: string) => void; } /** * Selectable cards for plans, shipping speeds or any one-of-many choice. A * 1px primary outline glides from card to card (a shared Motion `layoutId`). * It is a Radix radio group underneath, * so arrow keys move the selection, the value submits with forms via `name` * and each card is announced as a radio named by its title. Lay the cards out * with `className` (it is a CSS grid). Under reduced motion the outline jumps. */ export function RadioCards({ value: valueProp, defaultValue = "", onValueChange, className, children, ...props }: RadioCardsProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState(defaultValue); const value = valueProp ?? uncontrolled; const context = React.useMemo( () => ({ value, layoutId: `${id}-outline`, reduceMotion }), [value, id, reduceMotion] ); return ( <RadioCardsContext.Provider value={context}> <LayoutGroup id={id}> <RadioGroup value={value} onValueChange={(next) => { if (valueProp === undefined) setUncontrolled(next); onValueChange?.(next); }} className={cn("grid gap-3", className)} {...props} > {children} </RadioGroup> </LayoutGroup> </RadioCardsContext.Provider> ); } export interface RadioCardProps extends Omit<React.ComponentProps<typeof RadioGroupPrimitive.Item>, "children" | "title"> { value: string; title: React.ReactNode; description?: React.ReactNode; /** Leading icon element. */ icon?: React.ReactNode; /** Content on the right of the title, e.g. a price. */ aside?: React.ReactNode; /** Extra content under the description (features, badges…). */ children?: React.ReactNode; /** Classes for the card (the `<label>`). `className` styles the radio. */ containerClassName?: string; } /** One card. The whole card is the click target; the radio inside keeps Radix semantics. */ export function RadioCard({ value, title, description, icon, aside, children, id, disabled, className, containerClassName, ...props }: RadioCardProps) { const autoId = React.useId(); const radioId = id ?? autoId; const { value: selected, layoutId, reduceMotion } = useRadioCards(); const checked = selected === value; return ( <label htmlFor={radioId} data-slot="radio-card" data-state={checked ? "checked" : "unchecked"} data-disabled={disabled || undefined} className={cn( "group/card relative isolate flex cursor-pointer items-start gap-3 rounded-xl border bg-card p-4 select-none", "shadow-xs", "transition-[background-color,border-color] duration-150 ease-out motion-reduce:transition-none", "hover:bg-accent/50 data-[state=checked]:border-transparent", "has-focus-visible:ring-[3px] has-focus-visible:ring-ring/40", "data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:hover:border-border", containerClassName )} > {checked && ( <motion.span aria-hidden layoutId={layoutId} transition={reduceMotion ? { duration: 0 } : outlineSpring} className="pointer-events-none absolute -inset-px -z-10 rounded-xl border border-primary" /> )} {icon && ( <span aria-hidden className={cn( "grid size-9 shrink-0 place-items-center rounded-md border border-white/10 bg-white/[0.03] text-muted-foreground", "transition-colors duration-150 motion-reduce:transition-none", "group-data-[state=checked]/card:text-foreground", "[&_svg:not([class*='size-'])]:size-4" )} > {icon} </span> )} <span className="grid min-w-0 flex-1 gap-1"> <span className="flex items-baseline justify-between gap-3"> <span id={`${radioId}-title`} className="text-sm leading-5 font-medium"> {title} </span> {aside && ( <span className="shrink-0 text-sm font-medium tabular-nums"> {aside} </span> )} </span> {description && ( <span id={`${radioId}-description`} className="text-sm text-muted-foreground"> {description} </span> )} {children} </span> <RadioGroupItem id={radioId} value={value} disabled={disabled} aria-labelledby={`${radioId}-title`} aria-describedby={description ? `${radioId}-description` : undefined} className={cn("mt-0.5 size-[1.125rem] [&_[data-slot=radio-group-indicator]>span]:size-2.5", className)} {...props} /> </label> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| value / onValueChange | string / (value: string) => void | — | Controlled selection (on RadioCards). |
| defaultValue | string | — | Uncontrolled initial selection. |
| name / required / disabled / orientation | Radix RadioGroup props | — | Forwarded to the Radix root. |
| className | string | — | On RadioCards: it is a CSS grid, e.g. grid-cols-3. |
| RadioCard › title | ReactNode | — | Card title; names the radio. |
| RadioCard › description | ReactNode | — | Secondary text; describes the radio. |
| RadioCard › icon / aside | ReactNode | — | Leading icon and right-aligned content such as a price. |
| RadioCard › children | ReactNode | — | Extra content under the description (features, badges). |
| RadioCard › containerClassName | string | — | Classes for the card. `className` styles the radio. |
Radio Cards — grid
The same block in a three-column grid (className="sm:grid-cols-3"). The outline travels across the row, and the cards stack on small screens.
import { Bike, Plane, Truck } from "lucide-react";
import { RadioCard, RadioCards } from "@/components/dusk/radio-group/radio-cards";
export default function RadioCardsGridDemo() {
return (
<RadioCards
defaultValue="standard"
name="shipping"
aria-label="Shipping speed"
className="w-full max-w-xl grid-cols-1 sm:grid-cols-3"
>
<RadioCard value="economy" icon={<Bike />} title="Economy" description="5–7 days · Free" />
<RadioCard value="standard" icon={<Truck />} title="Standard" description="2–3 days · $4.99" />
<RadioCard value="express" icon={<Plane />} title="Express" description="Next day · $14.99" />
</RadioCards>
);
}$ npx shadcn@latest add https://duskui.com/r/radio-cards.json1. Install the dependencies.
npm install motion @radix-ui/react-radio-group2. Copy the source into
components/dusk/radio-group/radio-cards.tsx.radio-cards.tsx "use client"; import * as React from "react"; import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"; import { LayoutGroup, motion, useReducedMotion, type Transition } from "motion/react"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { cn } from "@/lib/utils"; const outlineSpring: Transition = { type: "spring", bounce: 0.1, duration: 0.3 }; type RadioCardsContextValue = { value: string; layoutId: string; reduceMotion: boolean }; const RadioCardsContext = React.createContext<RadioCardsContextValue | null>(null); function useRadioCards() { const context = React.useContext(RadioCardsContext); if (!context) throw new Error("RadioCard must be used inside <RadioCards>."); return context; } export interface RadioCardsProps extends Omit<React.ComponentProps<typeof RadioGroupPrimitive.Root>, "value" | "defaultValue" | "onValueChange"> { value?: string; defaultValue?: string; onValueChange?: (value: string) => void; } /** * Selectable cards for plans, shipping speeds or any one-of-many choice. A * 1px primary outline glides from card to card (a shared Motion `layoutId`). * It is a Radix radio group underneath, * so arrow keys move the selection, the value submits with forms via `name` * and each card is announced as a radio named by its title. Lay the cards out * with `className` (it is a CSS grid). Under reduced motion the outline jumps. */ export function RadioCards({ value: valueProp, defaultValue = "", onValueChange, className, children, ...props }: RadioCardsProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState(defaultValue); const value = valueProp ?? uncontrolled; const context = React.useMemo( () => ({ value, layoutId: `${id}-outline`, reduceMotion }), [value, id, reduceMotion] ); return ( <RadioCardsContext.Provider value={context}> <LayoutGroup id={id}> <RadioGroup value={value} onValueChange={(next) => { if (valueProp === undefined) setUncontrolled(next); onValueChange?.(next); }} className={cn("grid gap-3", className)} {...props} > {children} </RadioGroup> </LayoutGroup> </RadioCardsContext.Provider> ); } export interface RadioCardProps extends Omit<React.ComponentProps<typeof RadioGroupPrimitive.Item>, "children" | "title"> { value: string; title: React.ReactNode; description?: React.ReactNode; /** Leading icon element. */ icon?: React.ReactNode; /** Content on the right of the title, e.g. a price. */ aside?: React.ReactNode; /** Extra content under the description (features, badges…). */ children?: React.ReactNode; /** Classes for the card (the `<label>`). `className` styles the radio. */ containerClassName?: string; } /** One card. The whole card is the click target; the radio inside keeps Radix semantics. */ export function RadioCard({ value, title, description, icon, aside, children, id, disabled, className, containerClassName, ...props }: RadioCardProps) { const autoId = React.useId(); const radioId = id ?? autoId; const { value: selected, layoutId, reduceMotion } = useRadioCards(); const checked = selected === value; return ( <label htmlFor={radioId} data-slot="radio-card" data-state={checked ? "checked" : "unchecked"} data-disabled={disabled || undefined} className={cn( "group/card relative isolate flex cursor-pointer items-start gap-3 rounded-xl border bg-card p-4 select-none", "shadow-xs", "transition-[background-color,border-color] duration-150 ease-out motion-reduce:transition-none", "hover:bg-accent/50 data-[state=checked]:border-transparent", "has-focus-visible:ring-[3px] has-focus-visible:ring-ring/40", "data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:hover:border-border", containerClassName )} > {checked && ( <motion.span aria-hidden layoutId={layoutId} transition={reduceMotion ? { duration: 0 } : outlineSpring} className="pointer-events-none absolute -inset-px -z-10 rounded-xl border border-primary" /> )} {icon && ( <span aria-hidden className={cn( "grid size-9 shrink-0 place-items-center rounded-md border border-white/10 bg-white/[0.03] text-muted-foreground", "transition-colors duration-150 motion-reduce:transition-none", "group-data-[state=checked]/card:text-foreground", "[&_svg:not([class*='size-'])]:size-4" )} > {icon} </span> )} <span className="grid min-w-0 flex-1 gap-1"> <span className="flex items-baseline justify-between gap-3"> <span id={`${radioId}-title`} className="text-sm leading-5 font-medium"> {title} </span> {aside && ( <span className="shrink-0 text-sm font-medium tabular-nums"> {aside} </span> )} </span> {description && ( <span id={`${radioId}-description`} className="text-sm text-muted-foreground"> {description} </span> )} {children} </span> <RadioGroupItem id={radioId} value={value} disabled={disabled} aria-labelledby={`${radioId}-title`} aria-describedby={description ? `${radioId}-description` : undefined} className={cn("mt-0.5 size-[1.125rem] [&_[data-slot=radio-group-indicator]>span]:size-2.5", className)} {...props} /> </label> ); }
API Reference
Same parts as shadcn/ui: RadioGroup and RadioGroupItem, each forwarding its Radix Radio Group props.
| Prop | Type | Default | Description |
|---|---|---|---|
| value / onValueChange | string / (value: string) => void | — | Controlled selection (on RadioGroup). |
| defaultValue | string | — | Uncontrolled initial selection. |
| name / required | string / boolean | — | Form integration on RadioGroup. |
| orientation | "horizontal" | "vertical" | — | Which arrow keys move the selection. |
| loop | boolean | true | Wrap around at the ends. |
| value / disabled | string / boolean | — | On RadioGroupItem. |
Accessibility
- Built on Radix Radio Group: a radiogroup with radio items, roving focus (one Tab stop) and arrow keys that move the selection.
- Give the group a name with aria-label, aria-labelledby or a fieldset legend, and pair each item with a <label htmlFor>.
- Radio Cards name each radio by its title (aria-labelledby) and describe it with the description (aria-describedby); the card is a <label>, so clicking anywhere selects.
- The gliding outline is decorative (aria-hidden); the checked state is conveyed by aria-checked.
- Under prefers-reduced-motion the dot appears instantly and the outline jumps instead of gliding.