{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "stat-group",
  "title": "Stat Group",
  "description": "Primitives for a row of related stats: ruled columns, labels, light values, the change vs the previous period as a sentence or an arrow, and sparklines.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/trend.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/stat-group.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateText,\n  SkeletonText,\n  useBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { TinyAreaChart } from '@/registry/components/dashboardblocks/chart'\nimport { KPIChange, getKPIChange } from '@/registry/components/dashboardblocks/kpi'\nimport { Trend } from '@/registry/components/dashboardblocks/trend'\nimport {\n  formatNumber,\n  formatPoints,\n  formatSigned,\n} from '@/registry/lib/dashboardblocks/format'\nimport type { ComponentProps, ReactNode } from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype StatFormatter = (value: number) => string\n\ninterface StatMetric {\n  /** `count` shows the difference itself, such as `4`. @default 'percent' */\n  changeType?: 'percent' | 'points' | 'count'\n  /** A line under a value that has no `previous` to compare with, such as what it's about or since when. */\n  detail?: ReactNode\n  formatter?: StatFormatter\n  /** Use `down` for metrics like churn or latency, where a decrease is good. */\n  goodDirection?: 'up' | 'down'\n  key: string\n  label: string\n  previous?: number\n  /** Draws the value in a status colour: `bad` for a loss or a failure, `warning` for a figure that needs action now. */\n  tone?: 'bad' | 'warning'\n  value: number\n}\n\nconst defaultFormatter: StatFormatter = (value) => formatNumber(value)\n\nconst formatSignedPoints: StatFormatter = (value) =>\n  formatSigned(value, (points) => formatPoints(points))\n\nconst formatSignedCount: StatFormatter = (value) => formatSigned(value)\n\nfunction formatStatValue(metric: Pick<StatMetric, 'formatter' | 'value'>) {\n  return (metric.formatter ?? defaultFormatter)(metric.value)\n}\n\nconst statColumns: Record<number, string> = {\n  1: 'grid-cols-1',\n  2: 'grid-cols-1 @md:grid-cols-2',\n  3: 'grid-cols-1 @lg:grid-cols-3',\n  4: 'grid-cols-1 @md:grid-cols-2 @3xl:grid-cols-4',\n  5: 'grid-cols-1 @xl:grid-cols-3 @4xl:grid-cols-5',\n  6: 'grid-cols-1 @lg:grid-cols-3 @4xl:grid-cols-6',\n}\n\n/** Columns for a row of `count` stats: one on a phone, then rows that never leave a stat alone. */\nfunction getStatColumns(count: number) {\n  return statColumns[count] ?? 'grid-cols-1 @md:grid-cols-2 @3xl:grid-cols-4'\n}\n\nfunction getStatPlaceholders(count: number): StatMetric[] {\n  return Array.from({ length: count }, (_, index) => ({\n    key: `placeholder-${index}`,\n    label: '',\n    value: 0,\n  }))\n}\n\nfunction StatGroup({ className, ...props }: ComponentProps<'dl'>) {\n  const state = useBlockState()\n\n  if (state === 'error' || state === 'empty') {\n    return (\n      <div\n        data-slot='stat-group'\n        className='border-t pt-3 in-data-[slot=card]:first:border-t-0 in-data-[slot=card]:first:pt-0'\n      >\n        <BlockStateText className='text-4xl font-light tracking-tight' variant='short' />\n      </div>\n    )\n  }\n\n  return (\n    <dl\n      data-slot='stat-group'\n      className={cn(\n        'grid gap-x-6 gap-y-5 in-data-[slot=card]:-ml-[calc(1.5rem+1px)] in-data-[slot=card]:[clip-path:inset(-100vh_-100vw_-100vh_calc(1.5rem+1px))] in-data-[slot=card]:first:*:border-t-0 in-data-[slot=card]:first:*:pt-0',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\nfunction Stat({ className, ...props }: ComponentProps<'div'>) {\n  return (\n    <div\n      className={cn(\n        'flex min-w-0 flex-col gap-1 border-t pt-3 in-data-[slot=card]:border-t-0 in-data-[slot=card]:border-l in-data-[slot=card]:pt-0 in-data-[slot=card]:pl-6',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\nfunction StatLabel({ children, className, ...props }: ComponentProps<'dt'>) {\n  const state = useBlockState()\n\n  return (\n    <dt className={cn('text-muted-foreground text-sm', className)} {...props}>\n      {state === 'loading' ? (\n        <>\n          <SkeletonText className='w-20' />\n          <span className='sr-only'>Loading</span>\n        </>\n      ) : (\n        children\n      )}\n    </dt>\n  )\n}\n\nfunction StatValue({ children, className, ...props }: ComponentProps<'dd'>) {\n  const state = useBlockState()\n\n  return (\n    <dd\n      className={cn(\n        'text-4xl font-light tracking-tight whitespace-nowrap tabular-nums',\n        className,\n      )}\n      {...props}\n    >\n      {state === 'loading' ? <SkeletonText className='w-[4em]' /> : children}\n    </dd>\n  )\n}\n\ninterface StatChangeProps {\n  className?: string\n  /** Follows the previous value in the sentence, such as `last month`. */\n  comparison?: string\n  metric: StatMetric\n  /** With `variant='arrow'`, shows the previous value after the change. @default true */\n  showPrevious?: boolean\n  /** `short` shows only the change, such as `Up 12.4%`, and gives screen readers the rest. @default 'sentence' */\n  variant?: 'sentence' | 'short' | 'arrow'\n}\n\nfunction StatChangeContent({\n  className,\n  comparison = 'the previous period',\n  metric,\n  showPrevious = true,\n  variant = 'sentence',\n}: StatChangeProps) {\n  const state = useBlockState()\n  const change = getKPIChange(metric)\n\n  if (state === 'loading') {\n    return (\n      <span className={cn('block text-sm', className)}>\n        <SkeletonText className={variant === 'sentence' ? 'w-48' : 'w-16'} />\n      </span>\n    )\n  }\n  if (change === undefined) return null\n\n  if (variant === 'arrow') {\n    return (\n      <span\n        className={cn(\n          'flex flex-wrap items-center gap-x-1.5 gap-y-0.5 text-xs',\n          className,\n        )}\n      >\n        <Trend\n          className='text-xs [&_svg]:size-3.5'\n          formatter={\n            metric.changeType === 'points'\n              ? formatSignedPoints\n              : metric.changeType === 'count'\n                ? formatSignedCount\n                : undefined\n          }\n          goodDirection={metric.goodDirection}\n          trend={change}\n          trendIcon='arrow'\n        />\n        {showPrevious && metric.previous !== undefined ? (\n          <span className='text-muted-foreground whitespace-nowrap'>\n            vs {(metric.formatter ?? defaultFormatter)(metric.previous)}\n          </span>\n        ) : (\n          <span className='sr-only'>compared with {comparison}</span>\n        )}\n      </span>\n    )\n  }\n\n  return (\n    <KPIChange\n      inline\n      changeType={metric.changeType}\n      className={className}\n      comparison={comparison}\n      goodDirection={metric.goodDirection}\n      previous={metric.previous}\n      value={metric.value}\n      variant={variant}\n    />\n  )\n}\n\nfunction StatChange(props: StatChangeProps) {\n  return (\n    <dd>\n      <StatChangeContent {...props} />\n    </dd>\n  )\n}\n\ninterface StatSparklineProps {\n  animated?: boolean\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  data: number[]\n  /** Formats a point's value in the tooltip. */\n  formatter?: StatFormatter\n  label: string\n  /** Names each point in the tooltip, in the same order as `data`. */\n  labels?: string[]\n}\n\nfunction StatSparkline({\n  animated = true,\n  className,\n  color = 'var(--color-chart-2)',\n  data,\n  formatter = defaultFormatter,\n  label,\n  labels,\n}: StatSparklineProps) {\n  const state = useBlockState()\n  if (state === 'loading') {\n    return (\n      <div\n        aria-hidden\n        className={cn(\n          'bg-muted h-8 w-full animate-pulse rounded-md motion-reduce:animate-none',\n          className,\n        )}\n      />\n    )\n  }\n  if (data.length < 2) return null\n\n  return (\n    <div role='img' aria-label={label} className={cn('h-8 w-full', className)}>\n      <TinyAreaChart\n        accessibilityLayer={false}\n        areas={[\n          {\n            dataKey: 'value',\n            fill: color,\n            fillOpacity: 0.2,\n            isAnimationActive: animated,\n            stroke: color,\n          },\n        ]}\n        data={data.map((value, index) => ({ label: labels?.[index], value }))}\n        formatter={formatter}\n        height='100%'\n        markLast='value'\n        scale='data'\n      />\n    </div>\n  )\n}\n\nexport {\n  Stat,\n  StatChange,\n  StatChangeContent,\n  StatGroup,\n  StatLabel,\n  StatSparkline,\n  StatValue,\n  formatStatValue,\n  getStatColumns,\n  getStatPlaceholders,\n}\n\nexport type { StatChangeProps, StatFormatter, StatMetric, StatSparklineProps }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/stat-group.tsx"
    }
  ],
  "type": "registry:component"
}
