A stylized radio that allows you to add additional information or design elements to Radioes.
If you’re looking for RadioGroup, you can find it here.
import { RadioCard } from "#/ui/components/RadioCard"
<RadioCard>Radio</RadioCard>
As you build more complex components sometimes a simple radio doesn’t quite solve the needs of your design. The RadioCard component allows you to add additional information or design elements to Radioes.
The RadioCard component accepts any content as its children. This allows you to add additional information or design elements to radios.
Make sure you pass a unique id to the RadioCard component that’s associated with a label element. This will ensure that the radio is accessible.
<RadioCard id="custom-radio" defaultChecked={true} className="flex gap-4 max-w-md">
<ContainerIcon className="size-6 flex-shrink-0" />
<div className="text-left">
<label htmlFor="custom-radio" className="font-bold">
Lorem ipsum
</label>
<p className="text-sm text-gray-500">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla condimentum tortor sem, vel
aliquet velit blandit ut.
</p>
</div>
</RadioCard>
Alternatively, you can just pass a string to your RadioCard component (in which case, an id will be generated for you).
<RadioCard>Radio</RadioCard>
If you’d like to pre-select the radio, you can pass the defaultChecked
prop.
<RadioCard defaultChecked>Default Checked</RadioCard>
By default, the RadioCard manages its own state. You can control the state of the RadioCard from outside of the component by passing the checked
and onChange
props.
export const Controlled = () => {
const [checked, setChecked] = useState(false)
return (
<RadioCard
checked={checked}
onChange={() => setChecked(!checked)}
>Controlled</RadioCard>
)
}
If you’d like, you can show the radio itself by passing the showRadio
prop to the RadioCard component.
<RadioCard showRadio>Show Radio</RadioCard>
This tends to work better on larger cards
<RadioCard
id="custom-radio"
defaultChecked={true}
showRadio={true}
className="flex gap-4 max-w-md"
>
<ContainerIcon className="size-6 flex-shrink-0" />
<div className="text-left">
<label htmlFor="custom-radio" className="font-bold">
Lorem ipsum
</label>
<p className="text-sm text-gray-500">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla condimentum tortor sem, vel
aliquet velit blandit ut.
</p>
</div>
</RadioCard>
For the sake of simplicity, you do lose out on some radio functionality when you use the radio card, however. If you’re missing some controls it’s recommended that you extend the RadioCard component, or create your own card components using the radio.
All custom classes will be passed down to the RadioCard, so you can style it however you want.
<RadioCard
defaultChecked
className="font-medium data-[headlessui-state=checked]:bg-indigo-500 data-[headlessui-state=checked]:text-white"
>
Custom Radio
</RadioCard>