Components · Data Display
Table
Rows and columns of data on native table elements, for the few places that need columns, such as usage, billing and permissions.
import { Table } from "@botiverse/raft-ui";A table lays out data in rows and columns, with a header row and a hairline between rows. Use it only when the data has to be compared down columns, such as usage, billing or the permissions of an app. Everything else reads better as a DescriptionList or as ListRow rows (Built from existing parts).
When to use
Anatomy
Table: thetableelement. It fills its container's width and has no outer border. Required.TableCaption: the table's name, shown under the table intext-captionandforeground-hint, or hidden withsr-only. Required unless the table has anaria-label.TableColgroupandTableColumn: optional column widths.TableHeader, with oneTableRowofTableHeadcells: the column headings. Required.TableSortableHead(@botiverse/raft-ui/wip): aTableHeadthat sorts the rows, in place ofTableHeadfor a column that sorts (Sorting). Its label is aTableSortButtonfilling the cell.TableSortButton(@botiverse/raft-ui/wip): the button of a heading that sorts, on its own, for a header row drawn withoutTable. It fills the heading cell around it and has no padding, no border and no type of its own: the cell sets the type, height and line, and you give the button the row's horizontal padding (Sorting).TableBody, with aTableRowofTableCells per row. Required.TableScroll: the area a table wider than its container scrolls sideways in, around theTable(Wide tables). Optional.
The header type, the lines between rows and how numbers are set are in Built from existing parts › Tables. Table draws the header and the lines; the last row has none under it.
States
Behavior
- Columns take the width of their content. When content would make them jump (a value that changes, rows that load in), add the
table-fixedclass toTableand set the widths withTableColgroupandTableColumn. - Text in a cell wraps at any character, so an ID or a name without spaces breaks inside its column instead of pushing the table past its container.
- Cells align their content to the top, so a row with a 2-line description keeps its other values on the first line.
Wide tables
A table that can be wider than its column scrolls sideways in an area of its own, TableScroll, and never widens the page: a page that scrolls sideways moves its header and navigation away with the table, and on a phone every vertical swipe then drifts sideways too.
- Wrap the
TableinTableScroll. It is a horizontal ScrollArea and takes ScrollArea's props butorientation, so the table scrolls under raft-ui's thin scrollbar, not the platform's, and never withoverflow-x-autoon a wrapper of your own (Native scrolling). A plain<table>goes in it the same way. - Give the
Tablethe least width its columns can be read at, such asmin-w-280. A column header never wraps and text in a cell wraps at any character, so without a least width the table first squeezes every column to its header's width, a long cell becoming a tall, narrow strip of broken words, and scrolls only once the headers themselves don't fit. With a least width, the table fills a container wider than that and scrolls in a narrower one, with its columns still readable. Columns of a fixed width (table-fixedwithTableColgroup) need the same least width. TableScrollhas no frame and no size of its own: it is as wide as its container, andclassNamegoes to it. Where its container is a flex row or a grid, give it the width there, such asw-full.- A header row that sticks to the top of the page (
sticky top-0) no longer does insideTableScroll: the area is now its scrolling container. A first column that sticks to the side still does. - It scrolls sideways only. A vertical wheel or swipe over the table scrolls the page as it does anywhere else, and at either end of the table a sideways scroll stops there and is not passed on to the page behind.
- The whole table scrolls, its header row with its columns and its caption with them.
Tablehas no column that stays in place. - The scrollbar is drawn, and the area is a tab stop, only while the table is wider than the area (Accessibility).
Sorting
A column whose order someone would want to change, such as usage, a date or a name, sorts the rows when its heading is clicked. The rule is the same for any row of column headings, including a list that draws its own header row instead of using Table, and on the web and in the app.
TableSortableHead draws such a heading. Which column the rows are sorted by, and in which direction, stays in your state: pass the column's sort ("ascending", "descending", or nothing when the rows aren't sorted by it), and set it from onSortChange.
- Make the heading's label a button that fills the heading cell, so a click anywhere on the heading sorts. Put
aria-sorton the heading cell, not on the button:ascendingordescendingon the column the rows are sorted by, nothing on the others. Screen readers then read the direction with the column's name, so the label stays the column's name: "Memory", not "Sort by memory". A header row drawn withoutTablegives each heading cellrole="columnheader", inside arole="row"in arole="table"orrole="grid": screen readers readaria-sorton nothing else. - At rest a heading that sorts looks like any other, in
foreground-hint. Under the pointer it turnsforeground, with no fill, so the header row still reads as a row of labels. - The column the rows are sorted by has its heading in
foregroundand a 12pxarrow-down(descending: the values get smaller down the column) orarrow-up(ascending) 4px from its label. No other heading shows an arrow, not even under the pointer, so the one arrow on screen always says what the order is. Every heading that sorts keeps the arrow's room, empty when it isn't sorted: a column sizes to its widest content, its heading included, so without it the columns move sideways each time the sort moves to another one. - The arrow goes after the label, except in a right-aligned column, such as numbers, where it goes before it: the label stays over its numbers' right edge.
- Clicking the sorted column reverses it. Clicking another column sorts by that column in its first direction: largest or newest first for numbers and dates, A to Z for text. There are two directions and no third click back to unsorted: a click that removes the arrow looks like a click that did nothing.
- One column sorts at a time. Rows with the same value keep their order from before, and rows with no value (
—, such as an offline computer's usage) go last in both directions: a missing value is not the smallest one. - When the rows start in an order no column gives, such as the order they were added, no heading shows an arrow until one is clicked. If that order is worth going back to, make it a column, such as Added, rather than a third click. Remember the chosen sort for that list on that device, so coming back to the page doesn't reset it.
- On a touch screen the heading's button has a 44px-high hit area, as every control does (Accessibility). It grows upward only, so it never takes a tap from the first row: above a header row lower than 44px it reaches into what sits over the row, by 44px minus the row's height (16px over a 28px row). Leave that space free of controls, or give the controls there a stacking context of their own (
relative z-10), so they keep their taps.
TableSortableHead sets aria-sort from sort, colors the label, draws the arrow and gives the touch hit area. A click calls onSortChange with the next direction: the reverse of sort, or firstSort on a column the rows aren't sorted by. firstSort is "ascending" unless set; pass "descending" for numbers and dates. Pass align="right" in a right-aligned column, as on its cells, so the arrow goes before the label. Native props, className and ref go to the heading cell, not to the button. Sorting the rows themselves, with the missing values last, and remembering the sort are yours: the part never sees the rows.
A header row drawn without Table, such as a list's grid of divs, has the type of the list it heads, such as text-mono-meta for list metadata (Typography), and its own height and line. Put a TableSortButton in each heading cell that sorts. It is the same button, with the same label color, arrow, arrow room, touch hit area and sort, firstSort, onSortChange and align, but with no padding and no border, and it inherits the cell's font. It fills the cell, so the whole cell is the target and the focus ring goes around it: give the cell no padding and let it stretch to the row's height (don't set items-center on the row; the button centers its label), and give the button the row's horizontal padding with className, such as px-2, so its label starts and ends where the values do. Don't wrap the button in another element or give the cell a height of its own that the button doesn't fill: the target would shrink back to the label. Set aria-sort={sort} on the cell yourself, and give the cell role="columnheader" inside a role="row" in a role="table" or role="grid". In development the button warns when it has no such cell around it or the cell's aria-sort isn't its sort. Native props, className and ref go to the button.
TableSortableHead also takes render, such as render={<div role="columnheader" />}, for a cell that should look like a TableHead: the header type, the line under it and the cell's padding.
Content
- Write column headings in sentence case: Resource, Access, Last run (Writing).
TableCelldoesn't set numbers in Space Mono or align them. Set Space Mono on the cell (font-mono), and passalign="right"to the cell and to its column's heading,TableHeadorTableSortableHead, so the heading sits over its numbers. The heading keeps the header type, not Space Mono. Don't align with a class: on aTableSortableHead, or on the cell around aTableSortButton, it does nothing, because the label is a button filling the cell; passalign="right"to the button too.- Write a caption in sentence case that names what the rows are: Recent builds.
Accessibility
- The parts render native
table,caption,thead,tbody,tr,thandtdelements, so screen readers can move by row and column. TableHeadsetsscope="col". Row headings (scope="row") work but have no style of their own.TableSortableHeadsetsscope="col"too, but none withrender, since only athtakesscope. It setsaria-sorton the heading cell only while the rows are sorted by its column. Its label is a nativebuttonnamed by the column's name alone; the arrow isaria-hidden, so the direction is read fromaria-sort. Withrender, the element it renders keepsaria-sort, so give itrole="columnheader"(Sorting).TableSortButtonsets noaria-sort: a button can't carry it. Set it on the heading cell around the button, arole="columnheader"in arole="row"in arole="table"orrole="grid", from the samesort. In development it warns in the console when there is no such cell or the two differ.TableScrollis a tab stop while the table is wider than it, so someone using the keyboard can focus it and scroll with the arrow keys; a table that fits adds no stop. Leave that on: a table's cells often take no focus, and then nothing else can scroll it. PasstabIndex={-1}only when every row holds a control that takes focus and scrolls into view. The area is a<div>with no role, so give itrole="region"and anaria-labelnaming the table, so focusing it says what it is (ScrollArea).- Name every table with a
TableCaption, visible orsr-only. Usearia-labelonTableonly when there can be no caption. - Give a control in a cell a name that includes its row: "Access for Messages".
Rules
- Keep the header row even when the design shows none: hide it with
sr-only. Without it, the columns lose their names. - Wrap a table that can be wider than its container in
TableScroll, with a least width on theTable(Wide tables). Never let it widen the page. - Don't add vertical lines, an outer border or a header fill. A composite adds no new lines or colors (Built from existing parts).
API
Table, TableBody, TableCaption
Also takes the attributes of its element.
TableCell
Also takes the attributes of its element.
TableColgroup, TableColumn
Also takes the attributes of its element.
TableHead
Also takes the attributes of its element.
TableHeader, TableRow
Also takes the attributes of its element.
TableScroll
Also takes the attributes of its element.
TableSortableHead
Also takes the attributes of its element.
TableSortButton
Also takes the attributes of its element.
Related
Changes
- 0.97.0 · Added
TableScroll, the area a wide table scrolls sideways in ([Table › Wide tables](https://raft-design.botiverse.build/components/table/#wide-tables)). A part of Table: a horizontal ScrollArea around aTableor a plain<table>, withdata-slot="table-scroll". It takes ScrollArea's props butorientation, and has no frame or size of its own. - 0.9.5 · FixedOn a touch screen, a tap no longer leaves a hover highlight on:
TableBodyrows, anInboxItem's pressed toggle action, the Activity inbox sort button and aFileRow(when itsFileRowOpenis hovered) now only show their hover on devices that can hover. With a mouse they look and behave as before.