{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "table",
  "title": "Table",
  "description": "The parts every table is drawn with: rows, headers, row headers, group headers and cells that pad the edges of the table, numbers aligned to the end, a compact density for a small table in a headline block, rows that open a record or a page on click, a sticky first column, a sideways scroller, inline bars, a pager, and loading, empty and error rows.",
  "registryDependencies": [
    "button",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/details.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/link.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/table.tsx",
      "content": "'use client'\n\n/* oxlint-disable jsx-a11y/no-redundant-roles, jsx-a11y/no-interactive-element-to-noninteractive-role -- explicit roles keep the table's semantics when a class changes its display */\n\nimport {\n  BlockStateText,\n  SkeletonText,\n  useBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { Details } from '@/registry/components/dashboardblocks/details'\nimport { Link, type LinkProps } from '@/registry/components/dashboardblocks/link'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { formatNumber } from '@/registry/lib/dashboardblocks/format'\nimport {\n  type ComponentProps,\n  type ReactNode,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from 'react'\n\nimport { Button } from '@/components/ui/button'\n\nimport { cn } from '@/lib/utils'\n\ntype DataTableDensity = 'default' | 'compact'\n\ninterface DataTableProps extends ComponentProps<'table'> {\n  /** Read by screen readers only, and names the table's scrolling region. */\n  caption?: ReactNode\n  /** `'compact'` fits a small table inside a headline block: rows `py-2.5`, no fixed row height. @default 'default' */\n  density?: DataTableDensity\n  /** `false` leaves out the scrolling wrapper, for a parent that scrolls the table itself (a virtual list). @default true */\n  scroll?: boolean\n}\n\nfunction useOverflows() {\n  const ref = useRef<HTMLDivElement>(null)\n  const [overflows, setOverflows] = useState(false)\n\n  useEffect(() => {\n    const container = ref.current\n    if (!container) return\n    const observer = new ResizeObserver(() => {\n      container.style.setProperty(\n        '--data-table-visible-width',\n        `${container.clientWidth}px`,\n      )\n      setOverflows(container.scrollWidth > container.clientWidth)\n    })\n    observer.observe(container)\n    if (container.firstElementChild) observer.observe(container.firstElementChild)\n    return () => observer.disconnect()\n  }, [])\n\n  return [ref, overflows] as const\n}\n\nfunction DataTable({\n  caption,\n  children,\n  className,\n  density = 'default',\n  scroll = true,\n  ...props\n}: DataTableProps) {\n  const captionId = useId()\n  const [containerRef, overflows] = useOverflows()\n\n  const table = (\n    <table\n      role='table'\n      data-slot='data-table'\n      className={cn(\n        'w-full border-collapse text-sm',\n        density === 'compact' &&\n          '[--data-table-cell-height:auto] [--data-table-cell-py:0.625rem] [--data-table-head-height:2rem]',\n        className,\n      )}\n      {...props}\n    >\n      {caption && (\n        <caption id={captionId} className='sr-only'>\n          {caption}\n        </caption>\n      )}\n      {children}\n    </table>\n  )\n\n  if (!scroll) return table\n\n  return (\n    <div\n      ref={containerRef}\n      data-slot='data-table-container'\n      role={overflows && caption ? 'region' : undefined}\n      aria-labelledby={overflows && caption ? captionId : undefined}\n      // oxlint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- a table wider than its box scrolls, and keyboard users need to reach the columns that don't fit\n      tabIndex={overflows ? 0 : undefined}\n      className='focus-visible:ring-ring/50 relative isolate w-full overflow-x-auto outline-none [scrollbar-width:thin] focus-visible:ring-3 focus-visible:ring-inset'\n    >\n      {table}\n    </div>\n  )\n}\n\nfunction DataTableHeader({ className, ...props }: ComponentProps<'thead'>) {\n  return <thead role='rowgroup' className={cn('border-b', className)} {...props} />\n}\n\nfunction DataTableBody(props: ComponentProps<'tbody'>) {\n  return <tbody role='rowgroup' {...props} />\n}\n\nfunction DataTableFooter({ className, ...props }: ComponentProps<'tfoot'>) {\n  return (\n    <tfoot role='rowgroup' className={cn('border-t font-medium', className)} {...props} />\n  )\n}\n\ninterface DataTableRowProps extends ComponentProps<'tr'> {\n  highlighted?: boolean\n}\n\nconst OPEN_ITEM =\n  'data-open:bg-muted/50 data-open:relative data-open:before:absolute data-open:before:inset-y-0 data-open:before:start-0 data-open:before:w-0.5 data-open:before:bg-foreground'\n\n// A table row can't hold a positioned ::before, which the browser lays out as a cell.\nconst OPEN_ROW =\n  'data-open:bg-muted/50 data-open:[&>:first-child]:shadow-[inset_2px_0_0_var(--color-foreground)] rtl:data-open:[&>:first-child]:shadow-[inset_-2px_0_0_var(--color-foreground)]'\n\nconst ROW_HOST =\n  'has-data-[slot=data-table-row-trigger]:not-data-highlighted:not-data-open:hover:bg-muted/50 has-data-[slot=data-table-row-trigger]:relative has-data-[slot=data-table-row-trigger]:[&_:is(a,button,input,select,textarea,[role=checkbox],[role=radiogroup]):not([data-slot=data-table-row-trigger])]:relative has-data-[slot=data-table-row-trigger]:[&_:is(a,button,input,select,textarea,[role=checkbox],[role=radiogroup]):not([data-slot=data-table-row-trigger])]:z-10 transition-colors'\n\nfunction DataTableRow({ className, highlighted, ...props }: DataTableRowProps) {\n  return (\n    <tr\n      role='row'\n      data-highlighted={highlighted || undefined}\n      className={cn(\n        'group/data-table-row data-[state=selected]:bg-muted/50 data-highlighted:bg-muted border-b last:border-b-0',\n        ROW_HOST,\n        OPEN_ROW,\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\n/** A list row that a `DataTableRowLink` or `DataTableRowTrigger` inside it opens as a whole. A row someone ticked takes `data-state='selected'`, as a table row does. */\nfunction DataTableListItem({ className, ...props }: ComponentProps<'li'>) {\n  return (\n    <li\n      data-slot='data-table-list-item'\n      className={cn(\n        'data-[state=selected]:bg-muted/50 px-[var(--data-table-inset,0.75rem)]',\n        ROW_HOST,\n        OPEN_ITEM,\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\nconst ROW_TRIGGER =\n  'focus-visible:after:ring-ring/50 block max-w-full cursor-pointer text-start font-medium outline-none after:absolute after:inset-0 focus-visible:after:ring-3 focus-visible:after:ring-inset'\n\ninterface RowTriggerOptions {\n  /**\n   * Cuts a long name with an ellipsis. Pass `false` to let it wrap.\n   * @default true\n   */\n  truncate?: boolean\n}\n\n/** Makes the whole row open what this button opens. Other controls in the row stay above it. */\nfunction DataTableRowTrigger({\n  className,\n  truncate = true,\n  ...props\n}: ComponentProps<'button'> & RowTriggerOptions) {\n  return (\n    <button\n      data-slot='data-table-row-trigger'\n      type='button'\n      className={cn(ROW_TRIGGER, truncate && 'truncate', className)}\n      {...props}\n    />\n  )\n}\n\n/** Makes the whole row go where this link goes. Other controls in the row stay above it. */\nfunction DataTableRowLink({\n  className,\n  truncate = true,\n  ...props\n}: LinkProps & RowTriggerOptions) {\n  return (\n    <Link\n      data-slot='data-table-row-trigger'\n      className={cn(ROW_TRIGGER, truncate && 'truncate', className)}\n      {...props}\n    />\n  )\n}\n\ntype Align = 'start' | 'center' | 'end'\n\nconst EDGES =\n  'first:ps-[var(--data-table-inset,0.75rem)] last:pe-[var(--data-table-inset,0.75rem)]'\n\nconst STICKY =\n  'sticky start-0 z-10 bg-[var(--data-table-surface,var(--color-background))] transition-colors group-data-highlighted/data-table-row:bg-[color-mix(in_oklab,var(--color-muted)_50%,var(--data-table-surface,var(--color-background)))] group-data-[state=selected]/data-table-row:bg-[color-mix(in_oklab,var(--color-muted)_50%,var(--data-table-surface,var(--color-background)))] group-has-data-[slot=data-table-row-trigger]/data-table-row:group-hover/data-table-row:bg-[color-mix(in_oklab,var(--color-muted)_50%,var(--data-table-surface,var(--color-background)))]'\n\nconst ALIGN: Record<Align, string> = {\n  center: 'text-center',\n  end: 'text-end',\n  start: 'text-start',\n}\n\ninterface DataTableHeadProps extends Omit<ComponentProps<'th'>, 'align'> {\n  /** @default 'start' */\n  align?: Align\n  /** Keeps the column in view while the table scrolls sideways. */\n  sticky?: boolean\n}\n\nfunction DataTableHead({\n  align = 'start',\n  className,\n  sticky = false,\n  ...props\n}: DataTableHeadProps) {\n  return (\n    <th\n      role='columnheader'\n      scope='col'\n      className={cn(\n        'text-muted-foreground h-[var(--data-table-head-height,2.5rem)] px-3 align-middle text-xs font-medium whitespace-nowrap',\n        EDGES,\n        ALIGN[align],\n        sticky && STICKY,\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\ninterface DataTableCellProps extends Omit<ComponentProps<'td'>, 'align'> {\n  /** @default 'start' */\n  align?: Align\n  primary?: boolean\n  /** Cuts long text with an ellipsis and gives the column the room the others leave. */\n  truncate?: boolean\n}\n\nfunction cellClassName({\n  align = 'start',\n  primary = false,\n  truncate = false,\n}: Pick<DataTableCellProps, 'align' | 'primary' | 'truncate'>) {\n  return cn(\n    'h-[var(--data-table-cell-height,3rem)] px-3 py-[var(--data-table-cell-py,0.5rem)] align-middle',\n    EDGES,\n    ALIGN[align],\n    align === 'end' && 'whitespace-nowrap tabular-nums',\n    truncate && 'w-full max-w-0 min-w-32 truncate',\n    primary && 'font-medium',\n  )\n}\n\nfunction DataTableCell({\n  align,\n  className,\n  primary,\n  truncate,\n  ...props\n}: DataTableCellProps) {\n  return (\n    <td\n      role='cell'\n      className={cn(cellClassName({ align, primary, truncate }), className)}\n      {...props}\n    />\n  )\n}\n\ninterface DataTableRowHeadProps extends Omit<ComponentProps<'th'>, 'align' | 'scope'> {\n  /** @default 'start' */\n  align?: Align\n  primary?: boolean\n  /** Keeps the column in view while the table scrolls sideways. */\n  sticky?: boolean\n  /** Cuts long text with an ellipsis and gives the column the room the others leave. */\n  truncate?: boolean\n}\n\nfunction DataTableRowHead({\n  align,\n  className,\n  primary,\n  sticky = false,\n  truncate,\n  ...props\n}: DataTableRowHeadProps) {\n  return (\n    <th\n      role='rowheader'\n      scope='row'\n      className={cn(\n        'font-normal',\n        cellClassName({ align, primary, truncate }),\n        sticky && STICKY,\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\ninterface DataTableGroupHeadProps extends Omit<ComponentProps<'th'>, 'scope'> {\n  colSpan: number\n}\n\n/** Goes in the first row of the `DataTableBody` whose rows it names. */\nfunction DataTableGroupHead({ children, className, ...props }: DataTableGroupHeadProps) {\n  return (\n    <th\n      role='rowheader'\n      scope='rowgroup'\n      className={cn(\n        'text-muted-foreground px-3 pt-4 pb-1.5 text-start text-xs font-medium',\n        EDGES,\n        className,\n      )}\n      {...props}\n    >\n      <span className='sticky start-[var(--data-table-inset,0.75rem)]'>{children}</span>\n    </th>\n  )\n}\n\ninterface DataTableSpacerRowProps {\n  colSpan: number\n  /** The height, in pixels, of the rows a virtual list leaves out. */\n  height: number\n}\n\nfunction DataTableSpacerRow({ colSpan, height }: DataTableSpacerRowProps) {\n  return (\n    <tr aria-hidden>\n      {/* oxlint-disable-next-line jsx-a11y/control-has-associated-label -- an empty, hidden cell that holds the height of rows not rendered */}\n      <td colSpan={colSpan} style={{ height }} className='p-0' />\n    </tr>\n  )\n}\n\n/**\n * Holds what spans a whole row (a message, a panel opened under a row) to the table's visible\n * width, so it stays in view and wraps there while the table scrolls sideways.\n */\nfunction DataTableInView({ className, ...props }: ComponentProps<'div'>) {\n  return (\n    <div\n      data-slot='data-table-in-view'\n      className={cn(\n        'sticky start-[var(--data-table-inset,0.75rem)] w-[calc(var(--data-table-visible-width,100%)_-_2*var(--data-table-inset,0.75rem))]',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\ninterface DataTableStateRowsProps {\n  colSpan: number\n  /** Sketches one cell per column while loading, aligned as listed, the first being the row's name, so the rows keep the loaded rows' columns. */\n  columns?: ('start' | 'end')[]\n  /** Sketches a smaller second line under the name, for rows whose name has a detail line under it. */\n  detail?: boolean\n  /** What the empty row says, such as `Nobody matches these conditions.` */\n  emptyMessage?: string\n  /** How many rows to sketch while loading. @default 5 */\n  rows?: number\n}\n\nfunction DataTableStateRows({\n  colSpan,\n  columns,\n  detail = false,\n  emptyMessage,\n  rows = 5,\n}: DataTableStateRowsProps) {\n  const state = useBlockState()\n  if (!state) return null\n\n  if (state !== 'loading') {\n    return (\n      <DataTableRow>\n        <DataTableCell colSpan={colSpan}>\n          <DataTableInView>\n            <BlockStateText emptyMessage={emptyMessage} />\n          </DataTableInView>\n        </DataTableCell>\n      </DataTableRow>\n    )\n  }\n\n  if (columns) {\n    return Array.from({ length: rows }, (_, index) => (\n      <DataTableRow key={index} aria-hidden={index > 0 || undefined}>\n        {columns.map((align, column) =>\n          column === 0 ? (\n            <DataTableCell key={column}>\n              <span className='flex flex-col'>\n                {index === 0 && <span className='sr-only'>Loading</span>}\n                <span className='text-sm'>\n                  <SkeletonText className='w-32' />\n                </span>\n                {detail && (\n                  <span className='text-xs'>\n                    <SkeletonText className='w-24' />\n                  </span>\n                )}\n              </span>\n            </DataTableCell>\n          ) : (\n            <DataTableCell key={column} align={align}>\n              <SkeletonText className='w-14' />\n            </DataTableCell>\n          ),\n        )}\n      </DataTableRow>\n    ))\n  }\n\n  return Array.from({ length: rows }, (_, index) => (\n    <DataTableRow key={index} aria-hidden={index > 0 || undefined}>\n      <DataTableCell colSpan={colSpan}>\n        <span className='flex w-full items-center gap-4'>\n          {index === 0 && <span className='sr-only'>Loading</span>}\n          <SkeletonText className='w-1/4' />\n          <SkeletonText className='ms-auto w-16' />\n          <SkeletonText className='w-16' />\n        </span>\n      </DataTableCell>\n    </DataTableRow>\n  ))\n}\n\ninterface DataTablePagerProps {\n  /** Controls before Previous and Next, such as a page size menu. */\n  children?: ReactNode\n  className?: string\n  /** A fact after the range on the same line, such as `3 selected`. */\n  detail?: ReactNode\n  /** The first row on the page, counted from 1. */\n  from: number\n  onNext: () => void\n  onPrevious: () => void\n  /** The last row on the page. */\n  to: number\n  total: number\n}\n\nfunction DataTablePager({\n  children,\n  className,\n  detail,\n  from,\n  onNext,\n  onPrevious,\n  to,\n  total,\n}: DataTablePagerProps) {\n  const state = useBlockState()\n  const previousRef = useRef<HTMLButtonElement>(null)\n  const nextRef = useRef<HTMLButtonElement>(null)\n  const pressed = useRef<HTMLButtonElement | null>(null)\n  const loading = state === 'loading'\n  const canPrevious = !loading && from > 1\n  const canNext = !loading && to < total\n\n  useEffect(() => {\n    const button = pressed.current\n    if (!button || loading) return\n    pressed.current = null\n    if (!button.disabled) {\n      if (document.activeElement !== button) button.focus()\n      return\n    }\n    const other = button === previousRef.current ? nextRef.current : previousRef.current\n    other?.focus()\n  })\n\n  if (state === 'empty' || state === 'error') return null\n\n  return (\n    <div\n      className={cn(\n        'flex flex-wrap items-center justify-between gap-x-4 gap-y-2',\n        className,\n      )}\n    >\n      <p\n        className='text-muted-foreground text-xs whitespace-nowrap tabular-nums'\n        aria-live='polite'\n      >\n        {loading ? (\n          <SkeletonText className='w-24' />\n        ) : (\n          <Details>\n            {`${formatNumber(total === 0 ? 0 : from)}–${formatNumber(to)} of ${formatNumber(total)}`}\n            {detail}\n          </Details>\n        )}\n      </p>\n      <div className='flex flex-wrap items-center gap-2'>\n        {children}\n        <div className='flex gap-2'>\n          <Button\n            variant='outline'\n            size='sm'\n            ref={previousRef}\n            disabled={!canPrevious}\n            onClick={() => {\n              pressed.current = previousRef.current\n              onPrevious()\n            }}\n          >\n            <IconPlaceholder\n              lucide='ChevronLeftIcon'\n              tabler='IconChevronLeft'\n              hugeicons='ArrowLeft01Icon'\n              phosphor='CaretLeftIcon'\n              remixicon='RiArrowLeftSLine'\n              data-icon='inline-start'\n            />\n            Previous\n          </Button>\n          <Button\n            variant='outline'\n            size='sm'\n            ref={nextRef}\n            disabled={!canNext}\n            onClick={() => {\n              pressed.current = nextRef.current\n              onNext()\n            }}\n          >\n            Next\n            <IconPlaceholder\n              lucide='ChevronRightIcon'\n              tabler='IconChevronRight'\n              hugeicons='ArrowRight01Icon'\n              phosphor='CaretRightIcon'\n              remixicon='RiArrowRightSLine'\n              data-icon='inline-end'\n            />\n          </Button>\n        </div>\n      </div>\n    </div>\n  )\n}\n\ninterface DataTableBarProps {\n  className?: string\n  /** @default 'var(--color-chart-2)' */\n  color?: string\n  /** Colours the fill with classes, such as a status tone's, instead of `color`. */\n  fillClassName?: string\n  max: number\n  value: number\n}\n\nfunction DataTableBar({\n  className,\n  color = 'var(--color-chart-2)',\n  fillClassName,\n  max,\n  value,\n}: DataTableBarProps) {\n  const share = max > 0 ? Math.min(1, Math.max(0, value / max)) : 0\n  return (\n    <span\n      aria-hidden\n      className={cn(\n        'bg-muted block h-1.5 w-full overflow-hidden rounded-full',\n        className,\n      )}\n    >\n      <span\n        className={cn('block h-full rounded-full', fillClassName)}\n        style={{\n          backgroundColor: fillClassName ? undefined : color,\n          width: `${share * 100}%`,\n        }}\n      />\n    </span>\n  )\n}\n\nexport {\n  DataTable,\n  DataTableBar,\n  DataTableBody,\n  DataTableCell,\n  DataTableFooter,\n  DataTableGroupHead,\n  DataTableHead,\n  DataTableHeader,\n  DataTableInView,\n  DataTableListItem,\n  DataTablePager,\n  DataTableRow,\n  DataTableRowHead,\n  DataTableRowLink,\n  DataTableRowTrigger,\n  DataTableSpacerRow,\n  DataTableStateRows,\n}\n\nexport type {\n  DataTableBarProps,\n  DataTableCellProps,\n  DataTableDensity,\n  DataTableGroupHeadProps,\n  DataTableHeadProps,\n  DataTablePagerProps,\n  DataTableProps,\n  DataTableRowHeadProps,\n  DataTableRowProps,\n  DataTableSpacerRowProps,\n  DataTableStateRowsProps,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/table.tsx"
    }
  ],
  "type": "registry:component"
}
