Interactive Answers

Stat Grid

KPI cards with rolling numbers, deltas and sparklines for a quick summary answer.

3 variantsMIT

Preview

Installation

Copies the component into components/ui and installs its dependencies and theme tokens.

pnpm dlx shadcn@latest add https://uilab.tmih06.com/r/stat-grid.json

Usage

Pass your own data and wire the callbacks to your agent runtime.

import { StatGrid } from "@/components/ui/stat-grid";

// A summary answer: the agent fetched metrics and shows the headline numbers.
export function MetricsAnswer({ m }: { m: Metrics }) {
  return (
    <StatGrid
      title="October at a glance"
      data={[
        { id: "mrr", label: "MRR", value: formatMoney(m.mrr), delta: m.mrrDelta, trend: m.mrrTrend },
        {
          id: "latency",
          label: "Median response time",
          value: `${m.p50}s`,
          delta: m.p50Delta,
          higherIsBetter: false, // a drop is good news
        },
      ]}
    />
  );
}

// While the metrics request is in flight, pass null explicitly.
// Keep stale data and set loading when refetching.
export function MetricsState({ query }: { query: MetricsQuery }) {
  return (
    <StatGrid
      title="October at a glance"
      data={query.data ? toStats(query.data) : null}
      loading={query.isRefetching}
      skeleton={{ count: 4 }}
    />
  );
}

Props

PropTypeDefaultDescription
data*Stat[] | null—Two to four stats. Values are preformatted strings and roll when they change. Pass null while they load.
loadingbooleanfalseForce the layout-preserving skeleton while refetching, even when stale data is available.
skeleton{ count?: number }—Shape hints for the loading state; count is the number of KPI placeholders before the real cardinality is known.
titlestring—Optional header above the cards.
subtitlestring—Secondary line under the title.
sparklinesbooleantrueDraw a trend line for stats that carry `trend`.
onSelect(stat: Stat) => void—Makes each card a button, e.g. to open a chart for that metric.
localestring"en-US"BCP 47 locale for the delta and goal percentages.
labelsPartial<StatGridLabels>—Your own screen-reader strings (loading, better, worse, % of goal), for translating. Right-to-left works under `dir="rtl"`; sparklines still run left to right.
classNamestring—Extra classes for the root element.
type Stat = {
  id: string;
  label: string;
  value: string;            // "$48.2k", "1,204", "1.31s"
  delta?: number;           // % change; shown with an arrow and better/worse text
  higherIsBetter?: boolean; // default true; set false for latency, churn, cost
  trend?: number[];         // oldest first
  goal?: { ratio: number; label: string }; // striped progress toward a target
  hint?: string;            // e.g. "vs. last week"
};