Displays data in a tabular format with rows and columns.
1<script lang="ts">
2 import {
3 Table,
4 TableBody,
Installation
pnpm dlx sprawlify@latest add tablenpx sprawlify@latest add tableyarn sprawlify@latest add tablebunx --bun sprawlify@latest add table
Install the following dependencies:
pnpm add @sprawlify/primitives @sprawlify/sveltenpm install @sprawlify/primitives @sprawlify/svelteyarn add @sprawlify/primitives @sprawlify/sveltebun add @sprawlify/primitives @sprawlify/svelte
Add the following files to your project:
1export { default as Table } from "./table.svelte";2export { default as TableHeader } from "./table-header.svelte";3export { default as TableBody } from "./table-body.svelte";4export { default as TableFooter } from "./table-footer.svelte";5export { default as TableRow } from "./table-row.svelte";6export { default as TableHead } from "./table-head.svelte";7export { default as TableCell } from "./table-cell.svelte";8export { default as TableCaption } from "./table-caption.svelte";91<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
4 import type { HTMLAttributes } from "svelte/elements"
5
6 interface Props extends HTMLAttributes<HTMLTableSectionElement>, PolymorphicProps<"tbody"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="tbody"
13 data-slot="table-body"
14 class={cn("[&_tr:last-child]:border-0", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLAttributes } from "svelte/elements"
4 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
5
6 interface Props extends HTMLAttributes<HTMLElement>, PolymorphicProps<"caption"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="caption"
13 data-slot="table-caption"
14 class={cn("mt-4 text-sm text-muted-foreground", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLTdAttributes } from "svelte/elements";
4 import { Sprawlify, type PolymorphicProps } from "@sprawlify/svelte";
5
6 interface Props extends HTMLTdAttributes, PolymorphicProps<"td"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="td"
13 data-slot="table-cell"
14 class={cn("p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLAttributes } from "svelte/elements"
4 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
5
6 interface Props extends HTMLAttributes<HTMLTableSectionElement>, PolymorphicProps<"tfoot"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="tfoot"
13 data-slot="table-footer"
14 class={cn("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLThAttributes } from "svelte/elements"
4 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
5
6 interface Props extends HTMLThAttributes, PolymorphicProps<"th"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="th"
13 data-slot="table-head"
14 class={cn("h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLAttributes } from "svelte/elements"
4 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
5
6 interface Props extends HTMLAttributes<HTMLTableSectionElement>, PolymorphicProps<"thead"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="thead"
13 data-slot="table-header"
14 class={cn("[&_tr]:border-b", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLAttributes } from "svelte/elements"
4 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
5
6 interface Props extends HTMLAttributes<HTMLTableRowElement>, PolymorphicProps<"tr"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<Sprawlify
12 as="tr"
13 data-slot="table-row"
14 class={cn("border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted", className)}
15 {...props}
16>
17 {@render children?.()}
18</Sprawlify>1<script lang="ts">
2 import { cn } from "@/lib/utils"
3 import type { HTMLTableAttributes } from "svelte/elements"
4 import { type PolymorphicProps, Sprawlify } from "@sprawlify/svelte";
5
6 interface Props extends HTMLTableAttributes, PolymorphicProps<"table"> {}
7
8 let { class: className, children, ...props }: Props = $props()
9</script>
10
11<div data-slot="table-container" class="relative w-full overflow-x-auto">
12 <Sprawlify
13 as="table"
14 data-slot="table"
15 class={cn("w-full caption-bottom text-sm", className)}
16 {...props}
17 >
18 {@render children?.()}
19 </Sprawlify>
20</div>Update the import paths to match your project setup.
Usage
1import {
2 Table,
3 TableBody,
4 TableCaption,
5 TableCell,
6 TableHead,
7 TableHeader,
8 TableRow,
9} from "@/components/ui/table"1<Table>
2 <TableCaption>A list of your recent invoices.</TableCaption>
3 <TableHeader>
4 <TableRow>
5 <TableHead class="w-[100px]">Invoice</TableHead>
6 <TableHead>Status</TableHead>
7 <TableHead>Method</TableHead>
8 <TableHead class="text-right">Amount</TableHead>
9 </TableRow>
10 </TableHeader>
11 <TableBody>
12 <TableRow>
13 <TableCell class="font-medium">INV001</TableCell>
14 <TableCell>Paid</TableCell>
15 <TableCell>Credit Card</TableCell>
16 <TableCell class="text-right">$250.00</TableCell>
17 </TableRow>
18 </TableBody>
19</Table>Examples
Actions
A table showing actions for each row using a <DropdownMenu /> component.
1<script lang="ts">
2 import { Button } from "@/components/ui/button";
3 import {
4 DropdownMenu,
Footer
Use the <TableFooter /> component to add a footer to the table.
1<script lang="ts">
2 import {
3 Table,
4 TableBody,
Get PRO
Need premium blocks and templates? Upgrade to PRO and get access.