{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "compare-table",
  "type": "registry:ui",
  "title": "Compare Table",
  "description": "Two to four things side by side, attribute by attribute: a table whose first column (the attribute names) stays in view while the things scroll sideways inside it on narrow screens.",
  "categories": [
    "ui"
  ],
  "dependencies": [],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/ui/compare-table.tsx",
      "type": "registry:ui",
      "target": "components/ui/compare-table.tsx",
      "content": "/**\n * @name compare-table\n * @category ui\n * @description Two to four things side by side, attribute by attribute: a table whose first column (the attribute names) stays in view while the things scroll sideways inside it on narrow screens.\n * @when Helping someone choose between a few similar things: plans, products, places, quotes. Give every thing the same rows, in the same order. For plain rows of data use table; for more than four things let people pick which to compare first; for one thing's details use a description list or card.\n * @example\n * <CompareTable label=\"Compare the two plans\" columns={2} onlyDifferences={onlyDifferences}>\n *   <CompareHeader corner=\"Plan\">\n *     <CompareColumn><CompareColumnTitle>Starter</CompareColumnTitle><Button size=\"sm\">Choose</Button></CompareColumn>\n *     <CompareColumn><CompareColumnTitle>Team</CompareColumnTitle><Button size=\"sm\">Choose</Button></CompareColumn>\n *   </CompareHeader>\n *   <CompareGroup title=\"Basics\">\n *     <CompareRow label=\"Price a month\">\n *       <CompareCell value={12} best>€12</CompareCell>\n *       <CompareCell value={29}>€29</CompareCell>\n *     </CompareRow>\n *     <CompareRow label=\"Support\">\n *       <CompareCell value=\"email\">By email</CompareCell>\n *       <CompareCell value=\"email\">By email</CompareCell>\n *     </CompareRow>\n *   </CompareGroup>\n * </CompareTable>\n */\nimport { Children, createContext, isValidElement, use, type ComponentProps, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/cn\";\n\ntype CompareContextValue = { columns: number; bestLabel: string };\n\nconst CompareContext = createContext<CompareContextValue>({ columns: 2, bestLabel: \"Best\" });\n\nexport type CompareTableProps = Omit<ComponentProps<\"table\">, \"aria-label\"> & {\n  /** The table's name, read out for the table and for its scroll area (\"Compare the three plans\"). */\n  label: string;\n  /** How many things are compared (the number of CompareColumn). */\n  columns: number;\n  /** Hide the rows in which every thing has the same value (rows that carry `data-same`). The project owns the switch. */\n  onlyDifferences?: boolean;\n  /** The word shown in a cell marked `best`. Translate it with the interface. */\n  bestLabel?: string;\n  /** Classes for the outer frame (the table itself takes `className`). */\n  frameClassName?: string;\n};\n\n/**\n * A real table: screen readers announce each cell with its thing (column header) and its attribute (row\n * header). The frame is a container: when it is too narrow for every thing, the things scroll sideways\n * inside it, one or two to a view and snapping, with the attribute names held at the start; the page itself\n * never scrolls sideways. The scroll area can be focused and then scrolled with the arrow keys.\n */\nexport function CompareTable({ label, columns, onlyDifferences = false, bestLabel = \"Best\", className, frameClassName, children, ...props }: CompareTableProps) {\n  return (\n    <div\n      data-slot=\"compare-table\"\n      data-only-differences={onlyDifferences ? \"\" : undefined}\n      className={cn(\"group/compare @container w-full min-w-0 rounded-surface border bg-card text-card-foreground\", frameClassName)}\n    >\n      <div\n        data-slot=\"compare-table-scroll\"\n        role=\"region\"\n        aria-label={label}\n        tabIndex={0}\n        className=\"w-full snap-x snap-mandatory scroll-ps-(--compare-label) overflow-x-auto overscroll-x-contain rounded-surface [--compare-label:7rem] [--compare-per-view:1.2] focus-visible:-outline-offset-2 motion-reduce:snap-none @sm:[--compare-per-view:2.2] @xl:[--compare-label:11rem]\"\n      >\n        <table aria-label={label} className={cn(\"w-max min-w-full border-separate border-spacing-0 text-start text-sm\", className)} {...props}>\n          <CompareContext value={{ columns, bestLabel }}>{children}</CompareContext>\n        </table>\n      </div>\n    </div>\n  );\n}\n\n/** What the first column of every row shares: held at the inline start, on an opaque ground, with a hairline. */\nconst sticky = \"sticky start-0 z-10 w-(--compare-label) max-w-(--compare-label) min-w-(--compare-label) border-e bg-card\";\n\nexport type CompareHeaderProps = Omit<ComponentProps<\"thead\">, \"children\"> & {\n  /** What the first column holds, as its heading (\"Feature\"). Shown small; pass a visually hidden span to hide it. */\n  corner?: ReactNode;\n  /** One CompareColumn for each thing. */\n  children: ReactNode;\n};\n\nexport function CompareHeader({ corner, className, children, ...props }: CompareHeaderProps) {\n  return (\n    <thead data-slot=\"compare-header\" className={className} {...props}>\n      <tr>\n        {corner === undefined ? (\n          <td className={cn(sticky, \"border-b\")} />\n        ) : (\n          <th scope=\"col\" className={cn(sticky, \"border-b px-4 py-3 text-start align-bottom text-xs font-medium text-muted-foreground\")}>\n            {corner}\n          </th>\n        )}\n        {children}\n      </tr>\n    </thead>\n  );\n}\n\n/**\n * One thing: a column header. It can hold a picture, a name (CompareColumnTitle) and an action. Its width is\n * what sets how many things fit in view: a little over one on a narrow frame, a little over two on a medium\n * one (the part of the next thing that shows is the hint that there is more), and all of them once there is room.\n */\nexport function CompareColumn({ className, ...props }: ComponentProps<\"th\">) {\n  return (\n    <th\n      scope=\"col\"\n      data-slot=\"compare-column\"\n      className={cn(\n        \"w-[calc((100cqw-var(--compare-label))/var(--compare-per-view))] min-w-[calc((100cqw-var(--compare-label))/var(--compare-per-view))] snap-start border-b px-4 py-3 text-start align-bottom font-normal @2xl:w-auto @2xl:min-w-40\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\n/** The thing's name inside its column header. */\nexport function CompareColumnTitle({ className, ...props }: ComponentProps<\"span\">) {\n  return <span data-slot=\"compare-column-title\" className={cn(\"mb-2 block text-base leading-tight font-medium\", className)} {...props} />;\n}\n\nexport type CompareGroupProps = Omit<ComponentProps<\"tbody\">, \"title\"> & {\n  /** The band's heading (\"Basics\", \"Support\"). Leave it out for rows without a band. */\n  title?: ReactNode;\n};\n\n/** A band of rows under a heading. With `onlyDifferences`, a band whose rows are all the same is hidden with them. */\nexport function CompareGroup({ title, className, children, ...props }: CompareGroupProps) {\n  const { columns } = use(CompareContext);\n  return (\n    <tbody\n      data-slot=\"compare-group\"\n      className={cn(\"group-data-[only-differences]/compare:[&:not(:has(tr[data-slot=compare-row]:not([data-same])))]:hidden\", className)}\n      {...props}\n    >\n      {title === undefined ? null : (\n        <tr data-slot=\"compare-group-title\">\n          <th scope=\"rowgroup\" colSpan={columns + 1} className=\"border-b bg-muted p-0 text-start text-xs font-medium tracking-wide text-foreground uppercase\">\n            {/* The heading spans the table, so its words are held at the start as the things scroll. */}\n            <span className=\"sticky start-0 inline-block px-4 py-2\">{title}</span>\n          </th>\n        </tr>\n      )}\n      {children}\n    </tbody>\n  );\n}\n\n/** Whether every cell of a row carries the same comparable `value`. Cells without one make the row \"not the same\". */\nfunction allSame(children: ReactNode): boolean {\n  const values: unknown[] = [];\n  for (const child of Children.toArray(children)) {\n    if (!isValidElement<{ value?: unknown }>(child)) continue;\n    if (child.props.value === undefined) return false;\n    values.push(child.props.value);\n  }\n  return values.length > 1 && values.every((value) => Object.is(value, values[0]));\n}\n\nexport type CompareRowProps = ComponentProps<\"tr\"> & {\n  /** The attribute's name: the row header, held in view. */\n  label: ReactNode;\n  /** A line under the name: a unit, a short explanation. */\n  hint?: ReactNode;\n  /** Whether every thing has the same value. Worked out from the cells' `value` when left out. */\n  same?: boolean;\n};\n\n/** One attribute. Its CompareCell children are in the same order as the columns. */\nexport function CompareRow({ label, hint, same, className, children, ...props }: CompareRowProps) {\n  const isSame = same ?? allSame(children);\n  return (\n    <tr\n      data-slot=\"compare-row\"\n      data-same={isSame ? \"\" : undefined}\n      className={cn(\"group/row group-data-[only-differences]/compare:data-[same]:hidden\", className)}\n      {...props}\n    >\n      <th scope=\"row\" className={cn(sticky, \"border-b px-4 py-3 text-start align-top font-medium group-last/row:border-b-0 [tbody:not(:last-child)_&]:border-b\")}>\n        <span className=\"block hyphens-auto break-words\">{label}</span>\n        {hint ? <span className=\"mt-0.5 block text-xs font-normal text-muted-foreground\">{hint}</span> : null}\n      </th>\n      {children}\n    </tr>\n  );\n}\n\nexport type CompareCellProps = ComponentProps<\"td\"> & {\n  /** What this cell is compared by (a number, a string, a boolean), so the row can tell whether all things are the same. */\n  value?: string | number | boolean | null;\n  /** Marks this as the best in its row: a stronger look and the table's `bestLabel` word. */\n  best?: boolean;\n};\n\nexport function CompareCell({ value, best = false, className, children, ...props }: CompareCellProps) {\n  const { bestLabel } = use(CompareContext);\n  void value;\n  return (\n    <td\n      data-slot=\"compare-cell\"\n      data-best={best ? \"\" : undefined}\n      className={cn(\"border-b px-4 py-3 align-top group-last/row:border-b-0 data-[best]:font-medium [tbody:not(:last-child)_&]:border-b\", className)}\n      {...props}\n    >\n      {children}\n      {best ? (\n        <span data-slot=\"compare-best\" className=\"ms-2 inline-flex h-5 items-center rounded-control border border-primary bg-highlight-soft px-1.5 align-middle text-xs font-medium whitespace-nowrap text-foreground\">\n          {bestLabel}\n        </span>\n      ) : null}\n    </td>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Helping someone choose between a few similar things: plans, products, places, quotes. Give every thing the same rows, in the same order. For plain rows of data use table; for more than four things let people pick which to compare first; for one thing's details use a description list or card.",
    "example": "<CompareTable label=\"Compare the two plans\" columns={2} onlyDifferences={onlyDifferences}>\n  <CompareHeader corner=\"Plan\">\n    <CompareColumn><CompareColumnTitle>Starter</CompareColumnTitle><Button size=\"sm\">Choose</Button></CompareColumn>\n    <CompareColumn><CompareColumnTitle>Team</CompareColumnTitle><Button size=\"sm\">Choose</Button></CompareColumn>\n  </CompareHeader>\n  <CompareGroup title=\"Basics\">\n    <CompareRow label=\"Price a month\">\n      <CompareCell value={12} best>€12</CompareCell>\n      <CompareCell value={29}>€29</CompareCell>\n    </CompareRow>\n    <CompareRow label=\"Support\">\n      <CompareCell value=\"email\">By email</CompareCell>\n      <CompareCell value=\"email\">By email</CompareCell>\n    </CompareRow>\n  </CompareGroup>\n</CompareTable>",
    "client": false
  }
}
