{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "metric-list",
  "title": "Metric List",
  "description": "Primitives for compact lists of metrics: a row with value, the change written as words and a sparkline, a bar against a target with its status, and value formatting helpers.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/status-label.json",
    "https://www.dashboardblocks.com/r/radix/use-reveal.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/metric-list.tsx",
      "content": "'use client'\n\nimport { TinyAreaChart } from '@/registry/components/dashboardblocks/chart'\nimport { KPIChange, getKPIChange } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  StatusLabel,\n  type StatusTone,\n} from '@/registry/components/dashboardblocks/status-label'\nimport { useReveal } from '@/registry/hooks/use-reveal'\nimport {\n  formatCompact,\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n} from '@/registry/lib/dashboardblocks/format'\nimport type { ComponentProps } from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype MetricFormatter = (value: number) => string\n\ntype MetricFormat =\n  | 'number'\n  | 'compact'\n  | 'currency'\n  | 'currency-compact'\n  | 'percent'\n  | 'duration'\n  | 'milliseconds'\n\ninterface Metric {\n  /** @default 'percent' */\n  changeType?: 'percent' | 'points'\n  format?: MetricFormat | MetricFormatter\n  /** Use `down` for metrics like churn or latency, where a decrease is good. */\n  goodDirection?: 'up' | 'down'\n  /** Values over the period, oldest first, for the sparkline. */\n  history?: number[]\n  key: string\n  label: string\n  previous?: number\n  value: number\n}\n\nfunction formatDuration(seconds: number) {\n  const total = Math.round(Math.abs(seconds))\n  const hours = Math.floor(total / 3600)\n  const minutes = Math.floor((total % 3600) / 60)\n  const rest = total % 60\n  if (hours > 0) return `${hours}h ${String(minutes).padStart(2, '0')}m`\n  if (minutes > 0) return `${minutes}m ${String(rest).padStart(2, '0')}s`\n  return `${rest}s`\n}\n\nconst metricFormats: Record<MetricFormat, MetricFormatter> = {\n  compact: formatCompact,\n  currency: (value) => formatCurrency(value, { precision: 'exact' }),\n  'currency-compact': (value) => formatCurrency(value, { compact: true }),\n  duration: formatDuration,\n  milliseconds: (value) => `${formatNumber(value, { maximumFractionDigits: 0 })} ms`,\n  number: (value) => formatNumber(value),\n  percent: (value) => formatPercent(value / 100, { maximumFractionDigits: 2 }),\n}\n\nfunction getMetricFormatter(format: Metric['format'] = 'number'): MetricFormatter {\n  return typeof format === 'function' ? format : metricFormats[format]\n}\n\nfunction formatMetricValue(metric: Pick<Metric, 'format'>, value: number) {\n  return getMetricFormatter(metric.format)(value)\n}\n\nfunction getMetricChange({\n  changeType,\n  previous,\n  value,\n}: Pick<Metric, 'changeType' | 'previous' | 'value'>) {\n  return getKPIChange({ changeType, previous, value })\n}\n\nfunction describeHistory(\n  metric: Pick<Metric, 'format' | 'history' | 'label'>,\n  period = '',\n) {\n  const history = metric.history ?? []\n  if (history.length < 2) return metric.label\n  const first = history[0]\n  const last = history[history.length - 1]\n  const direction = last > first ? 'up' : last < first ? 'down' : 'flat'\n  const format = getMetricFormatter(metric.format)\n  return `${metric.label}${period ? `, ${period}` : ''}: ${direction} from ${format(first)} to ${format(last)}`\n}\n\nfunction MetricList({ className, ...props }: ComponentProps<'ul'>) {\n  return <ul className={cn('flex flex-col divide-y', className)} {...props} />\n}\n\ninterface MetricChangeProps {\n  className?: string\n  metric: Pick<Metric, 'changeType' | 'format' | 'goodDirection' | 'previous' | 'value'>\n  /** Follows the previous value, such as `last month`. @default 'the period before' */\n  comparison?: string\n  /** `sentence` also shows the previous value and the comparison. @default 'short' */\n  variant?: 'sentence' | 'short'\n}\n\nfunction MetricChange({\n  className,\n  comparison = 'the period before',\n  metric,\n  variant = 'short',\n}: MetricChangeProps) {\n  if (metric.previous === undefined) return null\n\n  return (\n    <KPIChange\n      changeType={metric.changeType}\n      className={cn(variant === 'short' && 'text-xs whitespace-nowrap', className)}\n      comparison={comparison}\n      goodDirection={metric.goodDirection}\n      previous={metric.previous}\n      value={metric.value}\n      variant={variant}\n    />\n  )\n}\n\ninterface MetricSparklineProps {\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?: MetricFormatter\n  label: string\n  /** Names each point in the tooltip, in the same order as `data`. */\n  labels?: string[]\n}\n\nfunction MetricSparkline({\n  animated = true,\n  className,\n  color = 'var(--color-chart-2)',\n  data,\n  formatter = metricFormats.number,\n  label,\n  labels,\n}: MetricSparklineProps) {\n  if (data.length < 2) return null\n\n  return (\n    <div role='img' aria-label={label} className={cn('h-7 w-16', className)}>\n      <TinyAreaChart\n        accessibilityLayer={false}\n        areas={[\n          {\n            dataKey: 'value',\n            fill: color,\n            isAnimationActive: animated,\n            stroke: color,\n            strokeWidth: 1.5,\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\ninterface MetricRowProps extends Omit<ComponentProps<'li'>, 'children'> {\n  comparison?: string\n  /** Names each point of the history in the sparkline's tooltip. */\n  labels?: string[]\n  metric: Metric\n  period?: string\n  /** @default true when the metric has a history */\n  showSparkline?: boolean\n  sparklineColor?: string\n}\n\nfunction MetricRow({\n  className,\n  comparison,\n  labels,\n  metric,\n  period,\n  showSparkline = (metric.history?.length ?? 0) > 1,\n  sparklineColor,\n  ...props\n}: MetricRowProps) {\n  return (\n    <li\n      className={cn(\n        'grid items-center gap-x-3 py-2.5 @md:gap-x-4',\n        showSparkline\n          ? 'grid-cols-[minmax(0,1fr)_3rem_5.5rem] @md:grid-cols-[minmax(0,1fr)_5rem_6rem_5rem]'\n          : 'grid-cols-[minmax(0,1fr)_5.5rem] @md:grid-cols-[minmax(0,1fr)_6rem_5rem]',\n        className,\n      )}\n      {...props}\n    >\n      <span className='text-sm'>{metric.label}</span>\n      {showSparkline && (\n        <div className='min-w-0'>\n          {metric.history && (\n            <MetricSparkline\n              className='w-full'\n              color={sparklineColor}\n              data={metric.history}\n              formatter={getMetricFormatter(metric.format)}\n              label={describeHistory(metric, period)}\n              labels={labels}\n            />\n          )}\n        </div>\n      )}\n      <div className='flex flex-col items-end gap-0.5 @md:contents'>\n        <span className='text-sm font-medium whitespace-nowrap tabular-nums @md:justify-self-end'>\n          {formatMetricValue(metric, metric.value)}\n        </span>\n        <MetricChange\n          className='@md:justify-self-end'\n          comparison={comparison}\n          metric={metric}\n        />\n      </div>\n    </li>\n  )\n}\n\ntype TargetStatus = 'met' | 'near' | 'missed'\n\ninterface TargetInput {\n  /** Use `down` when the target is a ceiling, like churn or latency. */\n  goodDirection?: 'up' | 'down'\n  target: number\n  /** How far short of the target, as a share of it, still counts as near, 0.05 by default. */\n  tolerance?: number\n  value: number\n}\n\nfunction getTargetStatus({\n  goodDirection = 'up',\n  target,\n  tolerance = 0.05,\n  value,\n}: TargetInput): TargetStatus {\n  const gap = goodDirection === 'up' ? target - value : value - target\n  if (gap <= 0) return 'met'\n  return gap <= Math.abs(target) * tolerance ? 'near' : 'missed'\n}\n\nconst targetStatusConfig: Record<TargetStatus, { label: string; tone: StatusTone }> = {\n  met: { label: 'On target', tone: 'good' },\n  missed: { label: 'Off target', tone: 'bad' },\n  near: { label: 'Near target', tone: 'warning' },\n}\n\nfunction TargetStatusLabel({\n  className,\n  status,\n}: {\n  className?: string\n  status: TargetStatus\n}) {\n  const config = targetStatusConfig[status]\n  return (\n    <StatusLabel className={className} tone={config.tone}>\n      {config.label}\n    </StatusLabel>\n  )\n}\n\ninterface MetricTargetBarProps {\n  animated?: boolean\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  domain?: [number, number]\n  target: number\n  value: number\n}\n\nfunction MetricTargetBar({\n  animated = true,\n  className,\n  color = 'var(--color-chart-2)',\n  domain,\n  target,\n  value,\n}: MetricTargetBarProps) {\n  const { ref, revealed } = useReveal(animated)\n  const [min, max] = domain ?? [0, Math.max(value, target) * 1.25]\n  const span = max - min || 1\n  const share = (input: number) => Math.min(1, Math.max(0, (input - min) / span))\n\n  return (\n    <div ref={ref} aria-hidden className={cn('relative h-1.5 w-full', className)}>\n      <div className='bg-muted h-full w-full overflow-hidden rounded-full'>\n        <div\n          className='h-full rounded-full transition-[width] duration-1000 ease-[cubic-bezier(0.33,1,0.68,1)] motion-reduce:transition-none'\n          style={{\n            backgroundColor: color,\n            width: `${(revealed ? share(value) : 0) * 100}%`,\n          }}\n        />\n      </div>\n      <span\n        className='bg-foreground ring-background absolute top-1/2 h-3.5 w-0.5 -translate-x-1/2 -translate-y-1/2 rounded-full ring-2'\n        style={{ left: `${share(target) * 100}%` }}\n      />\n    </div>\n  )\n}\n\nfunction MetricTargetKey({\n  className,\n  label = 'Target',\n}: {\n  className?: string\n  label?: string\n}) {\n  return (\n    <span\n      className={cn(\n        'text-muted-foreground inline-flex items-center gap-1.5 text-xs',\n        className,\n      )}\n    >\n      <span aria-hidden className='bg-foreground h-3 w-0.5 rounded-full' />\n      {label}\n    </span>\n  )\n}\n\nexport {\n  MetricChange,\n  MetricList,\n  MetricRow,\n  MetricSparkline,\n  MetricTargetBar,\n  MetricTargetKey,\n  TargetStatusLabel,\n  describeHistory,\n  formatDuration,\n  formatMetricValue,\n  getMetricChange,\n  getMetricFormatter,\n  getTargetStatus,\n  metricFormats,\n  targetStatusConfig,\n}\n\nexport type {\n  Metric,\n  MetricChangeProps,\n  MetricFormat,\n  MetricFormatter,\n  MetricRowProps,\n  MetricSparklineProps,\n  MetricTargetBarProps,\n  TargetInput,\n  TargetStatus,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/metric-list.tsx"
    }
  ],
  "type": "registry:component"
}
