Bedform UIMessages

Table

Present structured data in rows and columns.

Table

*
string
*

Usage

import { Table } from "@bedform/ui/table";

export function WorldsTable() {
  return (
    <Table variant="striped">
      <table>
        <caption>Available worlds</caption>
        <thead>
          <tr>
            <th scope="col">World</th>
            <th scope="col">Players</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th scope="row">Mountains</th>
            <td>4 / 10</td>
          </tr>
          <tr>
            <th scope="row">Village</th>
            <td>8 / 20</td>
          </tr>
        </tbody>
      </table>
    </Table>
  );
}

Table renders a scrollable div around a native table, which you write yourself.

Sorting

Put a button with the ore-table-sort class inside a header with aria-sort. Its value identifies the column.

<th scope="col" aria-sort={sort.column === "players" ? sort.direction : "none"}>
  <button className="ore-table-sort" type="button" value="players">
    Players
    <span className="ore-table-sort-indicator" aria-hidden="true" />
  </button>
</th>

The sort event provides the column and the next direction. Your application sorts the data.

const [sort, setSort] = useState<OreTableSortDetail>({
  column: "",
  direction: "none",
});

<Table onSort={(event) => setSort(event.detail)}>…</Table>;

OreTableSortDetail is exported from @bedform/ui.

API

Table accepts every div attribute and forwards its ref to the div.

PropTypeDefault
onSort(event: CustomEvent<OreTableSortDetail>) => void—
variant"plain" | "striped""plain"
EventDetail
sort{ column: string; direction: "none" | "ascending" | "descending" }

Public classes

ClassPurpose
.ore-tableTable container and controller root
.ore-table-row-header-columnRow-header column width
.ore-table-data-columnData column width
.ore-table-cornerEmpty top-left corner cell
.ore-table-sortSortable header button
.ore-table-sort-indicatorAscending and descending indicator

CSS variables

The horizontally scrollable table also accepts all Scrollbar CSS variables.

VariablePurpose
--ore-table-unitBase size unit
--ore-table-borderCell border color
--ore-table-header-backgroundHeader cell background
--ore-table-row-backgroundRow background
--ore-table-row-background-alternateAlternate striped row background
--ore-table-foregroundTable text color
--ore-table-sort-inactiveInactive sort indicator color

Accessibility

Keep the native table structure, add a caption, and use scope on headers.
Sorting updates aria-sort. Announce results when there are no changes.

On this page