Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Table for Astro

Semantic Astro table parts for records and comparisons.

Sample charges for completed studio visits.
InvoiceStatusMethodAmount
INV-001PaidCredit Card$250.00
INV-002PendingPayPal$150.00
INV-003UnpaidBank Transfer$350.00
Total$750.00

Installation

bunx --bun shadcn@latest add @gnostic/table
npx shadcn@latest add @gnostic/table
pnpm dlx shadcn@latest add @gnostic/table

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/table/index.ts
export { default as Table } from "./table.astro"
export { default as TableHeader } from "./table-header.astro"
export { default as TableBody } from "./table-body.astro"
export { default as TableFooter } from "./table-footer.astro"
export { default as TableRow } from "./table-row.astro"
export { default as TableHead } from "./table-head.astro"
export { default as TableCell } from "./table-cell.astro"
export { default as TableCaption } from "./table-caption.astro"
src/components/ui/table/table-body.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"tbody">

const { class: className, ...props } = Astro.props
---

<tbody data-slot="table-body" class={cn("relative in-data-[variant=card]:rounded-xl in-data-[variant=card]:shadow-xs/5 before:pointer-events-none before:absolute before:inset-px not-in-data-[variant=card]:before:hidden before:rounded-[calc(var(--radius-xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] dark:before:shadow-[0_-1px_--theme(--color-white/8%)] [&_tr:last-child]:border-0 in-data-[variant=card]:*:[tr]:border-0 in-data-[variant=card]:*:[tr]:*:[td]:border-b in-data-[variant=card]:*:[tr]:*:[td]:bg-card in-data-[variant=card]:*:[tr]:first:*:[td]:first:rounded-ss-xl in-data-[variant=card]:*:[tr]:*:[td]:first:border-s in-data-[variant=card]:*:[tr]:first:*:[td]:border-t in-data-[variant=card]:*:[tr]:last:*:[td]:last:rounded-ee-xl in-data-[variant=card]:*:[tr]:*:[td]:last:border-e in-data-[variant=card]:*:[tr]:first:*:[td]:last:rounded-se-xl in-data-[variant=card]:*:[tr]:last:*:[td]:first:rounded-es-xl in-data-[variant=card]:*:[tr]:hover:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-black)_2%)] in-data-[variant=card]:*:[tr]:data-[state=selected]:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-black)_4%)] dark:in-data-[variant=card]:*:[tr]:data-[state=selected]:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-white)_4%)] dark:in-data-[variant=card]:*:[tr]:hover:*:[td]:bg-[color-mix(in_srgb,var(--card),var(--color-white)_2%)]", className)} {...props}>
  <slot />
</tbody>
src/components/ui/table/table-caption.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"caption">

const { class: className, ...props } = Astro.props
---

<caption
  data-slot="table-caption"
  class={cn("in-data-[variant=card]:my-4 mt-4 text-muted-foreground text-sm", className)}
  {...props}
>
  <slot />
</caption>
src/components/ui/table/table-cell.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"td">

const { class: className, ...props } = Astro.props
---

<td data-slot="table-cell" class={cn("whitespace-nowrap bg-clip-padding p-2.5 in-data-[slot=table-footer]:py-3.5 align-middle leading-none in-data-[variant=card]:first:ps-[calc(--spacing(2.5)-1px)] in-data-[variant=card]:last:pe-[calc(--spacing(2.5)-1px)] has-[[role=checkbox]]:w-px last:has-[[role=checkbox]]:ps-0 first:has-[[role=checkbox]]:pe-0", className)} {...props}>
  <slot />
</td>
src/components/ui/table/table-footer.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"tfoot">

const { class: className, ...props } = Astro.props
---

<tfoot
  data-slot="table-footer"
  class={cn("border-t in-data-[variant=card]:border-none bg-transparent not-in-data-[variant=card]:bg-[color-mix(in_srgb,var(--card),var(--color-black)_2%)] font-medium dark:not-in-data-[variant=card]:bg-[color-mix(in_srgb,var(--card),var(--color-white)_2%)] [&>tr]:last:border-b-0", className)}
  {...props}
