Bloom Logo

Checkbox

A control that allows the user to toggle between checked and unchecked states, powered by Radix UI for ARIA accessibility.

checkbox.tsx

Core implementation of the Checkbox component built on Radix UI.

ReactRadix UITailwindForms
"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";
import { designRadius } from "@/lib/design-system";

export interface CheckboxProps
  extends React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root> {
  color?: "default" | "primary" | "secondary" | "accent" | "success" | "warning" | "danger";
  radius?: keyof typeof designRadius;
  label?: React.ReactNode;
  description?: React.ReactNode;
  isInvalid?: boolean;
}

const colorMap = {
  default: "data-[state=checked]:bg-foreground data-[state=checked]:text-background border-input",
  primary: "data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground data-[state=checked]:border-primary border-input",
  secondary: "data-[state=checked]:bg-secondary data-[state=checked]:text-secondary-foreground data-[state=checked]:border-secondary border-input",
  accent: "data-[state=checked]:bg-accent data-[state=checked]:text-accent-foreground data-[state=checked]:border-accent border-input",
  success: "data-[state=checked]:bg-success data-[state=checked]:text-success-foreground data-[state=checked]:border-success border-input",
  warning: "data-[state=checked]:bg-warning data-[state=checked]:text-warning-foreground data-[state=checked]:border-warning border-input",
  danger: "data-[state=checked]:bg-danger data-[state=checked]:text-danger-foreground data-[state=checked]:border-danger border-input",
};

const Checkbox = React.forwardRef<
  React.ComponentRef<typeof CheckboxPrimitive.Root>,
  CheckboxProps
>(({ className, color = "primary", radius = "md", label, description, isInvalid, id, disabled, ...props }, ref) => {
  const generatedId = React.useId();
  const checkboxId = id || generatedId;

  return (
    <div className="inline-flex items-start gap-2.5">
      <CheckboxPrimitive.Root
        ref={ref}
        id={checkboxId}
        disabled={disabled}
        className={cn(
          "peer size-4 shrink-0 border transition-all duration-200 outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 cursor-pointer mt-0.5",
          designRadius[radius],
          colorMap[color],
          isInvalid && "border-danger",
          className
        )}
        {...props}
      >
        <CheckboxPrimitive.Indicator className={cn("flex items-center justify-center text-current")}>
          <Check className="size-3 stroke-[3]" />
        </CheckboxPrimitive.Indicator>
      </CheckboxPrimitive.Root>
      {(label || description) && (
        <div className="flex flex-col gap-0.5 select-none">
          {label && (
            <label
              htmlFor={checkboxId}
              className={cn(
                "text-sm font-medium leading-none cursor-pointer peer-disabled:cursor-not-allowed peer-disabled:opacity-70",
                isInvalid && "text-danger"
              )}
            >
              {label}
            </label>
          )}
          {description && (
            <p className="text-xs text-muted-foreground">{description}</p>
          )}
        </div>
      )}
    </div>
  );
});
Checkbox.displayName = CheckboxPrimitive.Root.displayName;

export { Checkbox };

Basic Usage

Standard checkbox with label.

Colors

Supports primary, secondary, accent, success, warning, and danger themes.

color: default | primary | secondary | accent | success | warning | danger

With Description

Provide additional context below the checkbox label.

description: ReactNode

Receive instant alerts on your desktop or mobile device.

API Reference

Props — Checkbox

Properties to configure the Checkbox component.

PropTypeDefaultDescription
color'default' | 'primary' | 'secondary' | 'accent' | 'success' | 'warning' | 'danger''primary'Color palette when checked.
labelReactNodeClickable label content.
descriptionReactNodeSecondary text below the label.