# Metric Ticker

> A compact KPI strip with changing number transitions, signed deltas, and a disclosure for each recent trend.

- Kind: ui · Category: Data display · Interactivity: script
- Install: `npx shadcn@latest add @haze/metric-ticker`
- File: `src/components/haze/ui/metric-ticker.astro`
- npm dependencies: none
- Registry dependencies: @haze/animated-number, @haze/sparkline, @haze/utils
- Docs page: https://hazeui.com/ui/metric-ticker

## Use when
- Operations metrics need to fit in a narrow dashboard strip.
- People should inspect a metric's history without navigating away.

## Avoid when
- A chart must communicate several relationships together. Use line-chart.

## Alternatives
- [stat](https://hazeui.com/ui/stat.md)
- [sparkline](https://hazeui.com/ui/sparkline.md)
- [animated-number](https://hazeui.com/ui/animated-number.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique strip ID used for update events. |
| label | `string` |  | no | Accessible strip name. |
| metrics | `Metric[]` |  | no | Unique key, label, finite value, suffix, initial delta and labelled baseline samples. |
| class | `string` |  | no | Additional classes on the component wrapper. |

## 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.
- Key Enter / Space: Open a metric's recent trend.
- Key Tab: Move between metrics and the underlying sample disclosures.

## Agent prompts

### 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": "https://hazeui.com/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: https://hazeui.com/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.

## Example requests that should lead here
- Create a compact strip of request count and API latency that animates incoming updates.

## Source

```astro
---
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>
```
