Table

The Table component provides a styled and accessible way to display data in rows and columns. It’s designed to work with structured data and offers a variety of customization options.

Usage

<script>
  import { 
    Table,
    TableHeader,
    TableBody,
    TableFooter,
    TableHead,
    TableRow,
    TableCell,
    TableCaption
  } from "$lib/components/ui/table";
</script>

<Table>
  <TableCaption>A list of items.</TableCaption>
  <TableHeader>
    <TableRow>
      <TableHead>Name</TableHead>
      <TableHead>Description</TableHead>
      <TableHead>Status</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    <TableRow>
      <TableCell>Item 1</TableCell>
      <TableCell>Description for Item 1</TableCell>
      <TableCell>Active</TableCell>
    </TableRow>
    <TableRow>
      <TableCell>Item 2</TableCell>
      <TableCell>Description for Item 2</TableCell>
      <TableCell>Inactive</TableCell>
    </TableRow>
  </TableBody>
</Table>

Examples

Basic Table

A simple table with header, body, and caption.

A list of recent invoices.
Invoice Status Method Amount
INV001 Paid Credit Card $250.00
INV002 Pending PayPal $150.00
INV003 Unpaid Bank Transfer $350.00

With Footer

A table with a footer section for totals or additional information.

Product Quantity Price Total
Product A 2 $25.00 $50.00
Product B 1 $35.00 $35.00
Product C 3 $15.00 $45.00
Total $130.00

With Status Badges

A table with status indicators using the Badge component.

Task Assignee Due Date Status
Design Scene Composition Sarah Williams Oct 25, 2023 Completed
Record Voiceover John Roberts Oct 28, 2023 Overdue
Edit Footage Maria Garcia Nov 2, 2023 In Progress
Sound Design Alex Chen Nov 5, 2023 Pending

Film Project Table

A table example relevant to magic.mov for managing film projects.

Active Film Projects
Project Title Director Format Duration Status
The Silent Echo Alex Rivera 4K / 24fps 15:42 In Production
Midnight Crossing Elena Kim 1080p / 60fps 24:10 Pre-production
Urban Rhythms Marcus Johnson 4K / 30fps 11:35 Post-production
Beyond the Horizon Sophia Chen 8K / 24fps 18:22 Final Review

With Custom Styling

A table with customized styling using Tailwind classes.

Name Role Department Contact
Michael Scott Director Production m.scott@example.com
Jim Halpert Cinematographer Camera j.halpert@example.com
Pam Beesly Art Director Art p.beesly@example.com
Dwight Schrute Sound Engineer Sound d.schrute@example.com

API Reference

Table Components

ComponentDescription
TableThe root container for the table.
TableHeaderThe header section of the table.
TableBodyThe body section of the table containing the data rows.
TableFooterThe footer section of the table, often used for totals.
TableRowA row within the table.
TableHeadA header cell within the table header.
TableCellA standard cell within the table body.
TableCaptionA caption for the table.

Table Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table.
refHTMLTableElementundefinedA reference to the underlying HTML element.

TableHeader Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table header.
refHTMLTableSectionElementundefinedA reference to the underlying HTML element.

TableBody Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table body.
refHTMLTableSectionElementundefinedA reference to the underlying HTML element.

TableFooter Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table footer.
refHTMLTableSectionElementundefinedA reference to the underlying HTML element.

TableRow Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table row.
refHTMLTableRowElementundefinedA reference to the underlying HTML element.

TableHead Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table head cell.
refHTMLTableCellElementundefinedA reference to the underlying HTML element.

TableCell Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table cell.
refHTMLTableCellElementundefinedA reference to the underlying HTML element.

TableCaption Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the table caption.
refHTMLElementundefinedA reference to the underlying HTML element.

Implementation

The Table component is built using native HTML table elements with styled Svelte components to ensure proper accessibility and semantics:

<!-- Table component -->
<div class="relative w-full overflow-auto">
  <table class={cn("w-full caption-bottom text-sm", className)}>
    {@render children?.()}
  </table>
</div>

<!-- TableHeader component -->
<thead class={cn("[&_tr]:border-b", className)}>
  {@render children?.()}
</thead>

<!-- TableBody component -->
<tbody class={cn("[&_tr:last-child]:border-0", className)}>
  {@render children?.()}
</tbody>

<!-- TableRow component -->
<tr class={cn(
  "hover:bg-muted/50 data-[state=selected]:bg-muted border-b transition-colors",
  className
)}>
  {@render children?.()}
</tr>

<!-- TableCell component -->
<td class={cn(
  "p-2 align-middle [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
  className
)}>
  {@render children?.()}
</td>

The components are designed to be flexible and customizable, allowing for a wide range of table styles and uses. The default styling provides a clean and accessible table that can be easily extended with Tailwind classes.