Checkbox
A control that allows the user to toggle between checked and not checked.
By clicking this checkbox, you agree to the terms and conditions.
tsx
"use client"
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function CheckboxDemo() { return ( <div className="flex flex-col gap-6"> <div className="flex items-center gap-3"> <Checkbox id="terms" /> <Label htmlFor="terms">Accept terms and conditions</Label> </div> <div className="flex items-start gap-3"> <Checkbox id="terms-2" defaultChecked /> <div className="grid gap-2"> <Label htmlFor="terms-2">Accept terms and conditions</Label> <p className="text-muted-foreground text-sm"> By clicking this checkbox, you agree to the terms and conditions. </p> </div> </div> <div className="flex items-start gap-3"> <Checkbox id="toggle" disabled /> <Label htmlFor="toggle">Enable notifications</Label> </div> </div> )}
Installation
CLI
bash
npx fivui add checkbox
Manual
Copy and paste the following code into your project.
bash
npm install @radix-ui/react-checkbox
components/ui/checkbox.tsx
"use client"
import * as React from "react"import * as CheckboxPrimitive from "@radix-ui/react-checkbox"import { Check } from "lucide-react"
import { cn } from "@/lib/utils"
const Checkbox = React.forwardRef< React.ElementRef<typeof CheckboxPrimitive.Root>, React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>>(({ className, ...props }, ref) => ( <CheckboxPrimitive.Root ref={ref} className={cn( "peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground", className )} {...props} > <CheckboxPrimitive.Indicator className={cn("flex items-center justify-center text-current")} > <Check className="h-4 w-4" /> </CheckboxPrimitive.Indicator> </CheckboxPrimitive.Root>))Checkbox.displayName = CheckboxPrimitive.Root.displayName
export { Checkbox }
Usage
tsx
import { Checkbox } from "@/components/ui/checkbox"
tsx
<Checkbox />
Examples
Default
tsx
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function CheckboxDemo() { return ( <div className="flex items-center space-x-2"> <Checkbox id="terms" /> <Label htmlFor="terms">Accept terms and conditions</Label> </div> )}
Checked
tsx
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function CheckboxChecked() { return ( <div className="flex items-center space-x-2"> <Checkbox id="terms" defaultChecked /> <Label htmlFor="terms">Accept terms and conditions</Label> </div> )}
Disabled
tsx
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function CheckboxDisabled() { return ( <div className="flex items-center space-x-2"> <Checkbox id="terms" disabled /> <Label htmlFor="terms">Accept terms and conditions</Label> </div> )}
With Description
You agree to our Terms of Service and Privacy Policy.
tsx
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function CheckboxWithDescription() { return ( <div className="flex items-start space-x-3"> <Checkbox id="terms" defaultChecked /> <div className="grid gap-1.5 leading-none"> <Label htmlFor="terms">Accept terms and conditions</Label> <p className="text-sm text-muted-foreground"> You agree to our Terms of Service and Privacy Policy. </p> </div> </div> )}
API Reference
Checkbox
Prop | Type | Default | Description |
---|---|---|---|
checked | boolean | - | The controlled checked state of the checkbox |
defaultChecked | boolean | false | The default checked state when initially rendered |
onCheckedChange | function | - | Event handler called when the checked state changes |
disabled | boolean | false | When true, prevents the user from interacting with the checkbox |
Checkbox inherits all props from Radix UI Checkbox
primitive.