Activity heatmap
A year of daily activity as a grid of squares, revealed in a diagonal wave, with month labels, a legend, streak stats and a tooltip for each day.
"use client";
import {
useEffect,
useMemo,
useRef,
useState,
type CSSProperties,
} from "react";
import "./activity-heatmap.css";
const WEEKS = 53;
const DEFAULT_END = new Date(2026, 11, 5);
const MONTHS = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
/** Deterministic fake data (same on server and client). Replace with real counts per day. */
function makeData(end: Date) {
const days: { date: Date; count: number }[] = [];
let seed = 7;
const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const start = new Date(end);
start.setDate(end.getDate() - (WEEKS * 7 - 1) - end.getDay());
for (let i = 0; i < WEEKS * 7; i++) {
const d = new Date(start);
d.setDate(start.getDate() + i);
const weekend = d.getDay() === 0 || d.getDay() === 6;
const r = rand();
const count =
d > end
? -1
: r < (weekend ? 0.55 : 0.2)
? 0
: Math.round(r * (weekend ? 4 : 11));
days.push({ date: d, count });
}
return days;
}
const level = (c: number) =>
c <= 0 ? 0 : c < 3 ? 1 : c < 6 ? 2 : c < 9 ? 3 : 4;
/**
* ActivityHeatmap
* - 53 columns × 7 rows of buttons (one per day) in a CSS grid with grid-auto-flow: column.
* - Squares fade in with a delay based on (week + weekday), giving a diagonal wave.
*/
export function ActivityHeatmap({
theme = "light",
end = DEFAULT_END,
}: {
theme?: "light" | "dark";
end?: Date;
}) {
const days = useMemo(() => makeData(end), [end]);
const [tip, setTip] = useState<number | null>(null);
const scroller = useRef<HTMLDivElement>(null);
// Start scrolled to the most recent weeks on small screens.
useEffect(() => {
const el = scroller.current;
if (el) el.scrollLeft = el.scrollWidth;
}, []);
const valid = days.filter((d) => d.count >= 0);
const total = valid.reduce((s, d) => s + d.count, 0);
let best = 0;
let run = 0;
for (const d of valid) {
run = d.count > 0 ? run + 1 : 0;
best = Math.max(best, run);
}
let current = 0;
for (let i = valid.length - 1; i >= 0 && valid[i].count > 0; i--) current++;
const monthCols = days.reduce<{ col: number; label: string }[]>(
(acc, d, i) => {
if (d.date.getDate() <= 7 && d.date.getDay() === 0)
acc.push({ col: Math.floor(i / 7), label: MONTHS[d.date.getMonth()] });
return acc;
},
[],
);
const fmt = (d: Date) =>
d.toLocaleDateString("en-US", {
weekday: "short",
month: "short",
day: "numeric",
});
return (
<div className="gu-heat" data-theme={theme}>
<div className="gu-heat__stats">
<p>
<b>{total.toLocaleString("en-US")}</b> contributions in the last year
</p>
<p>
Longest streak <b>{best} days</b> · Current <b>{current} days</b>
</p>
</div>
<div className="gu-heat__scroll" ref={scroller} data-lenis-prevent>
<div className="gu-heat__months" aria-hidden="true">
{monthCols.map((m) => (
<span key={`${m.label}${m.col}`} style={{ gridColumn: m.col + 1 }}>
{m.label}
</span>
))}
</div>
<div className="gu-heat__grid" role="group" aria-label="Daily activity">
{days.map((d, i) =>
d.count < 0 ? (
<span key={i} className="gu-heat__cell gu-heat__cell--future" />
) : (
<button
key={i}
type="button"
className="gu-heat__cell"
data-l={level(d.count)}
style={{ "--d": Math.floor(i / 7) + (i % 7) } as CSSProperties}
aria-label={`${d.count || "No"} contributions on ${fmt(d.date)}`}
onMouseEnter={() => setTip(i)}
onFocus={() => setTip(i)}
onClick={() => setTip(i)}
onMouseLeave={() => setTip(null)}
onBlur={() => setTip(null)}
>
{tip === i && (
<span className="gu-heat__tip" role="tooltip">
<b>{d.count || "No"} contributions</b> {fmt(d.date)}
</span>
)}
</button>
),
)}
</div>
</div>
<div className="gu-heat__legend" aria-hidden="true">
Less
{[0, 1, 2, 3, 4].map((l) => (
<span key={l} className="gu-heat__cell" data-l={l} />
))}
More
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Activity heatmap
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A contribution-style activity heatmap for one year: 53 weeks × 7 days of small squares in five orange intensity levels, with month labels, total and streak stats, a legend and a tooltip per day. Squares appear in a diagonal wave.
## Layout and look
- Card 860px max, radius 24px, padding 22px.
- Stats: "1,734 contributions in the last year" (number big and bold) and "Longest streak 23 days · Current 4 days".
- Grid: 13px squares, 3px gaps, radius 3px, grid-auto-flow column with 7 rows. Colors from 7% Ink (empty) through peach to Signal orange #ff5a1f (dark mode uses deep rust tones).
- Month labels above the first column of each month; legend "Less ▢▢▢▢▢ More" bottom right.
- Tooltip: small Ink pill above the hovered square: "6 contributions Tue, Nov 4".
## Behavior and motion
1. Data: one count per day; levels 0, 1–2, 3–5, 6–8, 9+. Future days are blank.
2. Each square animates scale 0.3 → 1 and fades in with a delay of (week + weekday) × 12ms.
3. Streaks computed from consecutive non-zero days.
4. On small screens the grid scrolls horizontally and starts scrolled to the latest week.
## Accessibility
- Each day is a button labelled "6 contributions on Tue, Nov 4"; focus shows the tooltip.
- prefers-reduced-motion: no wave.
## Props
theme, end (the last date). Replace makeData with real counts.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### ActivityHeatmap.tsx
```tsx
"use client";
import {
useEffect,
useMemo,
useRef,
useState,
type CSSProperties,
} from "react";
import "./activity-heatmap.css";
const WEEKS = 53;
const DEFAULT_END = new Date(2026, 11, 5);
const MONTHS = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
/** Deterministic fake data (same on server and client). Replace with real counts per day. */
function makeData(end: Date) {
const days: { date: Date; count: number }[] = [];
let seed = 7;
const rand = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const start = new Date(end);
start.setDate(end.getDate() - (WEEKS * 7 - 1) - end.getDay());
for (let i = 0; i < WEEKS * 7; i++) {
const d = new Date(start);
d.setDate(start.getDate() + i);
const weekend = d.getDay() === 0 || d.getDay() === 6;
const r = rand();
const count =
d > end
? -1
: r < (weekend ? 0.55 : 0.2)
? 0
: Math.round(r * (weekend ? 4 : 11));
days.push({ date: d, count });
}
return days;
}
const level = (c: number) =>
c <= 0 ? 0 : c < 3 ? 1 : c < 6 ? 2 : c < 9 ? 3 : 4;
/**
* ActivityHeatmap
* - 53 columns × 7 rows of buttons (one per day) in a CSS grid with grid-auto-flow: column.
* - Squares fade in with a delay based on (week + weekday), giving a diagonal wave.
*/
export function ActivityHeatmap({
theme = "light",
end = DEFAULT_END,
}: {
theme?: "light" | "dark";
end?: Date;
}) {
const days = useMemo(() => makeData(end), [end]);
const [tip, setTip] = useState<number | null>(null);
const scroller = useRef<HTMLDivElement>(null);
// Start scrolled to the most recent weeks on small screens.
useEffect(() => {
const el = scroller.current;
if (el) el.scrollLeft = el.scrollWidth;
}, []);
const valid = days.filter((d) => d.count >= 0);
const total = valid.reduce((s, d) => s + d.count, 0);
let best = 0;
let run = 0;
for (const d of valid) {
run = d.count > 0 ? run + 1 : 0;
best = Math.max(best, run);
}
let current = 0;
for (let i = valid.length - 1; i >= 0 && valid[i].count > 0; i--) current++;
const monthCols = days.reduce<{ col: number; label: string }[]>(
(acc, d, i) => {
if (d.date.getDate() <= 7 && d.date.getDay() === 0)
acc.push({ col: Math.floor(i / 7), label: MONTHS[d.date.getMonth()] });
return acc;
},
[],
);
const fmt = (d: Date) =>
d.toLocaleDateString("en-US", {
weekday: "short",
month: "short",
day: "numeric",
});
return (
<div className="gu-heat" data-theme={theme}>
<div className="gu-heat__stats">
<p>
<b>{total.toLocaleString("en-US")}</b> contributions in the last year
</p>
<p>
Longest streak <b>{best} days</b> · Current <b>{current} days</b>
</p>
</div>
<div className="gu-heat__scroll" ref={scroller} data-lenis-prevent>
<div className="gu-heat__months" aria-hidden="true">
{monthCols.map((m) => (
<span key={`${m.label}${m.col}`} style={{ gridColumn: m.col + 1 }}>
{m.label}
</span>
))}
</div>
<div className="gu-heat__grid" role="group" aria-label="Daily activity">
{days.map((d, i) =>
d.count < 0 ? (
<span key={i} className="gu-heat__cell gu-heat__cell--future" />
) : (
<button
key={i}
type="button"
className="gu-heat__cell"
data-l={level(d.count)}
style={{ "--d": Math.floor(i / 7) + (i % 7) } as CSSProperties}
aria-label={`${d.count || "No"} contributions on ${fmt(d.date)}`}
onMouseEnter={() => setTip(i)}
onFocus={() => setTip(i)}
onClick={() => setTip(i)}
onMouseLeave={() => setTip(null)}
onBlur={() => setTip(null)}
>
{tip === i && (
<span className="gu-heat__tip" role="tooltip">
<b>{d.count || "No"} contributions</b> {fmt(d.date)}
</span>
)}
</button>
),
)}
</div>
</div>
<div className="gu-heat__legend" aria-hidden="true">
Less
{[0, 1, 2, 3, 4].map((l) => (
<span key={l} className="gu-heat__cell" data-l={l} />
))}
More
</div>
</div>
);
}
```
### activity-heatmap.css
```css
/* ActivityHeatmap: change these colors to match your brand. */
.gu-heat {
--card: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.1);
--l0: rgb(14 14 16 / 0.07);
--l1: #ffd2bf;
--l2: #ffa27e;
--l3: #ff7a45;
--l4: #ff5a1f;
--tip: #0e0e10;
--tip-text: #f3eee3;
--focus: #2b35ff;
--size: 13px;
--gap: 3px;
width: min(100%, 860px);
padding: 22px;
border: 1px solid var(--line);
border-radius: 24px;
background: var(--card);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-heat[data-theme="dark"] {
--card: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.1);
--l0: rgb(243 238 227 / 0.07);
--l1: #5a2a17;
--l2: #8f3a17;
--l3: #c9481a;
--l4: #ff5a1f;
--tip: #f3eee3;
--tip-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-heat__stats {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 6px 20px;
margin-bottom: 16px;
}
.gu-heat__stats p {
margin: 0;
color: var(--muted);
font-size: 0.9rem;
}
.gu-heat__stats b {
color: var(--text);
}
.gu-heat__stats p:first-child b {
font-size: 1.3rem;
font-weight: 900;
letter-spacing: -0.02em;
}
.gu-heat__scroll {
/* Size from the card, not from the 53-week grid, so the card never grows wider than its box */
contain: inline-size;
overflow-x: auto;
padding: 40px 2px 4px;
margin-top: -36px;
scrollbar-width: thin;
}
.gu-heat__months,
.gu-heat__grid {
display: grid;
grid-template-columns: repeat(53, var(--size));
gap: var(--gap);
width: max-content;
}
.gu-heat__months {
margin-bottom: 6px;
color: var(--muted);
font-size: 0.72rem;
font-weight: 600;
}
.gu-heat__months span {
white-space: nowrap;
}
.gu-heat__grid {
grid-template-rows: repeat(7, var(--size));
grid-auto-flow: column;
}
.gu-heat__cell {
position: relative;
width: var(--size);
height: var(--size);
padding: 0;
border: 0;
border-radius: 3px;
background: var(--l0);
cursor: pointer;
animation: gu-heat-in 0.5s calc(var(--d, 0) * 12ms) backwards;
}
@keyframes gu-heat-in {
from {
opacity: 0;
transform: scale(0.3);
}
}
.gu-heat__cell[data-l="1"] {
background: var(--l1);
}
.gu-heat__cell[data-l="2"] {
background: var(--l2);
}
.gu-heat__cell[data-l="3"] {
background: var(--l3);
}
.gu-heat__cell[data-l="4"] {
background: var(--l4);
}
.gu-heat__cell--future {
background: none;
cursor: default;
}
.gu-heat__cell:hover {
outline: 2px solid var(--text);
outline-offset: 1px;
}
.gu-heat__cell:focus-visible {
z-index: 2;
outline: 2px solid var(--focus);
outline-offset: 1px;
}
.gu-heat__tip {
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
z-index: 3;
padding: 6px 10px;
border-radius: 8px;
background: var(--tip);
color: var(--tip-text);
font-size: 0.75rem;
white-space: nowrap;
translate: -50% 0;
pointer-events: none;
}
.gu-heat__legend {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 4px;
margin-top: 12px;
color: var(--muted);
font-size: 0.75rem;
}
.gu-heat__legend .gu-heat__cell {
animation: none;
cursor: default;
}
.gu-heat__legend .gu-heat__cell:hover {
outline: none;
}
@media (prefers-reduced-motion: reduce) {
.gu-heat__cell {
animation: none;
}
}
```
You might also want
Next piece: Activity ringsActivity rings
Components, intermediate
Stat cards with sparklines
Components, intermediate
Avatar stack
Components, beginner