Activity Heatmap
Plots daily counts across up to twelve weeks with a five-step intensity scale and a readable data list.
pnpm dlx shadcn@latest add @haze/activity-heatmap---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Sample { date: string; count: number }
interface Props {
label?: string;
unit?: string;
/** Singular form of unit, used when a count is 1. Defaults to unit without a trailing "s" (not for -ss or -us). */
unitSingular?: string;
samples?: Sample[];
end?: string;
weeks?: number;
maxCount?: number;
class?: string;
}
const {
label = "Deployment activity", unit = "deployments", unitSingular, end, weeks = 8, maxCount,
samples = [], class: className,
} = Astro.props;
const countByDate = new Map(samples.map(({ date, count }) => [date, Number.isFinite(count) ? Math.max(0, count) : 0]));
const day = 86_400_000;
const newest = end ?? Array.from(countByDate.keys()).sort().at(-1);
const parsed = Date.parse(`${newest}T00:00:00Z`);
const last = Number.isFinite(parsed) ? parsed : Math.floor(Date.now() / day) * day;
const weekCount = Number.isFinite(weeks) ? Math.max(1, Math.min(12, Math.round(weeks))) : 8;
const mondayOffset = (new Date(last).getUTCDay() + 6) % 7;
const first = last - (mondayOffset + (weekCount - 1) * 7) * day;
const dates = Array.from({ length: weekCount * 7 }, (_, index) => {
const stamp = first + index * day;
const iso = new Date(stamp).toISOString().slice(0, 10);
return { stamp, iso, count: countByDate.get(iso) ?? 0, future: stamp > last };
});
const peak = maxCount && Number.isFinite(maxCount) && maxCount > 0 ? maxCount : Math.max(1, ...dates.filter(({ future }) => !future).map(({ count }) => count));
const dateLabel = new Intl.DateTimeFormat("en", { month: "short", day: "numeric", timeZone: "UTC" });
const colors = ["bg-foreground/8", "bg-brand/24", "bg-brand/42", "bg-brand/64", "bg-brand"];
const numberLabel = new Intl.NumberFormat("en");
const singular = unitSingular ?? (/[^su]s$/.test(unit) ? unit.slice(0, -1) : unit);
const unitFor = (count: number) => (count === 1 ? singular : unit);
---
<figure class={cn("grid min-w-0 gap-3 rounded-xl border border-border bg-card p-4.5 text-card-foreground", className)}>
<figcaption class="grid gap-0.5">
<span class="text-[0.9375rem]/5 font-medium tracking-[-0.01em]">{label}</span>
<span class="text-[0.8125rem]/5 text-muted-foreground">{dateLabel.format(first)} to {dateLabel.format(last)}</span>
</figcaption>
<div aria-hidden="true" class="grid grid-cols-[auto_minmax(0,1fr)] gap-2">
<div class="grid grid-rows-7 gap-1 text-xs/4 text-muted-foreground">
<span>Mon</span><span class="row-start-3">Wed</span><span class="row-start-5">Fri</span><span class="row-start-7">Sun</span>
</div>
<div class="grid auto-cols-fr grid-flow-col grid-rows-7 gap-1">
{dates.map(({ stamp, count, future }) => (
<div title={future ? undefined : `${dateLabel.format(stamp)} · ${numberLabel.format(count)} ${unitFor(count)}`}
class={cn("min-h-4 min-w-0 rounded-sm", future ? "bg-transparent" : colors[Math.min(4, Math.ceil(Math.min(1, count / peak) * 4))])}></div>
))}
</div>
</div>
<div aria-hidden="true" class="flex min-w-0 flex-wrap items-center justify-end gap-1 text-xs/4 text-muted-foreground">
<span class="me-1">Less</span>
{colors.map((color) => <span class={cn("size-2.5 shrink-0 rounded-sm", color)}></span>)}
<span class="ms-1">More</span>
</div>
<details class="group border-t border-border pt-2">
<summary class="flex min-h-7 cursor-pointer list-none items-center justify-between gap-2 rounded-sm text-[0.8125rem]/5 font-medium text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">
View daily counts
<ChevronDown aria-hidden="true" class="size-3.5 transition-[rotate] duration-200 ease-snappy group-open:rotate-180" />
</summary>
<div role="region" aria-label={`Daily ${unit}`} tabindex="0"
class="mt-2 max-h-40 overflow-auto rounded-md border border-frame px-2 py-1 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring">
<dl class="divide-y divide-border text-[0.8125rem]/5">
{dates.filter(({ future }) => !future).reverse().map(({ stamp, iso, count }) => (
<div class="flex min-w-0 flex-wrap items-start justify-between gap-x-2 py-1">
<dt><time datetime={iso} class="text-muted-foreground">{dateLabel.format(stamp)}</time></dt>
<dd class="wrap-anywhere text-end tabular-nums">{numberLabel.format(count)} {unitFor(count)}</dd>
</div>
))}
</dl>
</div>
</details>
</figure>When to use
- Daily activity varies enough to show trends by weekday.
- You need a compact view plus the option to read exact daily counts.
When not to use
- Events are best understood in chronological detail with descriptions.
Installation
Add the Haze registry to components.json once, then install Activity Heatmap with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/activity-heatmapnpx shadcn@latest add @haze/activity-heatmapyarn dlx shadcn@latest add @haze/activity-heatmapbunx --bun shadcn@latest add @haze/activity-heatmapFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | Deployment activity | Title for the chart. |
| unit | string | deployments | Name of the measured activity. |
| unitSingular | string | – | Singular form of unit, used for a count of 1. Defaults to unit without a trailing "s" (not for -ss or -us). Pass it for irregular plurals such as "series". |
| samples | Sample[] | [] | Dated counts. Missing dates are bucketed as zero. |
| end | string | – | Last day to display in YYYY-MM-DD format. |
| weeks | number | 8 | Number of Monday-aligned weeks, clamped to 1–12. |
| maxCount | number | – | Optional positive input for the darkest color band. |
| class | string | – | Classes for the chart panel. |
Keyboard interactions
- EnterSpace
- Expand or collapse the daily data.
- Tab
- Focus the scrollable list.
- Arrow keys
- Scroll the list of counts.
Accessibility
- The disclosure includes each date and count in text.
- Future cells in the final week are blank and excluded from the count list.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Activity Heatmap" with `npx shadcn@latest add @haze/activity-heatmap`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import ActivityHeatmap from "@/components/haze/ui/activity-heatmap.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/activity-heatmap.md
Customize with real content
Adapt the Haze activity-heatmap ui for our product. Replace the demo values of these props with our real content: label, unit, unitSingular, samples, end, weeks, maxCount, class. Keep the layout, the heading level rules and these accessibility details: The disclosure includes each date and count in text. Future cells in the final week are blank and excluded from the count list. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using activity-heatmap, check it fits: use it when daily activity varies enough to show trends by weekday.; you need a compact view plus the option to read exact daily counts.. Avoid it when events are best understood in chronological detail with descriptions.. Alternatives: @haze/timeline, @haze/sparkline, @haze/uptime-tracker.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.