componentes / exibição
Table
Uma tabela nativa estilizada para linhas e colunas de dados.
| Invoice | Customer | Status | Due | Amount |
|---|---|---|---|---|
| INV-1042 | Northwind Labs | paid | Oct 02 | $2,450.00 |
| INV-1043 | Acme Logistics | pending | Oct 18 | $890.00 |
| INV-1044 | Helio Studio | paid | Oct 21 | $1,320.50 |
| INV-1045 | Brightside Co. | failed | Sep 28 | $640.00 |
| INV-1046 | Orbit Foods | pending | Nov 04 | $3,075.00 |
table-default.tsx
import { Badge } from "@/components/ui/badge";
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
const invoices = [
{ id: "INV-1042", customer: "Northwind Labs", status: "paid", due: "Oct 02", amount: "$2,450.00" },
{ id: "INV-1043", customer: "Acme Logistics", status: "pending", due: "Oct 18", amount: "$890.00" },
{ id: "INV-1044", customer: "Helio Studio", status: "paid", due: "Oct 21", amount: "$1,320.50" },
{ id: "INV-1045", customer: "Brightside Co.", status: "failed", due: "Sep 28", amount: "$640.00" },
{ id: "INV-1046", customer: "Orbit Foods", status: "pending", due: "Nov 04", amount: "$3,075.00" },
] as const;
const variant = { paid: "brand", pending: "muted", failed: "destructive" } as const;
export default function TableDefault() {
return (
<Table aria-label="Recent invoices">
<TableCaption>Recent invoices</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Invoice</TableHead>
<TableHead>Customer</TableHead>
<TableHead>Status</TableHead>
<TableHead>Due</TableHead>
<TableHead numeric>Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{invoices.map((i) => (
<TableRow key={i.id}>
<TableCell className="whitespace-nowrap font-medium">{i.id}</TableCell>
<TableCell>{i.customer}</TableCell>
<TableCell>
<Badge variant={variant[i.status]}>{i.status}</Badge>
</TableCell>
<TableCell className="text-muted-foreground">{i.due}</TableCell>
<TableCell numeric>{i.amount}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}Instalação
$ npx shadcn@latest add https://cd-ui.vercel.app/r/table.jsonprimeira vez? configure o components.json e o tema antes: veja instalação.
Uso
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>Ada</TableCell>
</TableRow>
</TableBody>
</Table>Exemplos
Listrada
variant="striped" colore uma linha sim, outra não, e ajuda o olho a seguir uma linha entre células largas.
| Member | Role | Last seen |
|---|---|---|
M Marina Alvesmarina@northwind.dev | Owner | 2 min ago |
T Tiago Ferraztiago@northwind.dev | Admin | 1 hour ago |
J Joana Reisjoana@northwind.dev | Editor | Yesterday |
C Caio Mendescaio@northwind.dev | Editor | 3 days ago |
L Lara Costalara@northwind.dev | Viewer | Last week |
table-striped.tsx
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
const team = [
{ name: "Marina Alves", email: "marina@northwind.dev", role: "Owner", seen: "2 min ago" },
{ name: "Tiago Ferraz", email: "tiago@northwind.dev", role: "Admin", seen: "1 hour ago" },
{ name: "Joana Reis", email: "joana@northwind.dev", role: "Editor", seen: "Yesterday" },
{ name: "Caio Mendes", email: "caio@northwind.dev", role: "Editor", seen: "3 days ago" },
{ name: "Lara Costa", email: "lara@northwind.dev", role: "Viewer", seen: "Last week" },
] as const;
export default function TableStriped() {
return (
<Table aria-label="Team members" variant="striped">
<TableHeader>
<TableRow>
<TableHead>Member</TableHead>
<TableHead>Role</TableHead>
<TableHead>Last seen</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{team.map((m) => (
<TableRow key={m.email}>
<TableCell>
<div className="flex items-center gap-3">
<Avatar className="size-8">
<AvatarFallback>{m.name.slice(0, 1)}</AvatarFallback>
</Avatar>
<div className="flex flex-col leading-tight">
<span className="font-medium">{m.name}</span>
<span className="text-muted-foreground text-xs">{m.email}</span>
</div>
</div>
</TableCell>
<TableCell>
<Badge variant={m.role === "Owner" ? "brand" : "outline"}>{m.role}</Badge>
</TableCell>
<TableCell className="text-muted-foreground">{m.seen}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}Compacta, com rodapé
density="compact" aperta as linhas. TableFooter guarda o total e numeric alinha os dígitos à direita.
| Item | Qty | Price | Total |
|---|---|---|---|
| Pro plan, 5 seats | 5 | $12.00 | $60.00 |
| Extra storage (100 GB) | 2 | $8.00 | $16.00 |
| Priority support | 1 | $30.00 | $30.00 |
| Custom domain | 1 | $5.00 | $5.00 |
| Audit log retention | 1 | $15.00 | $15.00 |
| Total due | $126.00 | ||
table-compact.tsx
import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "@/components/ui/table";
const lines = [
{ item: "Pro plan, 5 seats", qty: 5, price: 12 },
{ item: "Extra storage (100 GB)", qty: 2, price: 8 },
{ item: "Priority support", qty: 1, price: 30 },
{ item: "Custom domain", qty: 1, price: 5 },
{ item: "Audit log retention", qty: 1, price: 15 },
] as const;
const money = (n: number) => `$${n.toFixed(2)}`;
export default function TableCompact() {
const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0);
return (
<Table aria-label="October bill" density="compact">
<TableHeader>
<TableRow>
<TableHead>Item</TableHead>
<TableHead numeric>Qty</TableHead>
<TableHead numeric>Price</TableHead>
<TableHead numeric>Total</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{lines.map((l) => (
<TableRow key={l.item}>
<TableCell>{l.item}</TableCell>
<TableCell numeric>{l.qty}</TableCell>
<TableCell numeric>{money(l.price)}</TableCell>
<TableCell numeric>{money(l.qty * l.price)}</TableCell>
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<TableCell colSpan={3}>Total due</TableCell>
<TableCell numeric>{money(total)}</TableCell>
</TableRow>
</TableFooter>
</Table>
);
}Cabeçalho e primeira coluna fixos
stickyHeader mantém o cabeçalho enquanto as linhas rolam e stickyFirstColumn mantém a primeira coluna enquanto a tabela rola para o lado. Estreite a janela para ver.
| Deploy | State | Branch | Environment | Commit | Duration | Bundle |
|---|---|---|---|---|---|---|
| dpl_5nw0 | ready | main | production | a13f09 | 24s | 1.2 MB |
| dpl_5nj1 | ready | main | preview | a15df8 | 41s | 1.9 MB |
| dpl_5n62 | building | feat/billing | preview | a17ce7 | 58s | 2.6 MB |
| dpl_5mt3 | error | fix/login | preview | a19bd6 | 75s | 1.5 MB |
| dpl_5mg4 | ready | main | production | a1bac5 | 31s | 2.2 MB |
| dpl_5m35 | ready | feat/search | preview | a1d9b4 | 48s | 2.9 MB |
| dpl_5lq6 | ready | main | production | a1f8a3 | 65s | 1.8 MB |
| dpl_5ld7 | ready | main | preview | a21792 | 82s | 2.5 MB |
| dpl_5l08 | building | feat/billing | preview | a23681 | 38s | 1.4 MB |
| dpl_5kn9 | error | fix/login | preview | a25570 | 55s | 2.1 MB |
| dpl_5ka10 | ready | main | production | a2745f | 72s | 2.8 MB |
| dpl_5jx11 | ready | feat/search | preview | a2934e | 28s | 1.7 MB |
| dpl_5jk12 | ready | main | production | a2b23d | 45s | 2.4 MB |
| dpl_5j713 | ready | main | preview | a2d12c | 62s | 1.3 MB |
| dpl_5iu14 | building | feat/billing | preview | a2f01b | 79s | 2.0 MB |
| dpl_5ih15 | error | fix/login | preview | a30f0a | 35s | 2.7 MB |
| dpl_5i416 | ready | main | production | a32df9 | 52s | 1.6 MB |
| dpl_5hr17 | ready | feat/search | preview | a34ce8 | 69s | 2.3 MB |
| dpl_5he18 | ready | main | production | a36bd7 | 25s | 1.2 MB |
| dpl_5h119 | ready | main | preview | a38ac6 | 42s | 1.9 MB |
| dpl_5go20 | building | feat/billing | preview | a3a9b5 | 59s | 2.6 MB |
| dpl_5gb21 | error | fix/login | preview | a3c8a4 | 76s | 1.5 MB |
| dpl_5fy22 | ready | main | production | a3e793 | 32s | 2.2 MB |
| dpl_5fl23 | ready | feat/search | preview | a40682 | 49s | 2.9 MB |
| dpl_5f824 | ready | main | production | a42571 | 66s | 1.8 MB |
| dpl_5ev25 | ready | main | preview | a44460 | 83s | 2.5 MB |
| dpl_5ei26 | building | feat/billing | preview | a4634f | 39s | 1.4 MB |
| dpl_5e527 | error | fix/login | preview | a4823e | 56s | 2.1 MB |
| dpl_5ds28 | ready | main | production | a4a12d | 73s | 2.8 MB |
| dpl_5df29 | ready | feat/search | preview | a4c01c | 29s | 1.7 MB |
table-sticky.tsx
import { Badge } from "@/components/ui/badge";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
const branches = ["main", "main", "feat/billing", "fix/login", "main", "feat/search"];
const envs = ["production", "preview", "preview", "preview", "production", "preview"];
const states = ["ready", "ready", "building", "error", "ready", "ready"] as const;
const variant = { ready: "brand", building: "muted", error: "destructive" } as const;
const deploys = Array.from({ length: 30 }, (_, i) => ({
id: `dpl_${(7340 - i * 13).toString(36)}${i}`,
state: states[i % states.length],
branch: branches[i % branches.length],
env: envs[i % envs.length],
commit: (0xa13f09 + i * 7919).toString(16),
duration: 24 + ((i * 17) % 61),
size: 1.2 + ((i * 7) % 18) / 10,
}));
export default function TableSticky() {
return (
<Table aria-label="Deploys" containerClassName="max-h-80" density="compact" stickyFirstColumn stickyHeader>
<TableHeader>
<TableRow>
<TableHead>Deploy</TableHead>
<TableHead>State</TableHead>
<TableHead>Branch</TableHead>
<TableHead>Environment</TableHead>
<TableHead>Commit</TableHead>
<TableHead numeric>Duration</TableHead>
<TableHead numeric>Bundle</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{deploys.map((d) => (
<TableRow key={d.id}>
<TableCell className="whitespace-nowrap font-medium font-mono">{d.id}</TableCell>
<TableCell>
<Badge variant={variant[d.state]}>{d.state}</Badge>
</TableCell>
<TableCell className="whitespace-nowrap font-mono text-xs">{d.branch}</TableCell>
<TableCell>{d.env}</TableCell>
<TableCell className="font-mono text-muted-foreground text-xs">{d.commit}</TableCell>
<TableCell numeric>{d.duration}s</TableCell>
<TableCell numeric>{d.size.toFixed(1)} MB</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}Estado vazio
TableEmpty ocupa uma linha inteira, de ponta a ponta, com uma mensagem para quando não há nada a mostrar.
| Invoice | Customer | Amount |
|---|---|---|
No invoices yet They show up here as soon as you send the first one. | ||
table-empty.tsx
import { InboxIcon } from "lucide-react";
import { Table, TableBody, TableEmpty, TableHead, TableHeader, TableRow } from "@/components/ui/table";
export default function TableEmptyExample() {
return (
<Table aria-label="Invoices">
<TableHeader>
<TableRow>
<TableHead>Invoice</TableHead>
<TableHead>Customer</TableHead>
<TableHead numeric>Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableEmpty colSpan={3}>
<InboxIcon aria-hidden="true" className="mx-auto mb-2 size-6" />
<p className="font-medium text-foreground">No invoices yet</p>
<p>They show up here as soon as you send the first one.</p>
</TableEmpty>
</TableBody>
</Table>
);
}Cartoon
variant="cartoon" usa o contorno grosso e a sombra dura dos tokens cartoon, com cabeçalho na cor da marca.
| # | Player | Quest | Title | Points |
|---|---|---|---|---|
| 1 | Pixel Fox | Dragon cave | legend | 9,840 |
| 2 | Mochi | Cloud castle | hero | 8,715 |
| 3 | Bruno B. | Candy swamp | hero | 7,302 |
| 4 | Zeca | Dragon cave | rookie | 5,190 |
table-cartoon.tsx
import { Badge } from "@/components/ui/badge";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
const scores = [
{ rank: 1, player: "Pixel Fox", quest: "Dragon cave", status: "legend", points: 9840 },
{ rank: 2, player: "Mochi", quest: "Cloud castle", status: "hero", points: 8715 },
{ rank: 3, player: "Bruno B.", quest: "Candy swamp", status: "hero", points: 7302 },
{ rank: 4, player: "Zeca", quest: "Dragon cave", status: "rookie", points: 5190 },
] as const;
export default function TableCartoon() {
return (
<Table aria-label="Leaderboard" containerClassName="mb-1 me-1" variant="cartoon">
<TableHeader>
<TableRow>
<TableHead numeric>#</TableHead>
<TableHead>Player</TableHead>
<TableHead>Quest</TableHead>
<TableHead>Title</TableHead>
<TableHead numeric>Points</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{scores.map((s) => (
<TableRow key={s.player}>
<TableCell className="font-bold" numeric>
{s.rank}
</TableCell>
<TableCell className="font-bold">{s.player}</TableCell>
<TableCell>{s.quest}</TableCell>
<TableCell>
<Badge variant="cartoon">{s.status}</Badge>
</TableCell>
<TableCell className="font-bold" numeric>
{s.points.toLocaleString("en-US")}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}API
Table
<table> dentro de um contêiner que rola para o lado em telas pequenas. Componente de servidor.
| prop | tipo | padrão |
|---|---|---|
variant
| "default" | "striped" | "cartoon" | "default" |
densityEspaçamento das linhas. | "compact" | "default" | "comfortable" | "default" |
stickyHeaderMantém o cabeçalho visível enquanto as linhas rolam (o contêiner ganha altura máxima). | boolean | false |
stickyFirstColumnMantém a primeira coluna no lugar enquanto a tabela rola para o lado. | boolean | false |
aria-labelDá nome ao contêiner de rolagem como região. Ele é focável por teclado de qualquer jeito, então dá pra rolar com as setas. | string | - |
classNameClasses extras, mescladas com | string | - |
TableHeader · TableBody · TableFooter
TableHeader, TableBody e TableFooter correspondem a <thead>, <tbody> e <tfoot>.
| prop | tipo | padrão |
|---|---|---|
classNameClasses extras, mescladas com | string | - |
TableEmpty
Uma linha de largura total com uma mensagem, para uma tabela sem linhas.
| prop | tipo | padrão |
|---|---|---|
colSpanQuantas colunas a tabela tem. | number | - |
classNameClasses extras, mescladas com | string | - |
TableRow · TableHead · TableCell · TableCaption
TableRow, TableHead, TableCell e TableCaption correspondem a <tr>, <th>, <td> e <caption>. A linha é destacada no hover e quando tem aria-selected ou data-state="selected".
| prop | tipo | padrão |
|---|---|---|
numericEm | boolean | false |
classNameClasses extras, mescladas com | string | - |
Acessibilidade
- Mantém a semântica nativa de tabela. Use
TableHead(<th>) nos cabeçalhos eTableCaptionpara dar nome à tabela.