{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kpi",
  "title": "KPI",
  "description": "Primitives for KPIs: a block with a top rule, a value that counts up with the final value read out, the change vs the previous period written as a sentence, in percent or percentage points with the good direction, a chart wrapper with a text alternative, and named en-US formats.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/change-amount.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/animated-number.json",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/kpi.tsx",
      "content": "'use client'\n\nimport { AnimatedNumber } from '@/registry/components/dashboardblocks/animated-number'\nimport {\n  BlockStateText,\n  SkeletonText,\n  useBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { ChangeAmount } from '@/registry/components/dashboardblocks/change-amount'\nimport {\n  formatCompact,\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n  formatPoints,\n} from '@/registry/lib/dashboardblocks/format'\nimport type { ComponentProps, ReactNode } from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype KPIFormatter = (value: number) => string\n\ntype KPIFormat = 'number' | 'compact' | 'currency' | 'currency-compact' | 'percent'\n\nconst kpiFormats: Record<KPIFormat, KPIFormatter> = {\n  compact: formatCompact,\n  currency: (value) => formatCurrency(value),\n  'currency-compact': (value) => formatCurrency(value, { compact: true }),\n  number: (value) => formatNumber(value),\n  percent: (value) => formatPercent(value / 100, { maximumFractionDigits: 2 }),\n}\n\nfunction getKPIFormatter(format: KPIFormat | KPIFormatter = 'number'): KPIFormatter {\n  return typeof format === 'function' ? format : kpiFormats[format]\n}\n\ninterface KPIChangeInput {\n  /** `points` for a rate's change (`2.3 pts`), `count` for a plain difference (`6`). @default 'percent' */\n  changeType?: 'percent' | 'points' | 'count'\n  previous?: number\n  value?: number\n}\n\nconst round = (value: number) => Math.round(value * 10) / 10 + 0\n\nfunction getKPIChange({ changeType = 'percent', previous, value }: KPIChangeInput) {\n  if (previous === undefined || value === undefined) return undefined\n  if (changeType === 'points' || changeType === 'count') return round(value - previous)\n  if (previous === 0) return value === 0 ? 0 : undefined\n  return round(((value - previous) / Math.abs(previous)) * 100)\n}\n\nfunction KPI({ className, ...props }: ComponentProps<'div'>) {\n  return (\n    <div\n      data-slot='kpi'\n      className={cn(\n        'flex flex-col border-t pt-3 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 KPIContent({ className, ...props }: ComponentProps<'div'>) {\n  return (\n    <div\n      data-slot='kpi-content'\n      className={cn('flex flex-1 flex-col justify-between', className)}\n      {...props}\n    />\n  )\n}\n\nfunction KPITitle({ className, ...props }: ComponentProps<'p'>) {\n  return (\n    <p\n      data-slot='kpi-title'\n      className={cn('text-muted-foreground text-sm', className)}\n      {...props}\n    />\n  )\n}\n\ninterface KPIValueProps {\n  animated?: boolean\n  className?: string\n  format?: KPIFormat | KPIFormatter\n  /** Where an animated value counts from, such as the previous period's value. @default 0 */\n  from?: number\n  value?: number\n}\n\nfunction KPIValue({ animated = false, className, format, from, value }: KPIValueProps) {\n  const state = useBlockState() ?? (value === undefined ? 'empty' : undefined)\n  const formatter = getKPIFormatter(format)\n\n  return (\n    <div\n      data-slot='kpi-value'\n      className={cn('text-4xl font-light tracking-tight tabular-nums', className)}\n    >\n      {state === 'loading' ? (\n        <SkeletonText className='w-[4.5em]' />\n      ) : state || value === undefined ? (\n        <BlockStateText state={state} variant='short' />\n      ) : animated ? (\n        <>\n          <span aria-hidden>\n            <AnimatedNumber value={value} from={from} formatter={formatter} />\n          </span>\n          <span className='sr-only'>{formatter(value)}</span>\n        </>\n      ) : (\n        formatter(value)\n      )}\n    </div>\n  )\n}\n\ninterface KPIChangeProps extends KPIChangeInput {\n  className?: string\n  /** Renders a `span` instead of a `p`, for inside a button or another line of text. */\n  inline?: boolean\n  /** What the change is measured against, after `vs`, such as `last month`. A leading `in`, `on`, `at` or `by` is dropped. */\n  comparison: string\n  /** Use `down` for metrics like churn or latency, where a decrease is good. */\n  goodDirection?: 'up' | 'down'\n  /** `short` shows only the change, such as an up arrow and `12.5%`, and gives screen readers the rest. @default 'sentence' */\n  variant?: 'sentence' | 'short'\n}\n\nfunction KPIChange({\n  className,\n  comparison,\n  goodDirection,\n  inline = false,\n  variant = 'sentence',\n  ...input\n}: KPIChangeProps) {\n  const state = useBlockState()\n  const change = getKPIChange(input)\n\n  if (state === 'loading') {\n    return (\n      <BlockStateText\n        className={cn('text-sm', className)}\n        variant={variant === 'short' ? 'short' : 'sentence'}\n      />\n    )\n  }\n  if (state || change === undefined) return null\n\n  const good = goodDirection === 'down' ? change < 0 : change > 0\n  const against = comparison.replace(/^(in|on|at|by) /, '')\n  const rest = against ? ` vs ${against}` : null\n\n  const Element = inline ? 'span' : 'p'\n  const amount =\n    input.changeType === 'points'\n      ? formatPoints(Math.abs(change))\n      : input.changeType === 'count'\n        ? formatNumber(Math.abs(change))\n        : formatPercent(Math.abs(change) / 100)\n\n  return (\n    <Element\n      data-slot='kpi-change'\n      className={cn('text-muted-foreground text-sm', inline && 'block', className)}\n    >\n      <ChangeAmount\n        change={change}\n        className={cn(\n          'font-medium tabular-nums',\n          change !== 0 &&\n            (good\n              ? 'text-emerald-700 dark:text-emerald-400'\n              : 'text-red-700 dark:text-red-400'),\n        )}\n      >\n        {amount}\n      </ChangeAmount>\n      {variant === 'short' ? <span className='sr-only'>{rest}</span> : rest}\n    </Element>\n  )\n}\n\ninterface KPIChartProps {\n  children: ReactNode\n  className?: string\n  label: string\n}\n\nfunction KPIChart({ children, className, label }: KPIChartProps) {\n  const state = useBlockState()\n\n  return (\n    <div\n      role={state ? undefined : 'img'}\n      aria-label={state ? undefined : label}\n      className={cn('w-full', className)}\n    >\n      {children}\n    </div>\n  )\n}\n\nfunction describeSeries(\n  title: string,\n  points: { label: string; value: number }[],\n  format?: KPIFormat | KPIFormatter,\n) {\n  if (points.length < 2) return title\n  const formatter = getKPIFormatter(format)\n  const first = points[0]\n  const last = points[points.length - 1]\n  const peak = points.reduce((max, point) => (point.value > max.value ? point : max))\n  return `${title}: from ${formatter(first.value)} on ${first.label} to ${formatter(last.value)} on ${last.label}, highest ${formatter(peak.value)} on ${peak.label}`\n}\n\nexport {\n  KPI,\n  KPIChange,\n  KPIChart,\n  KPIContent,\n  KPITitle,\n  KPIValue,\n  describeSeries,\n  getKPIChange,\n  getKPIFormatter,\n  kpiFormats,\n}\n\nexport type {\n  KPIChangeInput,\n  KPIChangeProps,\n  KPIChartProps,\n  KPIFormat,\n  KPIFormatter,\n  KPIValueProps,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/kpi.tsx"
    }
  ],
  "type": "registry:component"
}
