By clicking this checkbox, you agree to the terms.
"use client"
import { Checkbox } from "@/registry/ui/checkbox"Installation#
pnpm dlx shadcn@latest add https://ui.tyap.me/r/styles/base/checkbox.jsonnpx shadcn@latest add https://ui.tyap.me/r/styles/base/checkbox.jsonyarn dlx shadcn@latest add https://ui.tyap.me/r/styles/base/checkbox.jsonbunx --bun shadcn@latest add https://ui.tyap.me/r/styles/base/checkbox.json
Install the following dependencies:
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/react
Copy and paste the following code into your project.
"use client"
import * as React from "react"
import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox"
import { CheckIcon } from "lucide-react"
import { useShakeOnInvalid } from "@/hooks/use-shake-on-invalid"
import { cn } from "@/lib/utils"
const CHECKBOX_ANIMATION_CSS = `
.cn-checkbox {
transition:
border-color 220ms cubic-bezier(0.22, 1, 0.36, 1),
background-color 220ms cubic-bezier(0.22, 1, 0.36, 1),
box-shadow 240ms cubic-bezier(0.22, 1, 0.36, 1),
transform 130ms cubic-bezier(0.22, 1, 0.36, 1);
cursor: pointer;
}
.cn-checkbox:not(:disabled):not([data-disabled]):active {
transform: scale(0.89);
transition-duration: 70ms;
}
.cn-checkbox-indicator {
animation: cn-checkbox-pop-in 180ms ease-out both;
}
@keyframes cn-checkbox-pop-in {
from { opacity: 0; scale: 0.5; }
to { opacity: 1; scale: 1; }
}
@media (prefers-reduced-motion: reduce) {
.cn-checkbox { transition: none !important; }
.cn-checkbox-indicator { animation: none !important; }
}
`
function Checkbox({ className, ...props }: CheckboxPrimitive.Root.Props) {
const ref = React.useRef<HTMLButtonElement>(null)
useShakeOnInvalid(ref)
return (
<>
<style precedence="component" href="cn-checkbox-transitions">
{CHECKBOX_ANIMATION_CSS}
</style>
<CheckboxPrimitive.Root
ref={ref}
data-slot="checkbox"
className={cn(
"t-input peer relative shrink-0 outline-none after:absolute after:-inset-x-3 after:-inset-y-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
{...props}
>
<CheckboxPrimitive.Indicator
data-slot="checkbox-indicator"
className="grid place-content-center text-current"
>
<CheckIcon />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
</>
)
}
export { Checkbox }
Update the import paths to match your project setup.
Usage#
import { Checkbox } from "@/components/ui/checkbox"<Checkbox />Checked State#
Use defaultChecked for uncontrolled checkboxes, or checked and
onCheckedChange to control the state.
import * as React from "react"
export function Example() {
const [checked, setChecked] = React.useState(false)
return <Checkbox checked={checked} onCheckedChange={setChecked} />
}Invalid State#
Set aria-invalid on the checkbox and data-invalid on the field wrapper to
show the invalid styles.
import { Checkbox } from "@/registry/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
Examples#
Basic#
Pair the checkbox with Field and FieldLabel for proper layout and labeling.
import { Checkbox } from "@/registry/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
Description#
Use FieldContent and FieldDescription for helper text.
By clicking this checkbox, you agree to the terms and conditions.
import { Checkbox } from "@/registry/ui/checkbox"
import {
Field,Disabled#
Use the disabled prop to prevent interaction and add the data-disabled attribute to the <Field> component for disabled styles.
import { Checkbox } from "@/registry/ui/checkbox"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
Group#
Use multiple fields to create a checkbox list.
import { Checkbox } from "@/registry/ui/checkbox"
import {
Field,Table#
| Name | Role | ||
|---|---|---|---|
| Sarah Chen | sarah.chen@example.com | Admin | |
| Marcus Rodriguez | marcus.rodriguez@example.com | User | |
| Priya Patel | priya.patel@example.com | User | |
| David Kim | david.kim@example.com | Editor |
"use client"
import * as React from "react"RTL#
To enable RTL support in shadcn/ui, see the RTL configuration guide.
بالنقر على هذا المربع، فإنك توافق على الشروط.
"use client"
import * as React from "react"API Reference#
See the Base UI documentation for more information.