Table
A responsive table component.
| Invoice | Status | Method | Amount |
|---|---|---|---|
| INV001 | Credit card | $250.00 | |
| INV002 | PayPal | $150.00 | |
| INV003 | Bank transfer | $350.00 | |
| INV004 | Credit card | $450.00 | |
| INV005 | PayPal | $550.00 | |
| Total | $1,750.00 | ||
Installation
Install the following dependencies:
Set up the token layer and the StyleX compiler, if you have not already. See Installation.
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
Composition
The table is drawn on a tray. TableBody is a raised panel inside it, and
TableHeader, TableFooter and TableCaption stay on the tray around the
panel. Body rows highlight on hover, and a hairline divides them.
Alignment
Set align="end" on the heading and the cells of a column of figures. The
table uses tabular figures, so the digits line up.
Actions
Put a menu in the last column. A TableHead in a body row heads that row, as
INV001 does in the example above.
| Member | Role | |
|---|---|---|
OM Olivia Martinolivia@example.com | Owner | |
JL Jackson Leejackson@example.com | Admin | |
IN Isabella Nguyenisabella@example.com | Member |
API reference
Pass render to replace a part's element or compose it with another component.
Every part renders through Base UI's
useRender, and takes xstyle,
className and inline style alongside every attribute of its element.
Table
Renders a table inside a div that draws the tray and scrolls sideways when
the columns don't fit. xstyle, className and render go to the table.
| Prop | Type | Default |
|---|---|---|
containerProps | ComponentProps<"div"> & { xstyle?: StyleXStyles; className?: string } | — |
render | ReactElement | ((props, state) => ReactElement) | <table /> |
xstyle | StyleXStyles | — |
className | string | — |
TableHeader, TableBody, TableFooter
Render thead, tbody and tfoot. Each tells its rows which section they are
in, so only body rows are raised, divided and highlighted. TableBody draws
the panel behind itself. The footer's text is weight 500.
TableRow
Renders a tr. In the body it highlights on hover and draws a hairline below
itself. The last row has no hairline, because the panel's edge is below it.
TableHead
Renders a th. In the header it is a column label in small muted type with
scope="col". In a body row it heads that row at weight 500 with
scope="row". Pass scope to override either.
| Prop | Type | Default |
|---|---|---|
align | "start" | "center" | "end" | "start" |
TableCell
Renders a td. Cells don't wrap. To wrap one, pass xstyle.
| Prop | Type | Default |
|---|---|---|
align | "start" | "center" | "end" | "start" |
TableCaption
Renders a caption under the table, on the tray.
tableStyles
The style maps behind the component. Spread them onto any element.
| Key | What it is |
|---|---|
container | The tray and horizontal scroller |
table | The table: separate borders, type, tabular figures |
body | The raised panel behind the body |
footer | The footer's weight |
bodyRow | The variables a body row sets for its cells |
cell | A cell's padding and alignment |
bodyCell | A body cell's fill, hairline and corners, read from the row |
head | A column label's type |
caption | The caption |
aligns | start, center and end |
Data attributes
| Attribute | On | Effect |
|---|---|---|
data-slot="table-container" | the tray | Targets the tray from a parent |
data-slot="table" | the table | Targets the table from a parent |
data-slot="table-header" | the header | Targets the header from a parent |
data-slot="table-body" | the body | Targets the body from a parent |
data-slot="table-footer" | the footer | Targets the footer from a parent |
data-slot="table-row" | a row | Targets a row from a parent |
data-slot="table-head" | a heading | Targets a heading from a parent |
data-slot="table-cell" | a cell | Targets a cell from a parent |
data-slot="table-caption" | the caption | Targets the caption from a parent |
CSS variables
| Variable | On | Effect |
|---|---|---|
--carbon-table-radius | the tray | The tray's corner radius, 12px. The panel's is 4px less |
--carbon-table-panel-radius | the tray | The panel's corner, derived from the tray's |