{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "table-row-presence",
  "title": "Table row presence",
  "description": "Data table where new rows grow in with a brief highlight, removed rows slide out and collapse, and re-sorting glides every row to its new position.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/components/lists/table-row-presence.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion, type Transition } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\n\nconst CSS = `\n@keyframes pc-table-row-presence-flash {\n  0%, 35% { background-color: color-mix(in oklab, var(--primary) 11%, transparent) }\n  100% { background-color: transparent }\n}\n.pc-table-row-presence-new { animation: pc-table-row-presence-flash 2.2s ease-out both }\n@media (prefers-reduced-motion: reduce) { .pc-table-row-presence-new { animation-duration: 0.01s } }\n`\n\nexport type TableRowPresenceColumn<T> = {\n  /** Unique column id. */\n  key: string\n  /** Header cell content. */\n  header: React.ReactNode\n  /** Renders this column's cell for a row. */\n  cell: (row: T) => React.ReactNode\n  /** Classes for both the header and body cells, e.g. \"hidden sm:table-cell\" or \"w-24\". */\n  className?: string\n  /** Text alignment of the header and body cells. Default \"left\". Left and right are mirrored under RTL (they map to start and end). */\n  align?: \"left\" | \"right\" | \"center\"\n}\n\nexport type TableRowPresenceProps<T> = Omit<React.ComponentProps<\"table\">, \"children\"> & {\n  /** Rows in display order. Add, remove or re-sort this array and the table animates the difference. */\n  rows: T[]\n  /** Column definitions, in order. */\n  columns: TableRowPresenceColumn<T>[]\n  /** Stable unique id of a row; rows are matched across renders by it. */\n  getRowId: (row: T) => string\n  /** Briefly tint rows that arrive after the first render. */\n  highlightNew?: boolean\n  /** Shown in a full-width row when there are no rows. */\n  empty?: React.ReactNode\n  /** Table caption, shown below the table. */\n  caption?: React.ReactNode\n  /** Makes rows clickable: pointer cursor, and each row becomes focusable and activates with Enter or Space. */\n  onRowClick?: (row: T) => void\n}\n\nconst ALIGN = { left: \"text-start\", right: \"text-end\", center: \"text-center\" } as const\n\n// Table rows can't animate their height, so every cell wraps its content in a div that grows from 0 to auto.\n// Neighbours then slide down or close the gap in normal flow, while `layout` animates re-sorting.\nconst cellVariants = {\n  hidden: { height: 0, opacity: 0 },\n  shown: { height: \"auto\", opacity: 1 },\n}\n\ntype RowProps<T> = {\n  row: T\n  columns: TableRowPresenceColumn<T>[]\n  fresh: boolean\n  highlight: boolean\n  reduce: boolean\n  /** Mirrors the slide-in/out direction for right-to-left layouts. */\n  rtl: boolean\n  onRowClick?: (row: T) => void\n}\n\nfunction PresenceRow<T>({ row, columns, fresh, highlight, reduce, rtl, onRowClick }: RowProps<T>) {\n  // Decided once on mount: rows present on first render are not \"new\".\n  const [isNew] = React.useState(fresh)\n  const t: Transition = reduce ? { duration: 0 } : { type: \"spring\", bounce: 0, duration: 0.42 }\n  return (\n    <motion.tr\n      layout={reduce ? false : \"position\"}\n      transition={t}\n      initial={{ opacity: 0, x: reduce ? 0 : rtl ? 12 : -12 }}\n      animate={{ opacity: 1, x: 0 }}\n      exit={{ opacity: 0, x: reduce ? 0 : rtl ? -24 : 24, transition: reduce ? { duration: 0 } : { duration: 0.2, ease: \"easeIn\" } }}\n      onClick={onRowClick ? () => onRowClick(row) : undefined}\n      tabIndex={onRowClick ? 0 : undefined}\n      onKeyDown={\n        onRowClick\n          ? (e) => {\n              // Only when the row itself has focus, so buttons and links inside cells keep their own keys.\n              if (e.target !== e.currentTarget || (e.key !== \"Enter\" && e.key !== \" \")) return\n              e.preventDefault()\n              onRowClick(row)\n            }\n          : undefined\n      }\n      className={cn(\n        \"relative border-b transition-colors last:border-b-0 hover:bg-muted/40\",\n        onRowClick && \"cursor-pointer outline-none focus-visible:bg-muted/40 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring\",\n        isNew && highlight && \"pc-table-row-presence-new\",\n      )}\n    >\n      {columns.map((col) => (\n        <td key={col.key} className={cn(\"p-0 align-middle\", ALIGN[col.align ?? \"left\"], col.className)}>\n          <motion.div\n            variants={cellVariants}\n            initial=\"hidden\"\n            animate=\"shown\"\n            exit=\"hidden\"\n            transition={reduce ? { duration: 0 } : { height: { type: \"spring\", bounce: 0, duration: 0.42 }, opacity: { duration: 0.2 } }}\n            className=\"overflow-hidden\"\n          >\n            <div className=\"px-3 py-2.5\">{col.cell(row)}</div>\n          </motion.div>\n        </td>\n      ))}\n    </motion.tr>\n  )\n}\n\n/** Data table whose rows slide in, collapse out and glide to new positions when sorted. */\nexport function TableRowPresence<T>({\n  rows,\n  columns,\n  getRowId,\n  highlightNew = true,\n  empty = \"No rows\",\n  caption,\n  onRowClick,\n  className,\n  ...props\n}: TableRowPresenceProps<T>) {\n  const reduce = useReducedMotion() ?? false\n  const [mounted, setMounted] = React.useState(false)\n  const [rtl, setRtl] = React.useState(false)\n  const headRef = React.useRef<HTMLTableSectionElement>(null)\n  React.useEffect(() => {\n    setMounted(true)\n    if (headRef.current) setRtl(getComputedStyle(headRef.current).direction === \"rtl\")\n  }, [])\n\n  return (\n    <>\n      <style href=\"pc-table-row-presence\" precedence=\"default\">\n        {CSS}\n      </style>\n      <table className={cn(\"w-full caption-bottom border-collapse text-sm\", className)} {...props}>\n        {caption && <caption className=\"mt-3 text-xs text-muted-foreground\">{caption}</caption>}\n        <thead ref={headRef}>\n          <tr className=\"border-b\">\n            {columns.map((col) => (\n              <th\n                key={col.key}\n                scope=\"col\"\n                className={cn(\n                  \"h-9 px-3 text-xs font-medium whitespace-nowrap text-muted-foreground\",\n                  ALIGN[col.align ?? \"left\"],\n                  col.className,\n                )}\n              >\n                {col.header}\n              </th>\n            ))}\n          </tr>\n        </thead>\n        <tbody>\n          <AnimatePresence initial={false}>\n            {rows.map((row) => (\n              <PresenceRow\n                key={getRowId(row)}\n                row={row}\n                columns={columns}\n                fresh={mounted}\n                highlight={highlightNew}\n                reduce={reduce}\n                rtl={rtl}\n                onRowClick={onRowClick}\n              />\n            ))}\n            {rows.length === 0 && (\n              <motion.tr\n                key=\"pc-empty\"\n                initial={{ opacity: 0 }}\n                animate={{ opacity: 1, transition: { delay: reduce ? 0 : 0.2 } }}\n                exit={{ opacity: 0, transition: { duration: 0 } }}\n              >\n                <td colSpan={columns.length} className=\"px-3 py-10 text-center text-sm text-muted-foreground\">\n                  {empty}\n                </td>\n              </motion.tr>\n            )}\n          </AnimatePresence>\n        </tbody>\n      </table>\n    </>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/table-row-presence.tsx"
    }
  ],
  "categories": [
    "components",
    "lists"
  ],
  "type": "registry:component"
}