{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comparison-03",
  "description": "The net change, then the change in each segment as bars either side of zero, from the biggest gain to the biggest drop.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/comparison.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/comparison/comparison-03.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { DivergingBar, getDelta } from '@/registry/components/dashboardblocks/comparison'\nimport { KPIChange, KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n  formatSigned,\n} from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface Segment {\n  current: number\n  label: string\n  previous: number\n}\n\ninterface Comparison3Props extends BlockStateProps {\n  /** Defaults to a sentence naming the biggest gain and drop. */\n  description?: string\n  formatter?: (value: number) => string\n  /** Leave out, or pass none, to show the empty state. */\n  segments?: Segment[]\n  title: string\n}\n\nconst exampleProps: Comparison3Props = {\n  formatter: (value) => formatCurrency(value),\n  segments: [\n    { current: 18_498, label: 'Organic search', previous: 15_240 },\n    { current: 13_144, label: 'Paid search', previous: 12_880 },\n    { current: 8_276, label: 'Email', previous: 6_310 },\n    { current: 4_120, label: 'Social', previous: 4_980 },\n    { current: 2_410, label: 'Referral', previous: 2_150 },\n    { current: 1_762, label: 'Affiliates', previous: 2_330 },\n  ],\n  title: 'What changed',\n}\n\nconst Comparison3 = (props: Comparison3Props) => {\n  const {\n    formatter = (value) => formatNumber(value, { maximumFractionDigits: 0 }),\n    segments = [],\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, segments)\n  const rows = segments\n    .map((segment) => ({ ...segment, ...getDelta(segment.current, segment.previous) }))\n    .sort((a, b) => b.difference - a.difference)\n  const max = Math.max(...rows.map((row) => Math.abs(row.difference)), 0)\n  const net = rows.reduce((sum, row) => sum + row.difference, 0)\n  const before = rows.reduce((sum, row) => sum + row.previous, 0)\n  const signed = (value: number) => formatSigned(value, formatter)\n  const gain = rows[0]\n  const drop = rows[rows.length - 1]\n  const sentences = [\n    gain &&\n      gain.difference > 0 &&\n      `${gain.label} added the most, ${signed(gain.difference)}.`,\n    drop &&\n      drop.difference < 0 &&\n      `${drop.label} fell the most, ${signed(drop.difference)}.`,\n  ].filter(Boolean)\n  const {\n    description = sentences.length > 0 ? sentences.join(' ') : 'No segment changed.',\n  } = props\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <BlockReady className='text-sm'>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </BlockReady>\n            </header>\n            <div className='flex flex-col gap-1 border-t pt-4'>\n              <span className='text-muted-foreground text-sm'>Net change</span>\n              <KPIValue value={net} format={signed} />\n              {(state || before !== 0) && (\n                <KPIChange comparison='before' previous={before} value={before + net} />\n              )}\n            </div>\n            {state === 'loading' && <BlockStateRows rows={5} />}\n            {!state && (\n              <ul className='flex flex-col'>\n                {rows.map((row) => (\n                  <li\n                    key={row.label}\n                    className='grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 gap-y-1.5 border-b py-2.5 last:border-b-0 @md:grid-cols-[8rem_minmax(0,1fr)_9rem]'\n                  >\n                    <span className='truncate text-sm'>{row.label}</span>\n                    <span className='text-right text-sm whitespace-nowrap tabular-nums @md:order-last'>\n                      <span\n                        className={cn(\n                          'font-medium',\n                          row.difference > 0\n                            ? 'text-emerald-700 dark:text-emerald-400'\n                            : row.difference < 0\n                              ? 'text-red-700 dark:text-red-400'\n                              : 'text-muted-foreground',\n                        )}\n                      >\n                        {signed(row.difference)}\n                      </span>{' '}\n                      <span className='text-muted-foreground text-xs'>\n                        {formatSigned(row.percent, (value) => formatPercent(value / 100))}\n                      </span>\n                    </span>\n                    <DivergingBar\n                      className='col-span-2 @md:col-span-1'\n                      color={\n                        row.difference < 0\n                          ? 'color-mix(in oklab, var(--color-chart-2) 30%, transparent)'\n                          : 'var(--color-chart-2)'\n                      }\n                      max={max}\n                      value={row.difference}\n                    />\n                  </li>\n                ))}\n              </ul>\n            )}\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { Comparison3, exampleProps as comparison3ExampleProps, type Comparison3Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/comparison/comparison-03.tsx"
    }
  ],
  "meta": {
    "shows": "The net change, then the change in each segment as bars either side of zero, from the biggest gain to the biggest drop.",
    "size": "half",
    "usedWith": [
      "comparison-01",
      "breakdown-01",
      "data-table-01"
    ],
    "component": "Comparison3",
    "props": "{ description?: string; formatter?: (value: number) => string; segments?: { current: number; label: string; previous: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
