Forms

Checkbox.

A square toggle for yes or no choices, with checked, indeterminate and disabled states.

Other players can find your site from the Lantern Hub front page.

Installation#

With the CLI

$npx lanterncn add checkbox

This also installs the Lantern theme and any Lantern UI components it depends on. The shadcn CLI works directly too: npx shadcn@latest add httptim/lantern-ui/checkbox. See Installation if your project is not set up yet.

Manually

  1. Install the dependencies

    npm install radix-ui lucide-react
  2. Copy the source into your project

    components/ui/checkbox.tsx
    "use client";
    
    import * as React from "react";
    import { Checkbox as CheckboxPrimitive } from "radix-ui";
    import { CheckIcon, MinusIcon } from "lucide-react";
    
    import { cn } from "@/lib/utils";
    
    function Checkbox({ className, ...props }: React.ComponentProps<typeof CheckboxPrimitive.Root>) {
      return (
        <CheckboxPrimitive.Root
          data-slot="checkbox"
          className={cn(
            "peer group/checkbox relative size-[18px] shrink-0 cursor-pointer rounded-sm border border-input bg-background/40 text-primary-foreground transition-[border-color,background-color,box-shadow] outline-none",
            "after:absolute after:-inset-2 hover:border-muted-foreground",
            "focus-visible:border-primary focus-visible:ring-2 focus-visible:ring-ring/25",
            "data-[state=checked]:border-primary data-[state=checked]:bg-primary data-[state=indeterminate]:border-primary data-[state=indeterminate]:bg-primary",
            "aria-invalid:border-destructive aria-invalid:ring-destructive/20 disabled:cursor-not-allowed disabled:opacity-50",
            className,
          )}
          {...props}
        >
          <CheckboxPrimitive.Indicator
            data-slot="checkbox-indicator"
            className="flex items-center justify-center text-current [&_svg]:size-3.5 [&_svg]:stroke-[3]"
          >
            <CheckIcon className="group-data-[state=indeterminate]/checkbox:hidden" />
            <MinusIcon className="hidden group-data-[state=indeterminate]/checkbox:block" />
          </CheckboxPrimitive.Indicator>
        </CheckboxPrimitive.Root>
      );
    }
    
    export { Checkbox };
  3. Update the import paths to match your project

    The source imports cn from @/lib/utils.

Usage#

import { Checkbox } from "@/components/ui/checkbox";
<div className="flex items-center gap-3">
  <Checkbox id="terms" />
  <Label htmlFor="terms">Accept the hub rules</Label>
</div>

Pass checked="indeterminate" to show a dash, for example on a select-all box when only some rows are checked.

Examples#

Disabled and indeterminate

Checklist

2/4