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