suiss/ui · Component
Table
Styled native table elements for displaying rows of structured data.
Table is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
Table renders a native table inside a container that scrolls horizontally when the table is too wide. Build it from TableHeader, TableBody and TableFooter, each holding TableRow elements with TableHead or TableCell cells. TableCaption adds a caption below the table.
The parts add spacing, hairline row dividers, hover and selection backgrounds, and tabular numbers in cells.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
-
Add the
cnhelper. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a Table:
Examples
Selected row
Set data-selected on a TableRow to highlight it. The attribute is matched by presence, so leave it off unselected rows rather than setting it to false.
Accessibility
- The parts render native table elements, so rows, column headers and cells keep their built-in roles.
TableCaptiongives the table its accessible name.
API Reference
Table
The table element, wrapped in a horizontally scrolling container.
Also accepts the props of React.ComponentProps<"table">.
Every part passes its props to the native element, so use standard attributes such as colSpan. Sorting, pagination and selection logic are not included; handle those in your own code. Each part sets a data-slot, such as table-container or table-cell, for styling.
TableHeader
The thead section for column headings.
Also accepts the props of React.ComponentProps<"thead">.
TableBody
The tbody section for data rows.
Also accepts the props of React.ComponentProps<"tbody">.
TableFooter
The tfoot section, shaded and bold, for totals or summaries.
Also accepts the props of React.ComponentProps<"tfoot">.
TableRow
A tr row with a bottom divider and hover background.
Also accepts the props of React.ComponentProps<"tr">.
Set data-selected to give the row the selected background. A row that contains an element with aria-expanded also gets it.
TableHead
A th column heading in small, secondary text.
Also accepts the props of React.ComponentProps<"th">.
Align numeric columns with text-end on both the TableHead and its cells. Header and data cells that contain a role="checkbox" drop their end padding.
TableCell
A td data cell with tabular numbers.
Also accepts the props of React.ComponentProps<"td">.
TableCaption
A caption shown below the table that also names it.
Also accepts the props of React.ComponentProps<"caption">.