{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comparison-04",
  "description": "Two values per row on a shared scale, a hollow dot for before and a filled one for after, with the rows that got worse in grey.",
  "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-04.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 {\n  Dumbbell,\n  DumbbellLegend,\n  getDelta,\n} from '@/registry/components/dashboardblocks/comparison'\nimport { KPIChange } from '@/registry/components/dashboardblocks/kpi'\nimport { formatNumber } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface BeforeAfterRow {\n  after: number\n  before: number\n  label: string\n}\n\ninterface Comparison4Props extends BlockStateProps {\n  afterLabel: string\n  beforeLabel: string\n  /** Defaults to a sentence saying how many rows improved and which most. */\n  description?: string\n  formatter?: (value: number) => string\n  /** Use `down` when a decrease is an improvement, such as load time. */\n  goodDirection?: 'up' | 'down'\n  /** Leave out, or pass none, to show the empty state. */\n  rows?: BeforeAfterRow[]\n  title: string\n  /** Names what each row measures, under the title. */\n  unitLabel?: string\n}\n\nconst exampleProps: Comparison4Props = {\n  afterLabel: 'After, Sep 15–24',\n  beforeLabel: 'Before, Sep 1–10',\n  formatter: (value) => `${formatNumber(value, { minimumFractionDigits: 1 })} s`,\n  goodDirection: 'down',\n  rows: [\n    { after: 1.6, before: 2.9, label: 'Home' },\n    { after: 2.1, before: 3.8, label: 'Product' },\n    { after: 1.9, before: 2.6, label: 'Search' },\n    { after: 2.4, before: 2.7, label: 'Checkout' },\n    { after: 1.5, before: 1.4, label: 'Account' },\n  ],\n  title: 'Page speed after the CDN migration',\n  unitLabel: 'p75 largest contentful paint',\n}\n\nconst Comparison4 = (props: Comparison4Props) => {\n  const {\n    afterLabel,\n    beforeLabel,\n    formatter = (value) => formatNumber(value),\n    goodDirection = 'up',\n    rows = [],\n    errorMessage,\n    status,\n    title,\n    unitLabel,\n  } = props\n  const max = Math.max(...rows.flatMap((row) => [row.before, row.after]), 0) * 1.1\n  const isGood = (row: BeforeAfterRow) =>\n    goodDirection === 'down' ? row.after < row.before : row.after > row.before\n  const improved = rows.filter(isGood)\n  const best = improved.reduce<BeforeAfterRow | undefined>(\n    (top, row) =>\n      !top ||\n      Math.abs(getDelta(row.after, row.before).percent) >\n        Math.abs(getDelta(top.after, top.before).percent)\n        ? row\n        : top,\n    undefined,\n  )\n  const {\n    description = `${improved.length} of ${rows.length} improved${\n      best\n        ? `, ${best.label} the most, from ${formatter(best.before)} to ${formatter(best.after)}`\n        : ''\n    }.`,\n  } = props\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider\n          errorMessage={errorMessage}\n          state={getBlockState(status, rows)}\n        >\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              <p className='text-muted-foreground text-sm'>\n                {unitLabel &&\n                  `${unitLabel.charAt(0).toUpperCase()}${unitLabel.slice(1)}. `}\n                <BlockReady>{description}</BlockReady>\n              </p>\n            </header>\n            <div className='flex flex-col border-t pt-3'>\n              <DumbbellLegend afterLabel={afterLabel} beforeLabel={beforeLabel} />\n              <BlockStateRows className='mt-1' rows={4} />\n              <ul className='mt-1 flex flex-col empty:hidden'>\n                {!status &&\n                  rows.map((row) => {\n                    const good = isGood(row)\n                    return (\n                      <li\n                        key={row.label}\n                        className='grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-4 gap-y-2 border-b py-2.5 last:border-b-0 @md:grid-cols-[6rem_minmax(0,1fr)_12rem]'\n                      >\n                        <span className='truncate text-sm font-medium'>{row.label}</span>\n                        <span className='flex items-center justify-end gap-2 text-sm whitespace-nowrap tabular-nums @md:order-last'>\n                          <span className='text-muted-foreground'>\n                            <span className='sr-only'>{beforeLabel}: </span>\n                            {formatter(row.before)}\n                          </span>\n                          <span aria-hidden className='text-muted-foreground'>\n                            →\n                          </span>\n                          <span className='font-medium'>\n                            <span className='sr-only'>{afterLabel}: </span>\n                            {formatter(row.after)}\n                          </span>\n                          <KPIChange\n                            className='w-20 text-right text-xs'\n                            comparison={beforeLabel}\n                            goodDirection={goodDirection}\n                            inline\n                            previous={row.before}\n                            value={row.after}\n                            variant='short'\n                          />\n                        </span>\n                        <Dumbbell\n                          after={row.after}\n                          before={row.before}\n                          className='col-span-2 @md:col-span-1'\n                          color={\n                            good\n                              ? 'var(--color-chart-2)'\n                              : 'var(--color-muted-foreground)'\n                          }\n                          max={max}\n                        />\n                      </li>\n                    )\n                  })}\n              </ul>\n            </div>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { Comparison4, exampleProps as comparison4ExampleProps, type Comparison4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/comparison/comparison-04.tsx"
    }
  ],
  "meta": {
    "shows": "Two values per row on a shared scale, a hollow dot for before and a filled one for after, with the rows that got worse in grey.",
    "size": "half",
    "usedWith": [
      "comparison-01",
      "comparison-03",
      "metric-list-03"
    ],
    "component": "Comparison4",
    "props": "{ afterLabel: string; beforeLabel: string; description?: string; formatter?: (value: number) => string; goodDirection?: \"up\" | \"down\"; rows?: { after: number; before: number; label: string }[]; title: string; unitLabel?: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
