Table
newTable parts with borders on the cells, sticky header and footer, pinned columns and five row sizes.
Open Props in the corner of the example to try size.
Installation
Usage
<script setup lang="ts">
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
</script>
<template>
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
<TableHead align="end">Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="row in rows" :key="row.id">
<TableCell>{{ row.name }}</TableCell>
<TableCell align="end">{{ row.amount }}</TableCell>
</TableRow>
</TableBody>
</Table>
</template>
The parts are shadcn-vue's, with two differences underneath. Borders sit on the cells, not the rows, under border-collapse: separate, so a sticky header and a pinned cell carry their own lines. Row state never paints the row: hover, selected and stripes set --table-row-bg on the row and every cell paints it, so a pinned cell can stay opaque and still follow the row. The tints are --table-hover-bg, --table-selected-bg and --table-stripe-bg on the table, for a theme to retune.
For columns and data, sorting, pinning and virtualization, see Data Table, which renders these parts' classes.
Table wraps the table in a container that scrolls sideways, as shadcn does. A sticky header cannot stick to that container, which never scrolls vertically. Inside a scroll area, the table's own sideways scrolling turns off and a sticky TableHeader sticks to the area; give the scroll area orientation="both" when the table can be wider than it. On the page, give the table overflow="visible".
Examples
Sticky header and footer
sticky on TableHeader and TableFooter. Inside a scroll area the table needs nothing more: its container stops scrolling, so the viewport is the only scroller between them.
Sticky to the page
Without a scroller of its own, overflow="visible" lets the header stick to the nearest scrolling ancestor, here the preview's page: scroll inside the preview and the header stays at its top. Under a fixed app bar, set --table-sticky-top on the table, or on the region whose tables stick to the page, to the bar's height, and --table-sticky-bottom for a footer. Leave it off tables in a scroll area of their own.
Pinned columns
pinned="start" or "end" on a head and its cells keeps the column in view while the rest scrolls sideways. The first pinned column needs nothing more; a second one sets --pin-start (or --pin-end) to the width of the first, in style or a class. pinnedEdge on the outermost pinned column draws the shadow, which shows only inside a scroll area and only while there is content under it. Right-to-left mirrors everything.
Sizes and stripes
size sets the row height through --table-row-h: 28px at xs, then 36, 44 (md, the default), 52 and 60px at xl. The cells' padding above and below the text is --table-cell-py, 8px, and 3px at xs so a line of text still fits. Both sit on the table, set by its size, for a theme to retune. striped tints every even body row.
Selection and clickable rows
selected tints a row and clickable makes it a target with tabindex="0". The row keeps no role, so screen readers still read it as a row; handle the click and the Enter and Space keys yourself, and stop the click on controls inside the row.
Row actions
A menu in a cell gives a row its actions: MenuTrigger with as-child wraps an icon button, and the Menu next to it opens from that button. Label the button with the row, so a screen reader hears which row's actions open.
Open a row's menu.
Empty
TableEmpty spans the row with colspan and centres its content.
Wide
With the default overflow="x" a table wider than its place scrolls sideways inside the container, as shadcn's does. Give the heads a min-w-* so columns keep a readable width.
Grouped rows
A TableBody per group keeps the borders right: the heading row is an ordinary row with a colspan cell, and only the last row of the last body drops its bottom border.
Truncation
layout="fixed" sizes the columns from the header row alone, so truncate on a cell cuts long text with an ellipsis instead of widening the column. Put the full text in title so it is still reachable.
Right-to-left
pinned="start" and "end" are logical, so under dir="rtl" the start column sits on the right and the shadows mirror.
Large
Two hundred rows in a scroll area with a sticky header, two pinned columns at the start (the second sets --pin-start to the width of the first) and one at the end, with sizes and stripes to switch.
In a card
The container has no border of its own, so a table sits in a Card as it is; pad the outer cells to the card's spacing and keep the caption for assistive technology.
Deployments
The last four builds of this project.
API Reference
Every part accepts class, merged over its base classes.
Table
| Prop | Default | Type and description |
|---|---|---|
| overflow | "x" | TableOverflow
|
| layout | "auto" | TableLayout
|
| size | "md" | TableSizeRow height, as |
| striped | false | booleanTints every even body row. A |
| Slot | Scope and description |
|---|---|
| default | {}Caption, header, bodies and footer. |
TableHeader
| Prop | Default | Type and description |
|---|---|---|
| sticky | — | booleanSticks the header to the top of the nearest scroller, on an opaque background. Needs |
| Slot | Scope and description |
|---|---|
| default | {}Header rows. |
TableFooter
| Prop | Default | Type and description |
|---|---|---|
| sticky | — | booleanSticks the footer to the bottom of the nearest scroller, on an opaque background. |
| Slot | Scope and description |
|---|---|
| default | {}Footer rows, on a muted tint with a top border. |
TableRow
| Prop | Default | Type and description |
|---|---|---|
| selected | — | booleanTints the row with the primary colour, as |
| clickable | — | booleanMarks the row as a target with a pointer cursor, |
| parity | — | TableParitySets |
| Slot | Scope and description |
|---|---|
| default | {}Cells. |
TableHead
| Prop | Default | Type and description |
|---|---|---|
| align | — | TableAlignText alignment, |
| pinned | — | TablePinnedSticks the cell to the |
| pinnedEdge | — | booleanDraws the shadow strip on the cell's outer edge. Give it to the last pinned column on each side. It shows only inside a |
| Slot | Scope and description |
|---|---|
| default | {}The column's title. |
TableCell
| Prop | Default | Type and description |
|---|---|---|
| align | — | TableAlignText alignment, |
| pinned | — | TablePinnedSticks the cell to the |
| pinnedEdge | — | booleanDraws the shadow strip on the cell's outer edge. Give it to the last pinned column on each side. It shows only inside a |
| truncate | — | booleanCuts the content with an ellipsis instead of growing the column. |
| Slot | Scope and description |
|---|---|
| default | {}The cell's content. |
TableCaption
| Prop | Default | Type and description |
|---|---|---|
| visible | — | booleanShows the caption under the table. By default it is read by assistive technology only. |
| Slot | Scope and description |
|---|---|
| default | {}The caption. |
TableEmpty
| Prop | Default | Type and description |
|---|---|---|
| colspan* | — | numberThe number of columns, so the message spans the row. |
| Slot | Scope and description |
|---|---|
| default | {}The message. |
TableBody
| Slot | Scope and description |
|---|---|
| default | {}Rows. Several bodies are fine; the last row of the last body has no bottom border. |
Props
- overflow
"x" TableOverflowxwraps the table in a container that scrolls sideways, as shadcn does; a sticky header then sticks to that container, which never scrolls vertically. Inside aScrollAreait acts asvisible, and the area scrolls instead.visibleclips nothing, so a sticky header sticks to the nearest scroller, such as the page.- layout
"auto" TableLayoutautosizes columns from their content, the browser default.fixedsizes them from the header row's widths alone, so a long cell truncates instead of widening its column and rows never shift the layout as they come and go.- size
"md" TableSizeRow height, as--table-row-hon the table.xsis 1.75rem,sm2.25rem,md2.75rem,lg3.25rem,xl3.75rem. Content that wraps still grows a row.- striped
false booleanTints every even body row. ATableRowwithparityset is tinted by that instead of its position.
Slots
- default
{}Caption, header, bodies and footer.
Props
- sticky
booleanSticks the header to the top of the nearest scroller, on an opaque background. Needsoverflow="visible"on the table, or the container is the scroller.
Slots
- default
{}Header rows.
Props
- sticky
booleanSticks the footer to the bottom of the nearest scroller, on an opaque background.
Slots
- default
{}Footer rows, on a muted tint with a top border.
Props
- selected
booleanTints the row with the primary colour, asdata-state="selected".- clickable
booleanMarks the row as a target with a pointer cursor,data-clickableandtabindex="0". Handleclickand the Enter and Space keys yourself; the row keeps no role.- parity
TableParitySetsdata-parity, for stripes that do not follow the row's position, as in a virtualized list.
Slots
- default
{}Cells.
Props
- align
TableAlignText alignment,start,centerorend.- pinned
TablePinnedSticks the cell to thestartorendedge while the table scrolls sideways, on an opaque background that follows the row. The offset from the edge is--pin-startor--pin-end, 0 by default; set it instylefor the second pinned column.- pinnedEdge
booleanDraws the shadow strip on the cell's outer edge. Give it to the last pinned column on each side. It shows only inside aScrollArea, while the area can scroll under it.
Slots
- default
{}The column's title.
Props
- align
TableAlignText alignment,start,centerorend.- pinned
TablePinnedSticks the cell to thestartorendedge while the table scrolls sideways, on an opaque background that follows the row. The offset from the edge is--pin-startor--pin-end, 0 by default; set it instylefor the second pinned column.- pinnedEdge
booleanDraws the shadow strip on the cell's outer edge. Give it to the last pinned column on each side. It shows only inside aScrollArea, while the area can scroll under it.- truncate
booleanCuts the content with an ellipsis instead of growing the column.
Slots
- default
{}The cell's content.
Props
- visible
booleanShows the caption under the table. By default it is read by assistive technology only.
Slots
- default
{}The caption.
Props
- colspan*
numberThe number of columns, so the message spans the row.
Slots
- default
{}The message.
Slots
- default
{}Rows. Several bodies are fine; the last row of the last body has no bottom border.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| table-container | data-overflow | The wrapper; scrolls sideways with overflow x. |
| table | data-size, data-striped | The table. Carries --table-row-h and --table-cell-py for its size, the default --table-row-bg and the three tints. |
| table-header | data-sticky | The thead; sticky at top --table-sticky-top, 0 by default. |
| table-body | — | A tbody. |
| table-footer | data-sticky | The tfoot; sticky at bottom --table-sticky-bottom, 0 by default. |
| table-row | data-state, data-clickable, data-parity | A tr. data-state is selected when selected; the row sets --table-row-bg for its cells. |
| table-head | data-align, data-pinned, data-pinned-edge | A th with scope col. |
| table-cell | data-align, data-pinned, data-pinned-edge, data-truncate | A td. |
| table-caption | data-visible | The caption, visually hidden unless visible. |
| table-empty | — | The empty row. |
Accessibility
Every TableHead renders scope="col". TableCaption is read by assistive technology and hidden from sight unless visible; a table with a purpose that is not obvious from its surroundings should have one. A clickable row is focusable and a pointer target but has no role, since role="button" on a row hides it from some screen readers; give the same action to a control inside the row for keyboard and screen reader users.