Checkbox
Checkbox on Radix Checkbox with a check indicator and focus and invalid states.
"use client"
/*
* Adapted from shadcn/ui (https://github.com/shadcn-ui/ui).
*
* MIT License
*
* Copyright (c) 2023 shadcn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import * as React from "react"
import { cn } from "@/lib/utils"
import { CheckIcon } from "lucide-react"
import { Checkbox as CheckboxPrimitive } from "radix-ui"
function Checkbox({
className,
...props
}: React.ComponentProps<typeof CheckboxPrimitive.Root>) {
return (
<CheckboxPrimitive.Root
data-slot="checkbox"
className={cn(
"peer size-4 shrink-0 rounded-[4px] border border-input bg-background transition-[background-color,border-color,box-shadow] outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive data-[state=checked]:border-primary data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
className
)}
{...props}
>
<CheckboxPrimitive.Indicator
data-slot="checkbox-indicator"
className="grid place-content-center text-current transition-none"
>
<CheckIcon className="size-3.5" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
)
}
export { Checkbox }Installation
pnpm dlx shadcn@latest add @ward/checkboxnpx shadcn@latest add @ward/checkboxyarn dlx shadcn@latest add @ward/checkboxbunx --bun shadcn@latest add @ward/checkboxAdd the Ward registry to components.json once:
"registries": {
"@ward": "https://ward.so/r/{name}.json"
}Or install from the URL with no configuration: npx shadcn@latest add https://ward.so/r/checkbox.json
Install the dependencies.
npm install lucide-react@1.47.0 radix-ui@1.6.7Copy each file from the Code tab into:
components/ui/checkbox.tsx
Update the import paths to match your project.
Usage
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";<div className="flex items-center gap-2">
<Checkbox id="terms" defaultChecked onCheckedChange={(checked) => console.log(checked)} />
<Label htmlFor="terms">Accept the terms</Label>
</div>Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | "indeterminate" | - | Whether the box is checked, when the parent controls it; indeterminate draws a check too. |
defaultChecked | boolean | false | Whether the box starts checked when it is uncontrolled. |
onCheckedChange | (checked: boolean | "indeterminate") => void | - | Called when the box is toggled. |
disabled | boolean | false | Dim the box and stop it taking input. |
aria-invalid | boolean | false | Marks the box as invalid and draws the destructive border. |
Ward · Version 1.0.0 · Registry manifest