# Hero with live metrics (带实时指标的首屏)

Centered hero for an infrastructure product. Below the headline a wide panel carries four headline numbers that tick up once on view, and a per-region latency table.

- Ref: `component/hero-live-metrics` (component · hero)
- Page: https://uifino.com/en/components/hero/hero-live-metrics
- Markdown: https://uifino.com/md/components/hero-live-metrics.md
- Preview image: https://uifino.com/blocks/covers/hero-live-metrics-dark.webp

## Install

- shadcn: `npx shadcn@latest add "https://uifino.com/r/blocks/hero-live-metrics.json"`
- UIFino CLI: `npx uifino@latest add component/hero-live-metrics`

## Dependencies

`lucide-react`, `motion`

## shadcn/ui components it builds on

- `button`. The shadcn CLI installs them together with the block.

## Layout borrowed from

- https://www.tinybird.co: Centered headline with two actions, then one wide bordered panel that opens with real numbers. Content and panel structure are our own.

## Theme

The block reads the project's shadcn theme variables, so it follows whatever DESIGN.md the project uses and needs no options of its own.
To give the project the look of a UIFino brand or style, install its theme: `npx shadcn@latest add "https://uifino.com/r/themes/vercel.json"`.

## Usage

The files under `examples/` show the block wired with content, including its loading, empty and error states where it has them.

## Files

| Path | Role | Size |
| :--- | :--- | ---: |
| components/blocks/hero-live-metrics/examples/content.ts | demo | 2.4 KB |
| components/blocks/hero-live-metrics/examples/default.tsx | demo | 235 B |
| components/blocks/hero-live-metrics/hero-live-metrics.tsx | source | 5.8 KB |

### components/blocks/hero-live-metrics/hero-live-metrics.tsx

