Skip to Content
Zuri
Components

Checkbox

A native checkbox, restyled, with an invalid state.

Live preview
Theme
Preview width

Use Checkbox for an independent yes/no choice that is submitted with a form, or for picking several items from a list. For a setting that applies immediately, use Switch. For choosing one of several options, use Radio group.

Installation

pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/checkbox.json

Usage

It has no built-in label. Pair it with a Label using htmlFor, or wrap both in a <label>.

import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";

<Checkbox id="terms" name="terms" />
<Label htmlFor="terms">I accept the terms</Label>

States

Live preview
Theme
Preview width

Props

Checkbox extends all native <input> props (except type) and spreads them onto the input, including ref (React 19). Use checked with onChange to control it, or defaultChecked to leave it uncontrolled.

Props
PropTypeDefaultDescription
invalidboolean–Sets aria-invalid and a danger border.
classNamestring–Merged onto the input; yours win on conflicts.

Accessibility

  • It is a native checkbox: Tab focuses it, Space toggles it, and screen readers announce name and state.
  • The box is 24 × 24 pixels, the WCAG 2.2 minimum target size. A label next to it makes the click target larger.
  • The tick is a graphic added on top, hidden from screen readers; the checked state is conveyed by the input itself, not by colour alone.

Tokens used

--background, --control-border, --primary, --primary-foreground, --danger, --ring, --duration-base, --ease-out.

Known limitations

  • No indeterminate (mixed) state yet: it needs the DOM property to be set from JavaScript.
  • Windows forced-colours mode has not been checked.

Source

import type { ComponentProps } from "react";import { cn } from "@/lib/utils";export type CheckboxProps = Omit<ComponentProps<"input">, "type" | "size"> & {  /** Marks the value as invalid for assistive technology and styles the border. */  invalid?: boolean;};/** * A native checkbox, restyled. Wrap it in a `<label>` (or pair it with htmlFor) so it has a name. * The 24px box meets the WCAG 2.2 minimum target size on its own. */export function Checkbox({ className, invalid, ...props }: CheckboxProps) {  return (    <span className="relative inline-flex size-6 shrink-0">      <input        type="checkbox"        aria-invalid={invalid || props["aria-invalid"] || undefined}        className={cn(          "peer size-6 cursor-pointer appearance-none rounded-md border border-control-border bg-background transition-[background-color,border-color] duration-[var(--duration-base)] ease-[var(--ease-out)] checked:border-primary checked:bg-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:cursor-not-allowed disabled:opacity-50 aria-[invalid=true]:border-danger motion-reduce:transition-none",          className,        )}        {...props}      />      {/* Sibling of the input so the checked state can show it without JavaScript. */}      <svg        aria-hidden="true"        viewBox="0 0 16 16"        fill="none"        stroke="currentColor"        strokeWidth="2"        strokeLinecap="round"        strokeLinejoin="round"        className="pointer-events-none absolute inset-0 m-auto hidden size-4 text-primary-foreground peer-checked:block"      >        <path d="M3.5 8.5l3 3 6-7" />      </svg>    </span>  );}

On this page