{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comparison",
  "title": "Comparison",
  "description": "Primitives for comparisons: deltas, a two-proportion test with a 95% interval, diverging bars, dumbbells and an interval bar.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/use-reveal.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/comparison.tsx",
      "content": "'use client'\n\nimport { getKPIChange } from '@/registry/components/dashboardblocks/kpi'\nimport { useReveal } from '@/registry/hooks/use-reveal'\n\nimport { cn } from '@/lib/utils'\n\nfunction getDelta(current: number, previous: number) {\n  return {\n    difference: current - previous,\n    percent: getKPIChange({ previous, value: current }) ?? 0,\n  }\n}\n\ninterface ProportionTest {\n  /** Absolute difference in rate (b − a), with its 95% interval. */\n  difference: number\n  high: number\n  /** Relative lift of b over a, with its 95% interval, as fractions. */\n  lift: number\n  liftHigh: number\n  liftLow: number\n  low: number\n  rateA: number\n  rateB: number\n  significant: boolean\n}\n\nfunction compareProportions(\n  a: { conversions: number; visitors: number },\n  b: { conversions: number; visitors: number },\n): ProportionTest {\n  const rateA = a.visitors > 0 ? a.conversions / a.visitors : 0\n  const rateB = b.visitors > 0 ? b.conversions / b.visitors : 0\n  const se = Math.sqrt(\n    (a.visitors > 0 ? (rateA * (1 - rateA)) / a.visitors : 0) +\n      (b.visitors > 0 ? (rateB * (1 - rateB)) / b.visitors : 0),\n  )\n  const difference = rateB - rateA\n  const low = difference - 1.96 * se\n  const high = difference + 1.96 * se\n  return {\n    difference,\n    high,\n    lift: rateA > 0 ? difference / rateA : 0,\n    liftHigh: rateA > 0 ? high / rateA : 0,\n    liftLow: rateA > 0 ? low / rateA : 0,\n    low,\n    rateA,\n    rateB,\n    significant: low > 0 || high < 0,\n  }\n}\n\ninterface DivergingBarProps {\n  animated?: boolean\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  max: number\n  value: number\n}\n\nfunction DivergingBar({\n  animated = true,\n  className,\n  color = 'var(--color-chart-2)',\n  max,\n  value,\n}: DivergingBarProps) {\n  const { ref, revealed } = useReveal(animated)\n  const share = max > 0 ? Math.min(1, Math.abs(value) / max) : 0\n  const width = `${(revealed ? share : 0) * 50}%`\n\n  return (\n    <div ref={ref} aria-hidden className={cn('relative h-3 w-full', className)}>\n      <span className='bg-border absolute top-[-4px] bottom-[-4px] left-1/2 w-px' />\n      <span\n        className={cn(\n          'absolute top-0 h-full transition-[width] duration-700 ease-out motion-reduce:transition-none',\n          value < 0 ? 'right-1/2 rounded-l-[3px]' : 'left-1/2 rounded-r-[3px]',\n        )}\n        style={{ backgroundColor: color, width }}\n      />\n    </div>\n  )\n}\n\ninterface DumbbellProps {\n  after: number\n  before: number\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  max: number\n  /** @default 0 */\n  min?: number\n}\n\nfunction Dumbbell({\n  after,\n  before,\n  className,\n  color = 'var(--color-chart-2)',\n  max,\n  min = 0,\n}: DumbbellProps) {\n  const range = max - min || 1\n  const position = (value: number) => `${((value - min) / range) * 100}%`\n  const left = Math.min(before, after)\n  const right = Math.max(before, after)\n\n  return (\n    <div aria-hidden className={cn('relative h-4 w-full', className)}>\n      <span className='bg-muted absolute top-1/2 h-px w-full -translate-y-1/2' />\n      <span\n        className='absolute top-1/2 h-0.5 -translate-y-1/2 rounded-full'\n        style={{\n          backgroundColor: color,\n          left: position(left),\n          width: `${((right - left) / range) * 100}%`,\n        }}\n      />\n      <span\n        className='bg-background absolute top-1/2 size-3 -translate-1/2 rounded-full border-2'\n        style={{ borderColor: color, left: position(before) }}\n      />\n      <span\n        className='ring-background absolute top-1/2 size-3 -translate-1/2 rounded-full ring-2'\n        style={{ backgroundColor: color, left: position(after) }}\n      />\n    </div>\n  )\n}\n\nfunction DumbbellLegend({\n  afterLabel,\n  beforeLabel,\n  className,\n  color = 'var(--color-chart-2)',\n}: {\n  afterLabel: string\n  beforeLabel: string\n  className?: string\n  color?: string\n}) {\n  return (\n    <ul\n      className={cn(\n        'text-muted-foreground flex flex-wrap gap-x-4 gap-y-1 text-xs',\n        className,\n      )}\n    >\n      <li className='flex items-center gap-1.5'>\n        <span\n          aria-hidden\n          className='size-2.5 rounded-full border-2'\n          style={{ borderColor: color }}\n        />\n        {beforeLabel}\n      </li>\n      <li className='flex items-center gap-1.5'>\n        <span\n          aria-hidden\n          className='size-2.5 rounded-full'\n          style={{ backgroundColor: color }}\n        />\n        {afterLabel}\n      </li>\n    </ul>\n  )\n}\n\ninterface IntervalBarProps {\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  estimate: number\n  high: number\n  low: number\n  /** The axis runs from −extent to +extent, with zero in the middle. */\n  extent: number\n}\n\nfunction IntervalBar({\n  className,\n  color = 'var(--color-chart-2)',\n  estimate,\n  extent,\n  high,\n  low,\n}: IntervalBarProps) {\n  const position = (value: number) =>\n    `${Math.min(100, Math.max(0, ((value + extent) / (2 * extent)) * 100))}%`\n\n  return (\n    <div aria-hidden className={cn('relative h-6 w-full', className)}>\n      <span className='bg-border absolute top-1/2 h-px w-full -translate-y-1/2' />\n      <span className='bg-foreground/60 absolute top-0 bottom-0 left-1/2 w-px' />\n      <span\n        className='absolute top-1/2 h-2 -translate-y-1/2 rounded-full opacity-30'\n        style={{\n          backgroundColor: color,\n          left: position(low),\n          width: `calc(${position(high)} - ${position(low)})`,\n        }}\n      />\n      <span\n        className='ring-background absolute top-1/2 size-3 -translate-1/2 rounded-full ring-2'\n        style={{ backgroundColor: color, left: position(estimate) }}\n      />\n    </div>\n  )\n}\n\nexport {\n  DivergingBar,\n  Dumbbell,\n  DumbbellLegend,\n  IntervalBar,\n  compareProportions,\n  getDelta,\n}\n\nexport type { DivergingBarProps, DumbbellProps, IntervalBarProps, ProportionTest }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/comparison.tsx"
    }
  ],
  "type": "registry:component"
}