```tsx
"use client";

import { useEffect, useRef, useState } from "react";
import { animate, useInView, useReducedMotion } from "motion/react";
import { ArrowUpRight } from "lucide-react";
import { Button } from "@/components/ui/button";

export interface HeroLiveMetricsStat {
  readonly id: string;
  readonly value: number;
  readonly decimals: number;
  readonly unit: string;
  readonly label: string;
  readonly note: string;
}

export interface HeroLiveMetricsRegion {
  readonly id: string;
  readonly name: string;
  readonly p99: number;
  readonly queries: string;
}

export interface HeroLiveMetricsProps {
  readonly title: string;
  readonly description: string;
  readonly primaryAction: string;
  readonly secondaryAction: string;
  readonly windowLabel: string;
  readonly regionsTitle: string;
  readonly p99Label: string;
  readonly queriesLabel: string;
  readonly locale?: string;
  readonly stats: readonly HeroLiveMetricsStat[];
  readonly regions: readonly HeroLiveMetricsRegion[];
}

function formatNumber(value: number, decimals: number, locale: string) {
  return value.toLocaleString(locale, {
    minimumFractionDigits: decimals,
    maximumFractionDigits: decimals
  });
}

const START_RATIO = 0.82;

function TickingNumber({
  value,
  decimals,
  locale
}: {
  readonly value: number;
  readonly decimals: number;
  readonly locale: string;
}) {
  const ref = useRef<HTMLSpanElement>(null);
  const inView = useInView(ref, { once: true, margin: "0px 0px -10% 0px" });
  const reduceMotion = useReducedMotion();
  const [display, setDisplay] = useState<number | null>(null);

  useEffect(() => {
    if (reduceMotion || !inView) return;
    const controls = animate(value * START_RATIO, value, {
      duration: 1.1,
      ease: [0.16, 1, 0.3, 1],
      onUpdate: setDisplay,
      onComplete: () => setDisplay(null)
    });
    return () => controls.stop();
  }, [inView, reduceMotion, value]);

  return (
    <span ref={ref} className="tabular-nums">
      {formatNumber(display ?? value, decimals, locale)}
    </span>
  );
}

export function HeroLiveMetrics({
  title,
  description,
  primaryAction,
  secondaryAction,
  windowLabel,
  regionsTitle,
  p99Label,
  queriesLabel,
  stats,
  regions,
  locale = "en-US"
}: HeroLiveMetricsProps) {
  const slowest = Math.max(...regions.map((region) => region.p99), 1);

  return (
    <section className="@container relative flex min-h-svh flex-col overflow-hidden bg-background text-foreground">
      <div className="mx-auto flex w-full max-w-3xl flex-col items-center px-4 pt-16 text-center @3xl:pt-24">
        <h1 className="font-heading text-3xl font-medium tracking-tight text-balance @3xl:text-6xl @3xl:whitespace-pre-line @3xl:leading-[1.05]">
          {title}
        </h1>
        <p className="mt-6 max-w-xl text-base text-muted-foreground @3xl:text-lg">{description}</p>
        <div className="mt-8 flex flex-wrap items-center justify-center gap-3">
          <Button size="lg">{primaryAction}</Button>
          <Button size="lg" variant="ghost">
            {secondaryAction}
            <ArrowUpRight />
          </Button>
        </div>
      </div>

      <div className="mx-auto mt-12 w-full max-w-5xl flex-1 px-4 @3xl:mt-16 @3xl:px-8">
        <div className="rounded-xl border bg-card">
          <p className="px-4 py-3 text-sm text-muted-foreground @3xl:px-5">{windowLabel}</p>
          <dl className="grid grid-cols-2 gap-px border-y bg-border @3xl:grid-cols-4">
            {stats.map((stat) => (
              <div key={stat.id} className="flex min-w-0 flex-col gap-1 bg-card p-4 @3xl:p-5">
                <dt className="truncate text-sm text-muted-foreground">{stat.label}</dt>
                <dd className="flex items-baseline gap-1 text-3xl font-medium tracking-tight @3xl:text-4xl">
                  <TickingNumber value={stat.value} decimals={stat.decimals} locale={locale} />
                  <span className="text-base font-normal text-muted-foreground">{stat.unit}</span>
                </dd>
                <p className="truncate text-sm text-muted-foreground">{stat.note}</p>
              </div>
            ))}
          </dl>

          <div className="p-4 @3xl:p-5">
            <div className="grid grid-cols-[minmax(0,1fr)_auto] gap-x-4 text-sm text-muted-foreground @3xl:grid-cols-[10rem_minmax(0,1fr)_5rem_6rem]">
              <span>{regionsTitle}</span>
              <span className="hidden @3xl:block" />
              <span className="text-right">{p99Label}</span>
              <span className="hidden text-right @3xl:block">{queriesLabel}</span>
            </div>
            <ul className="mt-2 flex flex-col">
              {regions.map((region) => (
                <li
                  key={region.id}
                  className="grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 border-t py-2.5 text-sm @3xl:grid-cols-[10rem_minmax(0,1fr)_5rem_6rem]"
                >
                  <span className="flex min-w-0 flex-col">
                    <span className="truncate font-medium">{region.name}</span>
                    <span className="truncate text-muted-foreground @3xl:hidden">
                      {region.queries} {queriesLabel}
                    </span>
                  </span>
                  <span className="hidden h-1.5 overflow-hidden rounded-full bg-muted @3xl:block">
                    <span
                      className="block h-full rounded-full bg-chart-1"
                      style={{ width: `${Math.round((region.p99 / slowest) * 100)}%` }}
                    />
                  </span>
                  <span className="text-right tabular-nums">{region.p99.toFixed(1)} ms</span>
                  <span className="hidden text-right tabular-nums text-muted-foreground @3xl:block">
                    {region.queries}
                  </span>
                </li>
              ))}
            </ul>
          </div>
        </div>
      </div>
    </section>
  );
}
```

Not inlined (2 files, 2.7 KB). Full sources: https://uifino.com/r/blocks/hero-live-metrics.json
