RadioGroupDemo
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
export function RadioGroupDemo() {
return (
<div className='flex flex-wrap items-center gap-4'>
<RadioGroup defaultValue='comfortable'>
<div className='flex items-center gap-3'>
<RadioGroupItem value='default' id='r1' />
<label htmlFor='r1'>Default</label>
</div>
<div className='flex items-center gap-3'>
<RadioGroupItem value='comfortable' id='r2' />
<label htmlFor='r2'>Comfortable</label>
</div>
<div className='flex items-center gap-3'>
<RadioGroupItem value='compact' id='r3' />
<label htmlFor='r3'>Compact</label>
</div>
</RadioGroup>
</div>
)
}
Installation
- Install the following dependencies
pnpm add @radix-ui/react-radio-group- Copy and paste the following code into your project
src/components/ui/radio-group.tsx
'use client'
import * as React from 'react'
import * as RadioGroupPrimitive from '@radix-ui/react-radio-group'
import { cn } from '@/lib/utils'
import { Ripple } from './ripple'
function RadioGroup({ className, ...props }: React.ComponentProps<typeof RadioGroupPrimitive.Root>) {
return <RadioGroupPrimitive.Root data-slot='radio-group' className={cn('grid gap-3', className)} {...props} />
}
function RadioGroupItem({ className, ...props }: React.ComponentProps<typeof RadioGroupPrimitive.Item>) {
return (
<RadioGroupPrimitive.Item
data-slot='radio-group-item'
className={cn(
'group relative size-5 cursor-pointer rounded-full border-2 border-on-surface-variant text-primary transition-[color,box-shadow] outline-none disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:border-primary',
className,
)}
{...props}
>
<span className='absolute top-1/2 left-1/2 block h-10 w-10 -translate-x-1/2 -translate-y-1/2 rounded-full transition-colors group-hover:bg-on-surface/8 group-active:bg-on-surface/10'>
<Ripple />
</span>
<RadioGroupPrimitive.Indicator
data-slot='radio-group-indicator'
className='flex h-4 w-4 items-center justify-center'
>
<span className='block size-3 rounded-full bg-primary' />
</RadioGroupPrimitive.Indicator>
</RadioGroupPrimitive.Item>
)
}
export { RadioGroup, RadioGroupItem }
Usage
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'<RadioGroup defaultValue="option-one">
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-one" id="option-one" />
<label htmlFor="option-one">Option One</label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-two" id="option-two" />
<label htmlFor="option-two">Option Two</label>
</div>
</RadioGroup>API Reference
RadioGroup
| Prop | type | default |
|---|---|---|
| defaultValue | string | |
| value | string | |
| onValueChange | function | |
| disabled | boolean | false |
| name | string | |
| required | boolean | false |
| orientation | horizontal | vertical | |
| dir | ltr | rtl | |
| loop | boolean | true |
| asChild | boolean | false |
RadioGroupItem
| Prop | type | default |
|---|---|---|
| value | string | |
| disabled | boolean | false |
| required | boolean | false |
| asChild | boolean | false |