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
Component | Description |
---|
Table | The root container for the table. |
TableHeader | The header section of the table. |
TableBody | The body section of the table containing the data rows. |
TableFooter | The footer section of the table, often used for totals. |
TableRow | A row within the table. |
TableHead | A header cell within the table header. |
TableCell | A standard cell within the table body. |
TableCaption | A caption for the table. |
Table Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table. |
ref | HTMLTableElement | undefined | A reference to the underlying HTML element. |
TableHeader Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table header. |
ref | HTMLTableSectionElement | undefined | A reference to the underlying HTML element. |
TableBody Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table body. |
ref | HTMLTableSectionElement | undefined | A reference to the underlying HTML element. |
TableFooter Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table footer. |
ref | HTMLTableSectionElement | undefined | A reference to the underlying HTML element. |
TableRow Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table row. |
ref | HTMLTableRowElement | undefined | A reference to the underlying HTML element. |
TableHead Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table head cell. |
ref | HTMLTableCellElement | undefined | A reference to the underlying HTML element. |
TableCell Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table cell. |
ref | HTMLTableCellElement | undefined | A reference to the underlying HTML element. |
TableCaption Properties
Property | Type | Default | Description |
---|
class | string | undefined | Custom CSS classes for the table caption. |
ref | HTMLElement | undefined | A 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:
<div class="relative w-full overflow-auto">
<table class={cn("w-full caption-bottom text-sm", className)}>
{@render children?.()}
</table>
</div>
<thead class={cn("[&_tr]:border-b", className)}>
{@render children?.()}
</thead>
<tbody class={cn("[&_tr:last-child]:border-0", className)}>
{@render children?.()}
</tbody>
<tr class={cn(
"hover:bg-muted/50 data-[state=selected]:bg-muted border-b transition-colors",
className
)}>
{@render children?.()}
</tr>
<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.