Components
- Accordion
- Alert
- Alert Dialog
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Button
- Card
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Dialog
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Label
- Menu
- Meter
- Number Field
- Pagination
- Popover
- Preview Card
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Slider
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Toolbar
- Tooltip
Resources
Radio Group
A set of checkable buttons where no more than one of the buttons can be checked at a time.
import { Label } from "@/components/ui/label"
import { Radio, RadioGroup } from "@/components/ui/radio-group"
export function RadioGroupDemo() {
return (
<RadioGroup defaultValue="next">
<Label>
<Radio value="next" /> Next.js
</Label>
<Label>
<Radio value="vite" /> Vite
</Label>
<Label>
<Radio value="astro" /> Astro
</Label>
</RadioGroup>
)
}
Installation
pnpm dlx shadcn@latest add https://coss.com/ui/r/radio-group.json
Usage
import { Label } from "@/components/ui/label"
import { Radio, RadioGroup } from "@/components/ui/radio-group"
<RadioGroup defaultValue="next">
<Label>
<Radio value="next" /> Next.js
</Label>
<Label>
<Radio value="vite" /> Vite
</Label>
<Label>
<Radio value="astro" /> Astro
</Label>
</RadioGroup>
Examples
For accessible labelling and validation, prefer using the Field
component to wrap radio buttons. See the related example: Radio Group field.
Disabled
import { Label } from "@/components/ui/label"
import { Radio, RadioGroup } from "@/components/ui/radio-group"
export function RadioGroupDisabledDemo() {
return (
<RadioGroup defaultValue="next">
<Label>
<Radio value="next" /> Next.js
</Label>
<Label>
<Radio value="vite" disabled /> Vite (disabled)
</Label>
<Label>
<Radio value="astro" /> Astro
</Label>
</RadioGroup>
)
}
With Description
Basic features for personal use.
Advanced tools for professionals.
import { Label } from "@/components/ui/label"
import { Radio, RadioGroup } from "@/components/ui/radio-group"
export function RadioGroupWithDescriptionDemo() {
return (
<RadioGroup defaultValue="r-1">
<div className="flex items-start gap-2">
<Radio value="r-1" id="r-1" />
<div className="flex flex-col gap-1">
<Label htmlFor="r-1">Free</Label>
<p className="text-xs text-muted-foreground">
Basic features for personal use.
</p>
</div>
</div>
<div className="flex items-start gap-2">
<Radio value="r-2" id="r-2" />
<div className="flex flex-col gap-1">
<Label htmlFor="r-2">Pro</Label>
<p className="text-xs text-muted-foreground">
Advanced tools for professionals.
</p>
</div>
</div>
</RadioGroup>
)
}
Card Style
import { Label } from "@/components/ui/label"
import { Radio, RadioGroup } from "@/components/ui/radio-group"
export function RadioGroupCardDemo() {
return (
<RadioGroup defaultValue="r-1">
<Label className="flex items-start gap-2 rounded-lg border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50">
<Radio value="r-1" />
<div className="flex flex-col gap-1">
<p className="text-sm leading-4">Email</p>
<p className="text-xs text-muted-foreground">
Receive notifications via email.
</p>
</div>
</Label>
<Label className="flex items-start gap-2 rounded-lg border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50">
<Radio value="r-2" />
<div className="flex flex-col gap-1">
<p className="text-sm leading-4">SMS</p>
<p className="text-xs text-muted-foreground">
Receive notifications via text message.
</p>
</div>
</Label>
</RadioGroup>
)
}
Form Integration
"use client"
import * as React from "react"
import { Button } from "@/components/ui/button"
import { Field, FieldLabel } from "@/components/ui/field"
import { Fieldset, FieldsetLegend } from "@/components/ui/fieldset"
import { Form } from "@/components/ui/form"
import { Radio, RadioGroup } from "@/components/ui/radio-group"
export function RadioGroupFormDemo() {
const [loading, setLoading] = React.useState(false)
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const formData = new FormData(e.currentTarget)
setLoading(true)
await new Promise((r) => setTimeout(r, 800))
setLoading(false)
alert(`Selected: ${formData.get("frameworks")}`)
}
return (
<Form onSubmit={onSubmit} className="max-w-[160px]">
<Field
name="frameworks"
className="gap-4"
render={(props) => <Fieldset {...props} />}
>
<FieldsetLegend className="text-sm font-medium">
Frameworks
</FieldsetLegend>
<RadioGroup defaultValue="next">
<FieldLabel>
<Radio value="next" disabled={loading} /> Next.js
</FieldLabel>
<FieldLabel>
<Radio value="vite" disabled={loading} /> Vite
</FieldLabel>
<FieldLabel>
<Radio value="astro" disabled={loading} /> Astro
</FieldLabel>
</RadioGroup>
</Field>
<Button type="submit" disabled={loading}>
Submit
</Button>
</Form>
)
}
Comparing with Radix / shadcn
If you’re already familiar with Radix UI and shadcn/ui, this guide highlights the small differences and similarities so you can get started with coss.com ui quickly.
Quick Checklist
- Use
Radio
going forward;RadioGroupItem
remains for legacy - If you used
asChild
on parts, switch to therender
prop