Metric Ticker
A compact KPI strip with changing number transitions, signed deltas, and a disclosure for each recent trend.
pnpm dlx shadcn@latest add @haze/metric-ticker---
import { cn } from "../../../lib/haze/utils";
import AnimatedNumber from "./animated-number.astro";
import Sparkline from "./sparkline.astro";
interface Props {
id: string;
label?: string;
metrics?: { key: string; label: string; value: number; suffix?: string; delta?: number; samples?: { label: string; value: number }[] }[];
class?: string;
}
const { id, label = "Workspace metrics", class: className, metrics = [
{ key: "requests", label: "Requests", value: 1240, delta: 24, samples: [{ label: "09:00", value: 1120 }, { label: "10:00", value: 1180 }, { label: "11:00", value: 1240 }] },
{ key: "latency", label: "API p95", value: 214, suffix: " ms", delta: -12, samples: [{ label: "09:00", value: 238 }, { label: "10:00", value: 226 }, { label: "11:00", value: 214 }] },
] } = Astro.props;
---
<section id={id} data-haze-metric-ticker aria-label={label} class={cn("relative min-w-0 overflow-x-auto rounded-xl border border-border bg-card", className)}>
<ul class="flex min-w-0 divide-x divide-border">
{metrics.map(metric => <li data-metric-key={metric.key} class="min-w-40 flex-1">
<details class="group/metric min-w-0">
<summary class="grid min-h-24 cursor-pointer list-none gap-1 rounded-lg px-4.5 py-3.5 hover:bg-accent/50 motion-safe:transition-[background-color] motion-safe:duration-150 motion-safe:ease-snappy focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">
<span class="text-[0.8125rem]/5 text-muted-foreground">{metric.label}</span>
<AnimatedNumber value={metric.value} suffix={metric.suffix} class="text-[1.0625rem]/6 font-medium tracking-[-0.01em]" />
<span data-metric-delta class="text-xs/5 tabular-nums text-muted-foreground">{metric.delta ? `${metric.delta > 0 ? "+" : ""}${metric.delta}${metric.suffix ?? ""} since last update` : "No change"}<span class="sr-only">. Open recent trend</span></span>
</summary>
<div class="px-3 pb-3 motion-safe:animate-rise">
<Sparkline label={`${metric.label} recent trend`} samples={metric.samples ?? [{ label: "Current", value: metric.value }]} unit={metric.suffix?.trim() ?? ""} class="border-0 bg-muted/50 p-3" />
</div>
</details>
</li>)}
</ul>
</section>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
root.addEventListener("haze:metric-update", event => {
const updates = (event as CustomEvent<{ key: string; value: number }[]>).detail;
if (!Array.isArray(updates)) return;
for (const update of updates) {
if (!Number.isFinite(update.value)) continue;
const metric = [...root.querySelectorAll<HTMLElement>("[data-metric-key]")].find(item => item.dataset.metricKey === update.key);
const number = metric?.querySelector<HTMLElement>("[data-haze-animated-number]");
const delta = metric?.querySelector<HTMLElement>("[data-metric-delta]");
if (!number || !delta) continue;
const difference = update.value - Number(number.dataset.value);
number.dataset.value = String(update.value);
delta.textContent = difference ? `${difference > 0 ? "+" : ""}${Number(difference.toFixed(4))}${number.dataset.suffix ?? ""} since last update` : "No change";
metric?.querySelector("summary")?.setAttribute("aria-label", `${metric.querySelector("summary > span")?.textContent}, ${update.value}${number.dataset.suffix ?? ""}. ${delta.textContent}. Open recent trend.`);
}
});
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-metric-ticker]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-metric-ticker-listener")) {
document.documentElement.setAttribute("data-haze-metric-ticker-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- Operations metrics need to fit in a narrow dashboard strip.
- People should inspect a metric's history without navigating away.
When not to use
- A chart must communicate several relationships together. Use line-chart.
Installation
Add the Haze registry to components.json once, then install Metric Ticker with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/metric-tickernpx shadcn@latest add @haze/metric-tickeryarn dlx shadcn@latest add @haze/metric-tickerbunx --bun shadcn@latest add @haze/metric-tickerFirst 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 |
|---|---|---|---|
| id* | string | – | Unique strip ID used for update events. |
| label | string | – | Accessible strip name. |
| metrics | Metric[] | – | Unique key, label, finite value, suffix, initial delta and labelled baseline samples. |
| class | string | – | Additional classes on the component wrapper. |
Keyboard interactions
- EnterSpace
- Open a metric's recent trend.
- Tab
- Move between metrics and the underlying sample disclosures.
Accessibility
- Metrics are list items with native disclosure triggers and exact-value trend details. The strip scrolls inside its container on small screens.
- Dispatch haze:metric-update with key/value pairs to roll the values and report signed changes. Each disclosure preserves the supplied baseline history.
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 "Metric Ticker" with `npx shadcn@latest add @haze/metric-ticker`. 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 MetricTicker from "@/components/haze/ui/metric-ticker.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/metric-ticker.md
Customize with real content
Adapt the Haze metric-ticker ui for our product. Replace the demo values of these props with our real content: id, label, metrics, class. Keep the layout, the heading level rules and these accessibility details: Metrics are list items with native disclosure triggers and exact-value trend details. The strip scrolls inside its container on small screens. Dispatch haze:metric-update with key/value pairs to roll the values and report signed changes. Each disclosure preserves the supplied baseline history. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using metric-ticker, check it fits: use it when operations metrics need to fit in a narrow dashboard strip.; people should inspect a metric's history without navigating away.. Avoid it when a chart must communicate several relationships together. use line-chart.. Alternatives: @haze/stat, @haze/sparkline, @haze/animated-number.
Dependencies
- @haze/animated-number
- @haze/sparkline
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.