# Table

> Data table built from columns and rows with tabular numbers, optional striping, and a floating card variant.

- Kind: ui · Category: Data display · Interactivity: none
- Install: `npx shadcn@latest add @haze/table`
- File: `src/components/haze/ui/table.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/table

## Use when
- Comparing the same few fields across many records, such as invoices or deployments
- Numeric columns that should line up on the right with tabular figures
- Embedding a card-styled grid cleanly beneath transparent headers inside a Frame

## Avoid when
- Page layout; use grid or flex instead
- Data that needs sorting, filtering, selection or pagination; use sortable-data-table
- Two or three fields per record on narrow screens; the item component stacks better

## Alternatives
- [sortable-data-table](https://hazeui.com/ui/sortable-data-table.md)
- [frame](https://hazeui.com/ui/frame.md)
- [item](https://hazeui.com/ui/item.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| caption | `string` |  | yes | Title of the table. Also the aria-label of the scroll region. |
| hideCaption | `boolean` | false | no | Moves the caption to screen readers only. |
| columns | `{ key: string; label: string; align?: "left" \| "right" }[]` |  | yes | Header cells in order. align right also turns on tabular figures in the cells. |
| rows | `Record<string, string>[]` |  | yes | One object per row; each column reads row[column.key]. Values are plain strings. |
| striped | `boolean` | false | no | Shades every second body row for long tables. |
| variant | `"default" \| "card"` | "default" | no | default frames the full table in a card; card renders transparent headers over a rounded elevated body grid. |
| rowHeader | `string` |  | no | Key of the column whose cells render as <th scope="row">. |
| class | `string` |  | no | Extra classes on the outer panel. |

## Accessibility
- Uses a real table with <caption>, scope="col" headers and optional scope="row" headers.
- The scroll region is focusable and named by the caption so keyboard users can scroll wide tables.
- Cells hold plain strings so status meaning stays readable without relying on color.
- Key Tab, then Arrow keys: Focus the scroll region, then scroll it sideways or vertically

## Agent prompts

### Install and use

Install the Haze UI component "Table" with `npx shadcn@latest add @haze/table`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Table from "@/components/haze/ui/table.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/table.md

### Customize with real content

Adapt the Haze table ui for our product. Replace the demo values of these props with our real content: caption, hideCaption, columns, rows, striped, variant, rowHeader, class. Keep the layout, the heading level rules and these accessibility details: Uses a real table with <caption>, scope="col" headers and optional scope="row" headers. The scroll region is focusable and named by the caption so keyboard users can scroll wide tables. Cells hold plain strings so status meaning stays readable without relying on color. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using table, check it fits: use it when comparing the same few fields across many records, such as invoices or deployments; numeric columns that should line up on the right with tabular figures; embedding a card-styled grid cleanly beneath transparent headers inside a frame. Avoid it when page layout; use grid or flex instead; data that needs sorting, filtering, selection or pagination; use sortable-data-table; two or three fields per record on narrow screens; the item component stacks better. Alternatives: @haze/sortable-data-table, @haze/frame, @haze/item.

## Example requests that should lead here
- Show invoices in a card-variant table inside a Frame
- Add a table of deployments where the project name is the row header

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Column {
  key: string;
  label: string;
  /** Right alignment also turns on tabular figures, so numbers line up. */
  align?: "left" | "right";
}

interface Props {
  /** Visible title of the table; also the accessible name of the scroll region. */
  caption: string;
  /** Keeps the caption for screen readers only. */
  hideCaption?: boolean;
  columns: Column[];
  rows: Record<string, string>[];
  /** Key of the column whose cells become row headers. */
  rowHeader?: string;
  /** Shades every second body row. */
  striped?: boolean;
  /** default: single bordered table card. card: transparent header over an elevated separated card grid. */
  variant?: "default" | "card";
  class?: string;
}

const { caption, hideCaption = false, columns, rows, rowHeader, striped = false, variant = "default", class: className } = Astro.props;
const isCard = variant === "card";
const headAlign = { left: "text-left", right: "text-right" };
const cellAlign = { left: "text-left", right: "text-right tabular-nums" };
---

<div class={cn(isCard ? "min-w-0" : "overflow-hidden rounded-xl border border-border bg-card", className)}>
  {
    !hideCaption && (
      <div
        class={cn(
          "text-[0.8125rem]/5 font-medium text-foreground",
          isCard ? "mb-2 px-1" : "border-b border-border px-4 py-3",
        )}
        aria-hidden="true"
      >
        {caption}
      </div>
    )
  }
  <div
    role="region"
    aria-label={caption}
    tabindex="0"
    class="overflow-x-auto focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring"
  >
    <table class={cn("w-full text-[0.8125rem]/5", isCard ? "border-separate border-spacing-0" : "border-collapse")}>
      <caption class="sr-only">{caption}</caption>
      <thead>
        <tr class={cn(isCard ? "bg-transparent" : "border-b border-border bg-muted")}>
          {
            columns.map((col) => (
              <th
                scope="col"
                class={cn(
                  "h-9 px-4 text-xs/4 font-medium whitespace-nowrap text-muted-foreground",
                  headAlign[col.align ?? "left"],
                )}
              >
                {col.label}
              </th>
            ))
          }
        </tr>
      </thead>
      <tbody>
        {
          rows.map((row, rowIndex) => {
            const isFirstRow = rowIndex === 0;
            const isLastRow = rowIndex === rows.length - 1;
            return (
              <tr
                class={cn(
                  "group/row transition-colors duration-150",
                  !isCard && "border-t border-border first:border-t-0 hover:bg-muted",
                  !isCard && striped && "even:bg-muted/50",
                )}
              >
                {columns.map((col, colIndex) => {
                  const isFirstCol = colIndex === 0;
                  const isLastCol = colIndex === columns.length - 1;
                  const cardCell = isCard
                    ? cn(
                        "border-b border-border bg-card group-hover/row:bg-accent",
                        striped && rowIndex % 2 === 1 && "bg-muted/30",
                        isFirstRow && "border-t",
                        isFirstCol && "border-l",
                        isLastCol && "border-r",
                        isFirstRow && isFirstCol && "rounded-tl-xl",
                        isFirstRow && isLastCol && "rounded-tr-xl",
                        isLastRow && isFirstCol && "rounded-bl-xl",
                        isLastRow && isLastCol && "rounded-br-xl",
                      )
                    : "";
                  const shared = cn("px-4 py-3 text-[0.8125rem]/5 whitespace-nowrap transition-colors duration-150", cellAlign[col.align ?? "left"], cardCell);
                  return col.key === rowHeader ? (
                    <th scope="row" class={cn(shared, "font-medium text-foreground")}>
                      {row[col.key]}
                    </th>
                  ) : (
                    <td class={cn(shared, colIndex === 0 ? "font-medium text-foreground" : "text-muted-foreground")}>
                      {row[col.key]}
                    </td>
                  );
                })}
              </tr>
            );
          })
        }
      </tbody>
    </table>
  </div>
</div>
```
