{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kpi-trend",
  "title": "KPI trend",
  "description": "KPI tile family: the number rolls in on view and on every change (NumberFlow), the delta badge pops and pulses, the sparkline draws itself (SparklineDraw), and a group lines tiles up with hairline dividers.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json",
    "https://goodui.dev/r/number-flow.json",
    "https://goodui.dev/r/sparkline-draw.json"
  ],
  "files": [
    {
      "path": "registry/components/charts/kpi-trend.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { NumberFlow } from \"@/components/goodui/number-flow\"\nimport { SparklineDraw } from \"@/components/goodui/sparkline-draw\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens } from \"@/lib/goodui-motion\"\n\n/*\n * KPI tile family: headline number (NumberFlow rolls it in on view and on every change), a delta badge that\n * pulses when the change changes, and a SparklineDraw that redraws when the data changes. KpiTrendGroup lines\n * tiles up with hairline dividers.\n */\n\nexport type KpiTrendLabels = { increase: string; decrease: string; noChange: string }\nconst DEFAULT_LABELS: KpiTrendLabels = { increase: \"increase\", decrease: \"decrease\", noChange: \"no change\" }\n\nexport type KpiTrendSize = \"sm\" | \"md\" | \"lg\"\n\nexport type KpiTrendProps = Omit<React.ComponentProps<\"div\">, \"prefix\"> & {\n  /** Metric name above the value. */\n  label: React.ReactNode\n  value: number\n  /** Intl.NumberFormat options, e.g. { style: \"currency\", currency: \"USD\", maximumFractionDigits: 0 }. */\n  format?: Intl.NumberFormatOptions\n  locale?: string\n  /** Text before the number. */\n  prefix?: string\n  /** Text after the number, e.g. \"%\". */\n  suffix?: string\n  /** Change vs the previous period, e.g. 12.4 for +12.4%. */\n  delta?: number\n  /** Unit after the delta number. */\n  deltaSuffix?: string\n  /** Decimals of the delta. */\n  deltaDecimals?: number\n  /** A drop is good news (no-shows, churn, wait time). */\n  invertDelta?: boolean\n  /** Muted text after the delta, e.g. \"vs last week\". */\n  deltaLabel?: React.ReactNode\n  /** Sparkline values, oldest first. */\n  data?: number[]\n  /** One label per sparkline point, shown in its tooltip. */\n  pointLabels?: string[]\n  /** Sparkline color. Defaults to the theme's first chart color. */\n  color?: string\n  /** stack: sparkline under the number. row: sparkline on the end side. */\n  layout?: \"stack\" | \"row\"\n  size?: KpiTrendSize\n  /** Hover lift for tiles that link somewhere. */\n  interactive?: boolean\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<KpiTrendLabels>\n}\n\nconst CSS = `\n.pc-kpi-trend{background:var(--card);color:var(--card-foreground)}\n.pc-kpi-trend[data-interactive]{transition:transform .2s var(--pc-ease-out),border-color .2s;cursor:pointer}\n.pc-kpi-trend[data-interactive]:hover{transform:translateY(-2px);border-color:color-mix(in oklab,var(--foreground) 22%,var(--border))}\n.pc-kpi-trend-delta{--pc-d:var(--muted-foreground);color:color-mix(in oklab,var(--pc-d) 72%,var(--foreground));background:color-mix(in oklab,var(--pc-d) 13%,transparent)}\n.pc-kpi-trend-delta[data-trend=good]{--pc-d:oklch(0.53 0.13 155)}\n.pc-kpi-trend-delta[data-trend=bad]{--pc-d:oklch(0.56 0.21 27)}\n.dark .pc-kpi-trend-delta[data-trend=good]{--pc-d:oklch(0.72 0.16 155)}\n.dark .pc-kpi-trend-delta[data-trend=bad]{--pc-d:oklch(0.7 0.19 25)}\n.pc-kpi-trend-delta[data-pulse]{animation:pc-kpi-trend-pulse .6s ease-out}\n@keyframes pc-kpi-trend-pulse{0%{box-shadow:0 0 0 0 color-mix(in oklab,var(--pc-d) 45%,transparent)}100%{box-shadow:0 0 0 8px transparent}}\n.pc-kpi-trend[data-inview=false] .pc-kpi-trend-delta{opacity:0;transform:scale(.7)}\n.pc-kpi-trend[data-inview=true] .pc-kpi-trend-delta{animation:pc-kpi-trend-pop .45s var(--pc-ease-spring) both}\n@keyframes pc-kpi-trend-pop{from{opacity:0;transform:scale(.7)}to{opacity:1;transform:none}}\n.pc-kpi-trend-group{display:grid;gap:1px;background:var(--border);border:1px solid var(--border);border-radius:.75rem;overflow:hidden}\n.pc-kpi-trend-group>.pc-kpi-trend{border:0;border-radius:0}\n@media (prefers-reduced-motion:reduce){.pc-kpi-trend,.pc-kpi-trend-delta{animation:none!important;transition:none!important;opacity:1!important;transform:none!important}}\n`\n\nfunction useReduce() {\n  const prefers = useReducedMotion()\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  return mounted && !!prefers\n}\n\nfunction useInView(ref: React.RefObject<Element | null>, threshold = 0.3) {\n  const [inView, setInView] = React.useState(false)\n  React.useEffect(() => {\n    const el = ref.current\n    if (!el || inView) return\n    if (typeof IntersectionObserver === \"undefined\") {\n      setInView(true)\n      return\n    }\n    const io = new IntersectionObserver(\n      ([e]) => {\n        if (e?.isIntersecting) {\n          setInView(true)\n          io.disconnect()\n        }\n      },\n      { threshold },\n    )\n    io.observe(el)\n    return () => io.disconnect()\n  }, [ref, inView, threshold])\n  return inView\n}\n\nconst SIZE: Record<KpiTrendSize, { pad: string; value: string; label: string; chart: number; gap: string }> = {\n  sm: { pad: \"p-3.5\", value: \"text-xl\", label: \"text-xs\", chart: 32, gap: \"gap-2\" },\n  md: { pad: \"p-5\", value: \"text-3xl\", label: \"text-sm\", chart: 44, gap: \"gap-3\" },\n  lg: { pad: \"p-6\", value: \"text-4xl\", label: \"text-sm\", chart: 56, gap: \"gap-3.5\" },\n}\n\nfunction Arrow({ dir }: { dir: number }) {\n  return (\n    <svg viewBox=\"0 0 12 12\" className={cn(\"size-3\", dir < 0 && \"rotate-180\")} fill=\"none\" aria-hidden=\"true\">\n      <path d=\"M6 9.5v-7M3 5l3-3 3 3\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n    </svg>\n  )\n}\n\n/** KPI tile: the number rolls in on view and on every change, the delta badge pops and pulses, and the sparkline draws itself. */\nexport function KpiTrend({\n  label,\n  value,\n  format,\n  locale = \"en-US\",\n  prefix = \"\",\n  suffix = \"\",\n  delta,\n  deltaSuffix = \"%\",\n  deltaDecimals = 1,\n  invertDelta,\n  deltaLabel,\n  data,\n  pointLabels,\n  color = \"var(--chart-1)\",\n  layout = \"stack\",\n  size = \"md\",\n  interactive,\n  labels,\n  className,\n  ...props\n}: KpiTrendProps) {\n  const l = useLabels(\"kpi-trend\", DEFAULT_LABELS, labels)\n  const reduce = useReduce()\n  const ref = React.useRef<HTMLDivElement>(null)\n  const inView = useInView(ref)\n  const ready = inView || reduce\n  const s = SIZE[size]\n  const formatter = React.useMemo(() => new Intl.NumberFormat(locale, format), [locale, format])\n  const finalText = `${prefix}${formatter.format(value)}${suffix}`\n  const d = delta === undefined ? null : Number(delta.toFixed(deltaDecimals))\n  const dir = d === null ? 0 : d > 0 ? 1 : d < 0 ? -1 : 0\n  const trend = dir === 0 ? \"flat\" : (dir > 0) !== !!invertDelta ? \"good\" : \"bad\"\n  const deltaText = d === null ? \"\" : `${Math.abs(d).toLocaleString(locale, { minimumFractionDigits: deltaDecimals, maximumFractionDigits: deltaDecimals })}${deltaSuffix}`\n\n  // Count delta changes during render so the pulse starts on the same frame as the new value.\n  const [seen, setSeen] = React.useState(d)\n  const [pulse, setPulse] = React.useState(0)\n  if (seen !== d) {\n    setSeen(d)\n    if (seen !== null) setPulse((p) => p + 1)\n  }\n\n  const chart =\n    data && data.length > 1 ? (\n      ready ? (\n        <SparklineDraw data={data} pointLabels={pointLabels} color={color} height={s.chart} formatValue={(v) => `${prefix}${formatter.format(v)}${suffix}`} aria-label={typeof label === \"string\" ? label : undefined} className={layout === \"row\" ? \"w-28 shrink-0 sm:w-32\" : undefined} />\n      ) : (\n        <div className={layout === \"row\" ? \"w-28 shrink-0 sm:w-32\" : \"w-full\"} style={{ height: s.chart }} aria-hidden=\"true\" />\n      )\n    ) : null\n\n  return (\n    <div ref={ref} data-inview={ready} data-interactive={interactive ? \"\" : undefined} className={cn(\"pc-kpi-trend flex min-w-0 flex-col rounded-xl border\", s.pad, s.gap, className)} {...props}>\n      <GooduiTokens />\n      <style href=\"pc-kpi-trend\" precedence=\"default\">\n        {CSS}\n      </style>\n      <div className={cn(\"flex min-w-0 flex-1\", layout === \"row\" ? \"items-center justify-between gap-4\" : \"flex-col\", s.gap)}>\n        <div className={cn(\"flex min-w-0 flex-col\", size === \"sm\" ? \"gap-1\" : \"gap-1.5\")}>\n          <span className={cn(\"truncate font-medium text-muted-foreground\", s.label)}>{label}</span>\n          <span className={cn(\"leading-none font-semibold tracking-tight\", s.value)}>\n            <span className=\"sr-only\">{finalText}</span>\n            <span aria-hidden=\"true\">\n              <NumberFlow value={ready ? value : 0} format={format} locale={locale} prefix={prefix} suffix={suffix} />\n            </span>\n          </span>\n          {d !== null && (\n            <span className=\"flex flex-wrap items-center gap-x-1.5 gap-y-0.5\">\n              <span key={pulse} data-trend={trend} data-pulse={pulse > 0 ? \"\" : undefined} className=\"pc-kpi-trend-delta inline-flex items-center gap-0.5 rounded-full px-1.5 py-0.5 text-xs font-semibold tabular-nums\">\n                {dir !== 0 && <Arrow dir={dir} />}\n                <span aria-hidden=\"true\">\n                  <NumberFlow value={ready ? Math.abs(d) : 0} format={{ minimumFractionDigits: deltaDecimals, maximumFractionDigits: deltaDecimals }} locale={locale} suffix={deltaSuffix} />\n                </span>\n                <span className=\"sr-only\">{`${deltaText} ${dir > 0 ? l.increase : dir < 0 ? l.decrease : l.noChange}`}</span>\n              </span>\n              {deltaLabel && <span className=\"text-xs text-muted-foreground\">{deltaLabel}</span>}\n            </span>\n          )}\n        </div>\n        {chart}\n      </div>\n    </div>\n  )\n}\n\nexport type KpiTrendGroupProps = React.ComponentProps<\"div\"> & {\n  /** Tiles per row on wide screens; they stack to one and two columns below. */\n  columns?: 2 | 3 | 4\n}\n\nconst COLS: Record<2 | 3 | 4, string> = {\n  2: \"grid-cols-1 sm:grid-cols-2\",\n  3: \"grid-cols-1 sm:grid-cols-3\",\n  4: \"grid-cols-1 sm:grid-cols-2 lg:grid-cols-4\",\n}\n\n/** Lines KpiTrend tiles up with hairline dividers inside one rounded border. */\nexport function KpiTrendGroup({ columns = 4, className, children, ...props }: KpiTrendGroupProps) {\n  return (\n    <div className={cn(\"pc-kpi-trend-group\", COLS[columns], className)} {...props}>\n      <style href=\"pc-kpi-trend\" precedence=\"default\">\n        {CSS}\n      </style>\n      {children}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/kpi-trend.tsx"
    }
  ],
  "categories": [
    "components",
    "charts"
  ],
  "type": "registry:component"
}
