{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"stats-cards","type":"registry:block","title":"Stats cards","description":"Four numbers with their change and trend that count up, and a period switch that moves all of them.","dependencies":["lucide-react"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/lib-motion.json","https://dashral.com/r/metric-card.json","https://dashral.com/r/segmented-control.json","https://dashral.com/r/sparkline.json"],"files":[{"path":"registry/dashral/blocks/stats-cards.tsx","type":"registry:file","target":"components/dashral/blocks/stats-cards.tsx","content":"\"use client\"\n\nimport { useState } from \"react\"\nimport { Clock, DollarSign, Percent, ShoppingBag } from \"lucide-react\"\nimport { MetricCard } from \"../metric-card\"\nimport { SegmentedControl } from \"../segmented-control\"\nimport { Sparkline } from \"../sparkline\"\nimport { enterMotion, stagger } from \"../lib/motion\"\nimport { cn } from \"../lib/utils\"\n\nexport type Stat = { value: number; change: number; trend: number[] }\n/** A card: what it measures and how its number reads; its figures come from each period, under `key`. */\nexport type StatMetric = {\n  key: string\n  label: string\n  icon?: React.ReactNode\n  formatOptions?: Intl.NumberFormatOptions\n  formatValue?: (value: number) => string\n  /** After the change, e.g. \" pts\". */\n  changeUnit?: string\n  /** A fall is good news (response times): it shows in green. */\n  invert?: boolean\n  chart: \"area\" | \"bar\" | \"line\"\n}\n/** A period on the switch: its label there, what the change is against, and each card's figures by key. */\nexport type StatPeriod = { value: string; label: string; compare: string; stats: Record<string, Stat> }\n\nconst sampleMetrics: StatMetric[] = [\n  { key: \"revenue\", label: \"Revenue\", icon: <DollarSign />, formatOptions: { style: \"currency\", currency: \"USD\", maximumFractionDigits: 0 }, chart: \"area\" },\n  { key: \"orders\", label: \"Orders\", icon: <ShoppingBag />, chart: \"bar\" },\n  { key: \"conversion\", label: \"Conversion rate\", icon: <Percent />, formatOptions: { style: \"percent\", minimumFractionDigits: 1, maximumFractionDigits: 1 }, changeUnit: \" pts\", chart: \"line\" },\n  { key: \"response\", label: \"First response\", icon: <Clock />, formatValue: (v: number) => `${Math.round(v)} min`, invert: true, chart: \"line\" },\n]\n\nconst sampleData: Record<string, { label: string; revenue: Stat; orders: Stat; conversion: Stat; response: Stat }> = {\n  \"7d\": {\n    label: \"vs previous 7 days\",\n    revenue: { value: 12840, change: 8.2, trend: [1.4, 1.6, 1.5, 1.9, 1.8, 2.1, 2.3] },\n    orders: { value: 342, change: 4.6, trend: [41, 45, 44, 52, 49, 54, 57] },\n    conversion: { value: 0.031, change: 0.4, trend: [2.7, 2.8, 2.9, 3.0, 2.9, 3.1, 3.2] },\n    response: { value: 74, change: -12.1, trend: [92, 88, 85, 81, 79, 76, 74] },\n  },\n  \"30d\": {\n    label: \"vs previous 30 days\",\n    revenue: { value: 48290, change: 12.5, trend: [31, 34, 33, 38, 36, 41, 40, 44, 43, 47, 46, 52] },\n    orders: { value: 1408, change: 9.1, trend: [98, 104, 101, 112, 108, 117, 115, 121, 119, 126, 124, 133] },\n    conversion: { value: 0.029, change: 0.2, trend: [2.6, 2.7, 2.7, 2.8, 2.7, 2.9, 2.8, 2.9, 3.0, 2.9, 3.0, 3.1] },\n    response: { value: 81, change: -6.3, trend: [96, 94, 93, 90, 91, 88, 87, 85, 86, 83, 82, 81] },\n  },\n  \"90d\": {\n    label: \"vs previous 90 days\",\n    revenue: { value: 139400, change: -2.4, trend: [52, 49, 47, 50, 46, 44, 45, 43, 46, 44, 47, 45] },\n    orders: { value: 4120, change: 1.8, trend: [310, 322, 318, 331, 327, 335, 330, 341, 338, 344, 340, 349] },\n    conversion: { value: 0.027, change: -0.3, trend: [3.0, 2.9, 2.9, 2.8, 2.8, 2.7, 2.8, 2.7, 2.6, 2.7, 2.6, 2.7] },\n    response: { value: 88, change: 3.5, trend: [82, 83, 85, 84, 86, 85, 87, 86, 88, 87, 89, 88] },\n  },\n}\n\n/** Reads a trend aloud by its two ends, so a screen reader hears which way it went. */\nfunction trendLabel(name: string, trend: number[]) {\n  const rising = trend.at(-1)! >= trend[0]!\n  return `${name} trend: ${rising ? \"rising\" : \"falling\"}`\n}\n\nconst samplePeriods: StatPeriod[] = ([\"7d\", \"30d\", \"90d\"] as const).map((value) => {\n  const { label: compare, ...stats } = sampleData[value]!\n  return { value, label: `${value.slice(0, -1)} days`, compare, stats }\n})\n\n/**\n * The top of a dashboard: four numbers with their change and trend, and a period switch that moves all of them.\n * Response time goes down when it gets better, so its fall shows in green. Four columns, then two, then one. Every\n * part comes from props: your metrics and each period's figures; with `period` and `onPeriodChange` you load a\n * period's figures as it's picked.\n */\nexport function StatsCards({\n  title = \"Overview\",\n  description = \"Updated a minute ago · all stores\",\n  metrics = sampleMetrics,\n  periods = samplePeriods,\n  period: periodProp,\n  onPeriodChange,\n}: {\n  title?: React.ReactNode\n  description?: React.ReactNode\n  metrics?: StatMetric[]\n  periods?: StatPeriod[]\n  /** The period picked (the cards keep it without it). */\n  period?: string\n  onPeriodChange?: (period: string) => void\n}) {\n  const [ownPeriod, setOwnPeriod] = useState(periods[1]?.value ?? periods[0]?.value ?? \"\")\n  const period = periodProp ?? ownPeriod\n  const setPeriod = (next: string) => {\n    setOwnPeriod(next)\n    onPeriodChange?.(next)\n  }\n  const data = periods.find((p) => p.value === period) ?? periods[0]\n  const cards = metrics.flatMap((metric) => {\n    const stat = data?.stats[metric.key]\n    return stat ? [{ ...metric, stat }] : []\n  })\n\n  return (\n    <section aria-labelledby=\"stats-cards-title\" className=\"bg-background p-4 sm:p-6\">\n      <div className=\"flex flex-wrap items-end justify-between gap-3\">\n        <div>\n          <h2 id=\"stats-cards-title\" className=\"text-lg font-semibold tracking-[-0.01em]\">\n            {title}\n          </h2>\n          {description && <p className=\"mt-0.5 text-sm text-muted-foreground\">{description}</p>}\n        </div>\n        {periods.length > 1 && (\n          <SegmentedControl aria-label=\"Period\" value={period} onValueChange={setPeriod} options={periods.map(({ value, label }) => ({ value, label }))} />\n        )}\n      </div>\n\n      <div className=\"mt-4 grid gap-4 sm:grid-cols-2 xl:grid-cols-[repeat(var(--count),minmax(0,1fr))]\" style={{ \"--count\": Math.min(4, cards.length) } as React.CSSProperties}>\n        {cards.map((card, i) => (\n          <MetricCard\n            key={card.label}\n            label={card.label}\n            value={card.stat.value}\n            formatOptions={card.formatOptions}\n            formatValue={card.formatValue}\n            change={card.stat.change}\n            changeUnit={card.changeUnit}\n            changeLabel={data?.compare}\n            invert={card.invert}\n            icon={card.icon}\n            countOnMount\n            className={cn(enterMotion)}\n            style={stagger(i, 60)}\n          >\n            {/* Keyed by the period, so the new trend draws in instead of morphing from the old one. */}\n            <Sparkline\n              key={period}\n              data={card.stat.trend}\n              variant={card.chart}\n              min={card.chart === \"bar\" ? 0 : undefined}\n              className=\"h-10 text-primary\"\n              aria-label={trendLabel(card.label, card.stat.trend)}\n            />\n          </MetricCard>\n        ))}\n      </div>\n    </section>\n  )\n}\n"}]}