shadcn.io is not affiliated with official shadcn/ui
Installation
bunx --bun shadcn@latest add https://kit.dev/r/aria/checkbox.jsonnpx shadcn@latest add https://kit.dev/r/aria/checkbox.jsonpnpm dlx shadcn@latest add https://kit.dev/r/aria/checkbox.jsonyarn shadcn@latest add https://kit.dev/r/aria/checkbox.jsonInstall the following dependencies:
bun add react-aria-components lucide-reactnpm install react-aria-components lucide-reactpnpm add react-aria-components lucide-reactyarn add react-aria-components lucide-reactCopy and paste the following code into your project.
"use client";
import { CheckIcon, MinusIcon } from "lucide-react";
import {
Checkbox as CheckboxPrimitive,
type CheckboxProps,
composeRenderProps,
} from "react-aria-components";
import { cn } from "@/lib/utils";
export const Checkbox = (props: CheckboxProps) => {
const { className, children, ...rest } = props;
return (
<CheckboxPrimitive
className={cn(
"cn-checkbox cn-checkbox-aria peer relative shrink-0 outline-none after:absolute after:-inset-x-3 after:-inset-y-2 data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50",
classNameUpdate the import paths to match your project setup.
Anatomy
Checkbox
├── CheckboxLabel
├── checkboxVariants
└── CheckboxIndicatorUsage
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";<FieldGroup className="w-fit whitespace-nowrap">
<Field orientation="horizontal">
<Checkbox name="terms" />
<FieldLabel>Accept terms</FieldLabel>
</Field>
</FieldGroup>Accessibility
| Key | Description |
|---|---|
Space | Activate the focused control. |
Demos
Invalid
Set aria-invalid on the checkbox and data-invalid on the field wrapper to show the invalid styles.
Basic
Pair the checkbox with Field and FieldLabel for proper layout and labeling.
Description
Use FieldContent and FieldDescription for helper text.
Disabled
Use the disabled prop to prevent interaction and add the data-disabled attribute to the Field for disabled styles.
Group
Use multiple fields to create a checkbox list.
Table
Checkboxes in a table for row selection.
Rtl
Right-to-left checkbox. See the RTL configuration guide for document direction.
API Reference
Checkbox
PropType
AttributeType