>
  <slot />
</tfoot>
src/components/ui/table/table-head.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"th">

const { class: className, ...props } = Astro.props
---

<th data-slot="table-head" class={cn("h-10 whitespace-nowrap px-2.5 text-left align-middle font-medium text-muted-foreground leading-none has-[[role=checkbox]]:w-px last:has-[[role=checkbox]]:ps-0 first:has-[[role=checkbox]]:pe-0", className)} {...props}>
  <slot />
</th>
src/components/ui/table/table-header.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"thead">

const { class: className, ...props } = Astro.props
---

<thead
  data-slot="table-header"
  class={cn("[&_tr]:border-b", className)}
  {...props}
>
  <slot />
</thead>
src/components/ui/table/table-row.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"tr">

const { class: className, ...props } = Astro.props
---

<tr data-slot="table-row" class={cn("relative border-b not-in-data-[variant=card]:hover:bg-[color-mix(in_srgb,var(--background),var(--color-black)_2%)] not-in-data-[variant=card]:data-[state=selected]:bg-[color-mix(in_srgb,var(--background),var(--color-black)_4%)] dark:not-in-data-[variant=card]:data-[state=selected]:bg-[color-mix(in_srgb,var(--background),var(--color-white)_4%)] dark:not-in-data-[variant=card]:hover:bg-[color-mix(in_srgb,var(--background),var(--color-white)_2%)]", className)} {...props}>
  <slot />
</tr>
src/components/ui/table/table.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"table"> & { variant?: "default" | "card" }

const { class: className, variant = "default", ...props } = Astro.props
---

<div
  data-slot="table-container"
  data-variant={variant}
  class="relative w-full overflow-x-auto"
>
  <table data-slot="table" class={cn("w-full caption-bottom in-data-[variant=card]:border-separate in-data-[variant=card]:border-spacing-0 text-sm", className)} {...props}>
    <slot />
  </table>
</div>

Usage

import {
    Table,
    TableBody,
    TableCaption,
    TableCell,
    TableFooter,
    TableHead,
    TableHeader,
    TableRow,
} from "@/components/ui/table"
<Table>
  <TableCaption>A list of your invoices.</TableCaption>
  <TableHeader>
    <TableRow>
      <TableHead>Invoice</TableHead>
      <TableHead>Status</TableHead>
      <TableHead>Amount</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    <TableRow>
      <TableCell>INV-001</TableCell>
      <TableCell>Paid</TableCell>
      <TableCell>$250.00</TableCell>
    </TableRow>
  </TableBody>
</Table>

Composition

Arrange the parts as shown for a Table:

Table
├── TableCaption
├── TableHeader
│   └── TableRow
│       ├── TableHead
│       ├── TableHead
│       └── TableHead
├── TableBody
│   └── TableRow
│       ├── TableCell
│       ├── TableCell
│       └── TableCell
└── TableFooter

Notes

  • Keep table structure semantic: headers in TableHeader, rows in TableBody, and summary data in TableFooter when needed.
  • Right-align numeric columns so values are easier to scan.
  • Wrap large tables in a container with horizontal overflow when the content may exceed the available width.

Sizes and states

Content ready for handoff
PageReview
ServicesApproved
ContactSelected for review

API Reference

Read the installed source for the complete prop types.

When to use Table

Compose TableHeader, TableBody, rows, and cells from your data. Use header cells for column or row names and a caption when it helps identify the data. The wrapper allows horizontal overflow.

Keyboard and behavior

Native table markup preserves the relationship between headers and cells. The component does not add sorting, filtering, row selection, or pagination. Implement those as labelled controls and keep their state clear.

Theme and layout

Use variant="card" for the card table treatment or the default for a page table. Check long cells and narrow-screen overflow. Avoid hiding essential columns only to fit a small screen.