Checkbox

Ink that fills from the centre.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/checkbox.json

This copies the component into components/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/checkbox.tsx
"use client";

import * as React from "react";
import "./checkbox.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

export type CheckboxProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, "type" | "size"> & {
  /** Called when it's checked or unchecked. */
  onCheckedChange?: (checked: boolean) => void;
  /** "some of these": a dash instead of the tick */
  indeterminate?: boolean;
  /** a round one, for pills */
  round?: boolean;
  /** 22px (default) or 18px, for dense lists and tables */
  size?: "md" | "sm";
components/fabricui/checkbox.css
/*
 * Checkbox — shared by every page with a checkbox (checkbox.html, tables…).
 * The ink spreads from the centre inside the box, the tick draws itself in.
 * Indeterminate (input.indeterminate = true): a dash, for "some of these".
 *
 *   <span class="gc"><input class="gc__input" type="checkbox" /><span class="gc__box"><span class="gc__ink"></span></span>
 *     <svg class="gc__tick" viewBox="0 0 24 24" aria-hidden="true"><path pathLength="1" d="M5 14L8.5 17.5L19 6.5" /></svg><span class="gc__dash"></span></span>
 *
 * --round: a round one, for pills.  --sm: 18px, for dense lists and tables.
 */
.gc.gc--round:where(.fui-checkbox, .fui-checkbox *) { width: 18px; height: 18px; }   /* (two classes: beats the square box's 22px) */
.gc--round .gc__box:where(.fui-checkbox, .fui-checkbox *) { border-radius: 50%; background: var(--fabric-check-round); box-shadow: var(--fabric-check-round-edge); }
/* a touch larger than the circle, so the fill covers the ring to its very edge */
.gc--round .gc__ink:where(.fui-checkbox, .fui-checkbox *) { width: 20px; height: 20px; margin: -10px 0 0 -10px; }
.gc--round .gc__input:checked ~ .gc__box:where(.fui-checkbox, .fui-checkbox *) { box-shadow: inset 0 0 0 1.5px var(--fabric-text); }
.gc--round .gc__tick:where(.fui-checkbox, .fui-checkbox *) { left: 3px; top: 3px; width: 12px; height: 12px; stroke-width: 2.8; }

Usage

Usage
"use client";

import * as React from "react";
import { Checkbox } from "@/components/fabricui/checkbox";

export function Terms() {
  const [agreed, setAgreed] = React.useState(false);
  return (
    <label style={{ display: "flex", alignItems: "center", gap: 12 }}>
      <Checkbox checked={agreed} onCheckedChange={setAgreed} />
      I agree to the terms
    </label>
  );
}

Props

PropTypeDefault
checked

Checked or not, if you control it.

boolean—
defaultChecked

The starting state when you don't.

booleanfalse
onCheckedChange

Called when it's checked or unchecked.

(checked: boolean) => void—
indeterminate

"Some of these": a dash instead of the tick.

booleanfalse
round

A round one, 18px, for pills.

booleanfalse
size

22px, or 18px for dense lists and tables.

"md" | "sm""md"