Hatch Grid
A year of days as hand-drawn squares, one column a week, shaded in five levels from an empty box to solid ink.
639 commits in 2026
import { createRng, hashSeed } from "@/lib/ink-sketch";
import { HatchGrid, type HatchGridDay } from "@/components/ui/hatch-grid";
// A made-up year of commits: busier midweek, with a few long weekends off.
const r = createRng(hashSeed("hatch-grid-demo"));
const days: HatchGridDay[] = Array.from({ length: 365 }, (_, i) => {
const date = new Date(Date.UTC(2026, 0, 1 + i)).toISOString().slice(0, 10);
const weekday = new Date(`${date}T00:00:00Z`).getUTCDay();
const busy = weekday === 0 || weekday === 6 ? 0.25 : 0.8;
return { date, count: r() < busy ? Math.round(r() ** 2 * 12) : 0 };
});
const total = days.filter((d) => d.date <= "2026-10-06").reduce((sum, d) => sum + d.count, 0);
export default function HatchGridDemo() {
return (
<div className="flex w-full max-w-full flex-col gap-2">
<HatchGrid
data={days}
today="2026-10-06"
unit="commit"
summary={`${total} commits in 2026`}
/>
<p className="text-sm text-ink-3">
<span className="font-bold text-ink">{total}</span> commits in 2026
</p>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ hatch-grid"use client";
import { memo, useId, useLayoutEffect, useMemo, useRef, useState, type CSSProperties, type PointerEvent } from "react";
import { createPortal } from "react-dom";
import { cn } from "@/lib/utils";
import { penStyle, useInkBox, useInkStage, usePen, type Pen } from "@/hooks/use-ink-box";
import { Stroke } from "@/lib/ink";
import { boxStroke, hashSeed, hatchStrokes, loopStroke } from "@/lib/ink-sketch";
export type HatchGridDay = {
/** YYYY-MM-DD. */
date: string;
count: number;
/** 0–4. Worked out from count when left out: none, then quarters of the busiest day. */
level?: 0 | 1 | 2 | 3 | 4;
};
type Level = NonNullable<HatchGridDay["level"]>;
type Cell = HatchGridDay & { level: Level };
const CELL = 11.5;
const PITCH = 14.5;
// How far in from an edge weeks scrolled past it fade out.
const FADE = 24;
// Each level is drawn three ways, so the grid never looks stamped.
const VARIANTS = 3;
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const utcDay = (date: string) => new Date(`${date}T00:00:00Z`).getUTCDay();
/** "Oct 6th, 2026". */
function longDate(date: string) {
const d = new Date(`${date}T00:00:00Z`);
const n = d.getUTCDate();
const th = n % 10 === 1 && n !== 11 ? "st" : n % 10 === 2 && n !== 12 ? "nd" : n % 10 === 3 && n !== 13 ? "rd" : "th";
return `${MONTHS[d.getUTCMonth()]} ${n}${th}, ${d.getUTCFullYear()}`;
}
/**
* Cell stamps, shaded the way you'd shade squares on graph paper: an empty
* box, single hatching, denser hatching, cross-hatching, solid ink. And a
* dotted box for days still to come.
*/
const Defs = memo(function Defs({ id }: { id: string }) {
const out = [];
for (let v = 0; v < VARIANTS; v++) {
const s = hashSeed(`hatch-cell-${v}`);
const box = boxStroke(s, CELL, CELL, { overshoot: 0.9, jitter: 0.45, bow: 0.5 });
const light = hatchStrokes(s + 1, CELL, CELL, { gap: 4.4, angle: -45, jitter: 0.35, inset: 0.8 }).join("");
const dense = hatchStrokes(s + 2, CELL, CELL, { gap: 2.5, angle: -45, jitter: 0.3, inset: 0.6 }).join("");
const cross = hatchStrokes(s + 3, CELL, CELL, { gap: 2.7, angle: 45, jitter: 0.3, inset: 0.6 }).join("");
// Inline styles, not classes: page CSS doesn't reach the copies <use>
// makes in every browser, and where it does, these still win.
const w = (n: number) => ({ style: { fill: "none", stroke: "currentColor", strokeWidth: n, strokeLinecap: "round", strokeLinejoin: "round" } as CSSProperties });
out.push(
<g key={`f${v}`} id={`${id}-f-${v}`}>
<path d={box} {...w(0.8)} strokeDasharray="1.2 2.2" opacity={0.3} />
</g>,
<g key={`0${v}`} id={`${id}-0-${v}`}>
<path d={box} {...w(0.85)} opacity={0.38} />
</g>,
<g key={`1${v}`} id={`${id}-1-${v}`}>
<path d={box} {...w(0.9)} opacity={0.8} />
<path d={light} {...w(0.8)} opacity={0.75} />
</g>,
<g key={`2${v}`} id={`${id}-2-${v}`}>
<path d={box} {...w(0.9)} />
<path d={dense} {...w(0.85)} opacity={0.85} />
</g>,
<g key={`3${v}`} id={`${id}-3-${v}`}>
<path d={box} {...w(0.95)} />
<path d={dense} {...w(0.9)} />
<path d={cross} {...w(0.85)} opacity={0.85} />
</g>,
<g key={`4${v}`} id={`${id}-4-${v}`}>
<path d={`${boxStroke(s + 4, CELL, CELL, { overshoot: 0, jitter: 0.5, bow: 0.5 })}Z`} style={{ fill: "currentColor", stroke: "none" }} opacity={0.92} />
<path d={box} {...w(1)} />
</g>,
);
}
return <defs>{out}</defs>;
});
/** The columns, memoised: hovering only redraws the loop and the label, never the cells. */
const Columns = memo(function Columns({ id, weeks, today, animate }: { id: string; weeks: (Cell | null)[][]; today?: string; animate: boolean }) {
return weeks.map((week, wi) => (
// Each column lands as the pen sweeps across, left to right.
<g key={wi} className={animate ? "ink-land" : undefined} transform={`translate(${wi * PITCH} 0)`} style={{ "--ink-d": "320ms", "--ink-dd": `${wi * 14}ms` } as CSSProperties}>
{week.map((day, di) => {
if (!day) return null;
const v = hashSeed(day.date) % VARIANTS;
return <use key={day.date} href={`#${id}-${today && day.date > today ? "f" : day.level}-${v}`} y={di * PITCH} />;
})}
</g>
));
});
/**
* A year (or any run of days) as a grid of hand-drawn squares, one column a
* week, shaded in five levels from an empty box to solid ink. Hovering a
* day circles it and says what it holds. The columns land left to right as
* it scrolls into view.
*/
function HatchGrid({
data,
today,
unit = "contribution",
label,
summary,
className,
draw,
speed,
}: Pick<Pen, "draw" | "speed"> & {
data: HatchGridDay[];
/** YYYY-MM-DD: days after it are drawn dotted, still to come. */
today?: string;
/** What's counted, for the hover label: "3 commits on Oct 6th, 2026". Plural adds an s; pass [one, many] otherwise. */
unit?: string | [string, string];
/** What hovering a day says, in place of the unit's sentence. A function, so only from a client component. */
label?: (day: HatchGridDay) => string;
/** What the grid holds, for screen readers (it's one image to them). */
summary?: string;
className?: string;
}) {
const pen = usePen({ draw, speed });
const id = `hg${useId().replace(/[^\w-]/g, "")}`;
const mode = pen.draw ?? "auto";
const [ref] = useInkBox([800, 120]);
const monthRow = useInkStage({ draw: mode });
const [hover, setHover] = useState<{ wi: number; di: number; text: string; x: number; y: number } | null>(null);
const scroller = useRef<HTMLDivElement>(null);
const [overflows, setOverflows] = useState(false);
const { weeks, months } = useMemo(() => {
const days = [...data].sort((a, b) => (a.date < b.date ? -1 : 1));
const max = Math.max(1, ...days.map((d) => d.count));
const level = (d: HatchGridDay) => d.level ?? ((d.count <= 0 ? 0 : Math.min(4, Math.ceil((d.count / max) * 4))) as Level);
const weeks: (Cell | null)[][] = [];
let week: (Cell | null)[] = Array.from({ length: days[0] ? utcDay(days[0].date) : 0 }, () => null);
for (const day of days) {
week.push({ ...day, level: level(day) });
if (week.length === 7) {
weeks.push(week);
week = [];
}
}
if (week.length) weeks.push([...week, ...Array.from({ length: 7 - week.length }, () => null)]);
// A month's name over the week it starts in, if there's room for it.
const months: { index: number; label: string }[] = [];
let last = -1;
weeks.forEach((w, i) => {
const first = w.find(Boolean);
if (!first) return;
const month = new Date(`${first.date}T00:00:00Z`).getUTCMonth();
if (month === last) return;
last = month;
const prev = months[months.length - 1];
if (!prev || i - prev.index >= 3) months.push({ index: i, label: MONTHS[month] });
});
return { weeks, months };
}, [data]);
// Starts scrolled to the latest weeks when the grid is wider than its box:
// with `today`, today's week sits at the right edge with a couple of the
// weeks still to come after it, rather than a run of blank ones. Then
// it's a stop for the keyboard too, so arrow keys can scroll it. There's
// no scrollbar: weeks scrolled out of sight fade at the edge they left by.
const latest = today ? weeks.findIndex((w) => w.some((d) => d && d.date >= today)) : -1;
useLayoutEffect(() => {
const el = scroller.current;
if (!el) return;
el.scrollLeft = latest < 0 ? el.scrollWidth : parseFloat(getComputedStyle(el).paddingLeft) + (latest + 3) * PITCH - el.clientWidth;
const fade = () => {
const rest = el.scrollWidth - el.clientWidth - el.scrollLeft;
el.style.setProperty("--fade-start", `${Math.min(FADE, el.scrollLeft)}px`);
el.style.setProperty("--fade-end", `${Math.min(FADE, Math.max(0, rest))}px`);
};
const check = () => {
setOverflows(el.scrollWidth > el.clientWidth + 1);
fade();
};
check();
el.addEventListener("scroll", fade, { passive: true });
const observer = new ResizeObserver(check);
observer.observe(el);
return () => {
el.removeEventListener("scroll", fade);
observer.disconnect();
};
}, [latest]);
const width = Math.max(1, weeks.length * PITCH - (PITCH - CELL));
const height = 7 * PITCH - (PITCH - CELL);
const loop = useMemo(() => loopStroke(hashSeed("hatch-loop"), CELL, CELL, { pad: 3.5, turns: 1.15 }), []);
function onMove(e: PointerEvent<HTMLDivElement>) {
if (e.pointerType === "touch") return;
const r = e.currentTarget.getBoundingClientRect();
const wi = Math.floor((e.clientX - r.left) / PITCH);
const di = Math.floor((e.clientY - r.top) / PITCH);
const day = weeks[wi]?.[di];
if (!day || (today && day.date > today)) return setHover(null);
const [one, many] = typeof unit === "string" ? [unit, `${unit}s`] : unit;
const text = label ? label(day) : `${day.count === 0 ? "No" : day.count} ${day.count === 1 ? one : many} on ${longDate(day.date)}`;
// The label floats over the page (a portal), clear of the grid's scroll box.
const x = Math.min(Math.max(r.left + wi * PITCH + CELL / 2, 110), window.innerWidth - 110);
const y = Math.max(8, r.top + di * PITCH - 8);
setHover((prev) => (prev?.wi === wi && prev.di === di ? prev : { wi, di, text, x, y }));
}
return (
<div
ref={scroller}
data-slot="hatch-grid"
tabIndex={overflows ? 0 : undefined}
role={overflows ? "region" : undefined}
aria-label={overflows ? (summary ?? "Days") : undefined}
onScroll={() => setHover(null)}
className={cn(
"max-w-full overflow-x-auto overflow-y-hidden px-1 pt-1 pb-2 outline-none [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
// The fade is a mask, which would hide the focus ring with it.
overflows && "[mask-image:linear-gradient(to_right,transparent,#000_var(--fade-start),#000_calc(100%_-_var(--fade-end)),transparent)] focus-visible:[mask-image:none]",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-2 focus-visible:outline-ring",
className,
)}
style={penStyle(pen)}
>
<div className="w-max">
{/* The months are written in, then the weeks land under them. */}
<div aria-hidden="true" {...monthRow} className="relative mb-2 h-4 text-xs leading-none text-ink-3">
{months.map(({ index, label }) => (
<span
key={index}
className={cn("absolute top-0", mode !== "none" && "ink-land")}
style={{ left: index * PITCH, "--ink-d": "320ms", "--ink-dd": `${index * 14}ms` } as CSSProperties}
>
{label}
</span>
))}
</div>
<div className="relative" onPointerMove={onMove} onPointerLeave={() => setHover(null)}>
<svg
ref={ref}
role="img"
aria-label={summary}
aria-hidden={summary ? undefined : true}
data-ink-pending={mode === "auto" ? "" : undefined}
width={width}
height={height}
viewBox={`0 0 ${width} ${height}`}
className="ink-sketch block text-ink"
>
<Defs id={id} />
<Columns id={id} weeks={weeks} today={today} animate={mode !== "none"} />
</svg>
{hover && (
<>
<svg aria-hidden="true" viewBox={`-6 -6 ${CELL + 12} ${CELL + 12}`} className="ink-sketch pointer-events-none absolute text-ink" style={{ left: hover.wi * PITCH - 6, top: hover.di * PITCH - 6, width: CELL + 12, height: CELL + 12 }}>
<Stroke d={loop} draw="mount" duration={260} width={1.3} />
</svg>
{createPortal(
<div
aria-hidden="true"
data-slot="hatch-grid-label"
className="pointer-events-none fixed z-50 -translate-x-1/2 -translate-y-full bg-primary px-2.5 py-0.5 text-sm whitespace-nowrap text-primary-foreground [border-radius:var(--hand-radius)]"
style={{ left: hover.x, top: hover.y }}
>
{hover.text}
</div>,
document.body,
)}
</>
)}
</div>
</div>
</div>
);
}
export { HatchGrid };Usage
import { HatchGrid } from "@/components/ui/hatch-grid"
<HatchGrid
data={days} // { date: "2026-01-01", count: 3 }[]
today="2026-10-06"
summary="1,203 commits in 2026"
/>Examples
Your own levels and labels
Pass level per day to set the shading yourself, and label to write the hover line.
"use client";
import { HatchGrid, type HatchGridDay } from "@/components/ui/hatch-grid";
// Twelve weeks of pages written: explicit levels, plus a custom hover line.
const pages = [0, 2, 5, 3, 0, 0, 1, 4, 6, 2, 0, 3, 1, 0, 7, 8, 2, 0, 0, 1, 3, 5, 0, 0];
const days: HatchGridDay[] = Array.from({ length: 84 }, (_, i) => {
const count = pages[(i * 7) % pages.length] ?? 0;
return {
date: new Date(Date.UTC(2026, 6, 13 + i)).toISOString().slice(0, 10),
count,
level: count === 0 ? 0 : count < 3 ? 1 : count < 5 ? 2 : count < 7 ? 3 : 4,
};
});
export default function HatchGridCustom() {
return (
<HatchGrid
data={days}
today="2026-10-03"
unit={["page", "pages"]}
label={(day) => (day.count === 0 ? `A day off, ${day.date}` : `${day.count} written on ${day.date}`)}
summary="Pages written over twelve weeks"
/>
);
}Several habits
The same half-year for three habits, each with its own unit and total.
import { createRng, hashSeed } from "@/lib/ink-sketch";
import { HatchGrid, type HatchGridDay } from "@/components/ui/hatch-grid";
// Three made-up habits over the same half-year, each with its own rhythm.
const habits = [
{ name: "Write a page", chance: 0.8, peak: 4, unit: "page" },
{ name: "Draw something", chance: 0.4, peak: 3, unit: "drawing" },
{ name: "Read in bed", chance: 0.6, peak: 2, unit: ["chapter", "chapters"] as [string, string] },
];
function year(name: string, chance: number, peak: number): HatchGridDay[] {
const r = createRng(hashSeed(name));
return Array.from({ length: 182 }, (_, i) => {
const date = new Date(Date.UTC(2026, 3, 1 + i)).toISOString().slice(0, 10);
return { date, count: r() < chance ? 1 + Math.floor(r() ** 2 * peak) : 0 };
});
}
export default function HatchGridHabits() {
return (
<div className="flex w-full flex-col gap-8">
{habits.map(({ name, chance, peak, unit }) => {
const data = year(name, chance, peak);
const total = data.filter((d) => d.date <= "2026-09-30").reduce((n, d) => n + d.count, 0);
return (
<div key={name} className="flex flex-col gap-2">
<div className="flex items-baseline justify-between">
<span className="font-bold">{name}</span>
<span className="text-sm text-ink-3 tabular-nums">{total} since April</span>
</div>
<HatchGrid data={data} today="2026-09-30" unit={unit} summary={`${name}: ${total} since April`} />
</div>
);
})}
</div>
);
}API reference
- data
{ date: string; count: number; level?: 0 | 1 | 2 | 3 | 4 }[]One entry a day. Level is worked out from count when left out: none, then quarters of the busiest day.
- today
stringDays after it are drawn dotted, still to come. Where the grid is wider than its box, it opens on this week.
- unitdefault
"contribution" string | [string, string]What's counted, for the hover label ("3 commits on Oct 6th, 2026"). The plural adds an s; pass [one, many] when it doesn't.
- label
(day) => stringWhat hovering a day says, in place of the unit's sentence. A function, so pass it from a client component.
- summary
stringWhat the grid shows, for screen readers; to them it's one image.
Pen settings
The drawing also takes draw and speed, as props or from the nearest InkProvider. What each one does.