Back to Inputs
Components / Inputs
Checkbox Field Input
Free Inputs React component — CheckboxFieldInput. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.
Accessible checkbox with custom mark, inline label, hint, and error. Controlled or uncontrolled checked state.
Preview
Setup
How to use
Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.
- 1
Run in your terminal:
$npm install clsx tailwind-merge lucide-react - 2
Copy
lib/cn.tsbelow. Skip if you already havecn(). - 3
Copy the code below and create
components/inputs/checkbox-field-input.tsxin your project. - 4
Import and render:
Exampleimport { CheckboxFieldInput } from "@/components/inputs/checkbox-field-input";
lib/cn.ts
import { type ClassValue, clsx } from "clsx";import { twMerge } from "tailwind-merge";export function cn(...inputs: ClassValue[]) {return twMerge(clsx(inputs));}
components/inputs/checkbox-field-input.tsx
"use client";import {forwardRef,useCallback,useId,useState,type ChangeEvent,type ComponentPropsWithoutRef,} from "react";import { Check } from "lucide-react";import { cn } from "@/lib/cn";export type CheckboxFieldInputProps = Readonly<{label?: string;hint?: string;error?: boolean;errorMessage?: string;containerClassName?: string;onCheckedChange?: (checked: boolean, event: ChangeEvent<HTMLInputElement>) => void;} & Omit<ComponentPropsWithoutRef<"input">, "size" | "type" | "onChange">>;export const CheckboxFieldInput = forwardRef<HTMLInputElement,CheckboxFieldInputProps>(function CheckboxFieldInput({className,containerClassName,id,label = "I agree to the terms and privacy policy",hint,error = false,errorMessage = "You must accept before continuing.",disabled,required,checked,defaultChecked = false,onCheckedChange,...props},ref,) {const generatedId = useId();const inputId = id ?? generatedId;const hintId = `${inputId}-hint`;const errorId = `${inputId}-error`;const isControlled = checked !== undefined;const [internal, setInternal] = useState(defaultChecked);const isChecked = isControlled ? checked : internal;const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {const next = event.target.checked;if (!isControlled) setInternal(next);onCheckedChange?.(next, event);},[isControlled, onCheckedChange],);return (<divdata-slot="checkbox-field-input"data-error={error || undefined}className={cn("w-full max-w-sm font-sans", containerClassName)}><labelhtmlFor={inputId}className={cn("flex cursor-pointer items-start gap-3",disabled && "cursor-not-allowed opacity-60",)}><span className="relative mt-0.5 shrink-0"><inputref={ref}id={inputId}type="checkbox"disabled={disabled}required={required}checked={isControlled ? isChecked : undefined}defaultChecked={isControlled ? undefined : defaultChecked}aria-invalid={error || undefined}aria-describedby={error ? errorId : hint ? hintId : undefined}onChange={handleChange}className={cn("peer absolute size-4 cursor-pointer opacity-0 disabled:cursor-not-allowed",className,)}{...props}/><spanaria-hiddenclassName={cn("flex size-4 items-center justify-center rounded border transition-[border-color,background-color] duration-200",error? "border-rose-300 bg-white peer-focus:border-rose-400": "border-neutral-300 bg-white peer-focus:border-neutral-900","peer-checked:border-neutral-900 peer-checked:bg-neutral-900","peer-disabled:border-neutral-200 peer-disabled:bg-neutral-100",)}><Checksize={12}strokeWidth={3}className={cn("text-white transition-opacity duration-150",isChecked ? "opacity-100" : "opacity-0",)}/></span></span><span className="min-w-0"><span className="block text-sm text-neutral-900">{label}{required ? (<span className="ml-0.5 text-rose-500" aria-hidden>*</span>) : null}</span>{hint ? (<span id={hintId} className="mt-0.5 block text-xs text-neutral-500">{hint}</span>) : null}</span></label>{error ? (<p id={errorId} role="alert" className="mt-1.5 text-xs text-rose-600">{errorMessage}</p>) : null}</div>);});CheckboxFieldInput.displayName = "CheckboxFieldInput";