Skip to Content
ink-ui

๐Ÿ“Š Table

Structured data grid that measures its own columns, sorts, selects and virtualises rows, and truncates by display width.

<Table> renders a structured data grid with bold, muted column headers. Columns size themselves to their content, shrink to fit the terminal when the table is too wide, and either wrap or truncate whatever still does not fit.

import { Table } from "@kud/ink-ui"
import type { Column } from "@kud/ink-ui"

Props

PropTypeDefaultDescription
dataT[]โ€”Array of row objects
columnsColumn<T>[]โ€”Column definitions (see below)
gapnumber2Characters between columns
maxWidthnumberterminal widthWidth the table shrinks to fit
headerColorstringcolors.mutedColour of the header row
sortSortState | nullโ€”Sorts the rows and reserves the โ†‘/โ†“ slot on sortable headers; passing null makes the table sortable but unsorted
cursornumberโ€”Active row in display order; draws the โฏ gutter
focusedColumnnumberโ€”Underlines that header in the accent
selection"single" | "multiple"โ€”Draws the selection column: โ—/โ—‹ or โ– /โ–ก
selectedstring[][]Keys of the selected rows
rowKey(row, index) => stringindexIdentity of a row, so a selection survives a re-sort
heightnumberall rowsBody rows on screen; only those render, under a header that stays put
offsetnumberfollows cursorFirst visible row, when the host scrolls itself
emptyReactNode"No rows"Shown under the header when there are no rows

Every new prop is optional: a table given none of them renders exactly as it did before.

The table is generic over the row type T, so column keys are checked against your data shape.

Column<T> shape

FieldTypeDefaultDescription
keykeyof T & stringโ€”Object key to read for this column
headerstringโ€”Column heading text
widthnumbermeasuredFixed character width โ€” never shrunk
minWidthnumber3Floor a measured column shrinks to
align"left" | "center" | "right""left"Horizontal alignment of header and cells
overflow"wrap" | "truncate""truncate"What happens to content wider than the column
maxWidthnumberโ€”Ceiling on a measured or flex column
flexnumberโ€”Share of the spare width this column takes
format(value, row) => stringStringThe text a cell shows, measures and truncates
color(value, row) => stringโ€”A colour token for the cell โ€” never alone
compare(a, b) => numberby valueAscending order for this column
sortablebooleantruefalse keeps the column out of the sort cycle

Missing or nullish cell values render as an empty string.

How columns are sized

A column without a width is measured: it takes the widest of its header and its cells, counting display columns, so wide glyphs and CJK text are not undercounted. A cell holding newlines is measured by its widest line.

If the measured columns plus their gaps come to more than maxWidth, the widest column gives up characters first, one at a time, until the table fits or every measured column has reached its minWidth. Columns you gave an explicit width are never touched โ€” an over-wide table you asked for stays over-wide rather than quietly disagreeing with you.

A column with flex grows into whatever the table has left once every column is measured, in proportion to its weight and never past its own maxWidth. A fixed width always wins over flex.

Truncation

A truncated cell is clipped by display width, one grapheme at a time, and ends in โ€ฆ. A wide ideograph that would straddle the edge is dropped rather than spilling a column, and a flag, a ZWJ emoji or a letter with combining marks is kept whole or dropped whole โ€” never cut in half.

Overflow and alignment

overflow: "wrap" wraps a cell inside its column and grows the row to suit; the neighbouring cells stay top-aligned beside it. The default, overflow: "truncate", keeps every row one line tall and ends a clipped cell with โ€ฆ.

align positions a cell within its column. A cell that wraps to the full column width has nowhere left to move, so alignment shows on cells that are narrower than their column.

Examples

A measured table โ€” no widths at all:

<Table
  columns={[
    { key: "name", header: "Name" },
    { key: "status", header: "Status" },
  ]}
  data={[
    { name: "api", status: "ok" },
    { name: "db", status: "degraded" },
  ]}
/>

A wide, wrapping column beside fixed ones:

<Table
  columns={[
    { key: "file", header: "File", width: 32 },
    { key: "program", header: "Program", overflow: "wrap" },
    { key: "source", header: "Source", align: "right" },
  ]}
  data={items}
/>

Sorting, selection and ten thousand rows

Table stays presentational: it draws what it is given and binds no keys. useTable owns the keyboard and hands back tableProps to spread onto it.

import { Table, useTable, statusColumn, type Column } from "@kud/ink-ui"

const columns: Column<Job>[] = [
  { key: "id", header: "Job" },
  { key: "name", header: "Name", flex: 1, maxWidth: 40 },
  { key: "took", header: "Took", align: "right", format: (s) => `${s}s` },
  statusColumn<Job>({ key: "state", header: "State", status: (job) => job.state }),
]

const Jobs = ({ jobs }: { jobs: Job[] }) => {
  const table = useTable({
    data: jobs,
    columns,
    rowKey: (job) => job.id,
    selection: "multiple",
    height: 20,
  })
  return <Table maxWidth={96} {...table.tableProps} />
}
KeyDoes
โ†‘ โ†“ ยท k jMove the row cursor, clamped at the ends
PgUp PgDn ยท Home End ยท g GMove a page, or to either end
โ† โ†’ ยท h lMove the focused column
sCycle the focused column: ascending, descending, unsorted
โฃToggle the row (multiple), or pick it (single)
โ‡งโ†‘ โ‡งโ†“ ยท K JExtend a range from where it began; going back shrinks it
aSelect every row, or clear them all when every row is selected

The hook never binds q, esc or backspace โ€” those are the app's (see the keys contract in AGENTS.md). To let esc drop a selection, add an arm to the host's peel: if (table.selected.length) return (table.clearSelection(), true). Turn vimKeys off while a filter is typing, and pass isActive: false while another region owns the keyboard. table.hints holds the footer cells for what the table binds.

Selection and sort are each controlled (selected + onSelectedChange, sort + onSortChange) or uncontrolled (defaultSelected, defaultSort). The sort is stable, keeps empty values last in both directions and, through a re-sort, the cursor follows the row it was on.

Virtualisation. With height, only that many body rows render, under a header that never scrolls away. Columns are measured across the whole data set once โ€” memoised on data and columns, so pass the same references between renders โ€” and the window moves only when the cursor would leave it. Measured on 10 000 rows ร— 6 columns: the first frame (format, measure, sort) costs about 115 ms, and a cursor move about 23 ms end-to-end, against about 12 ms for the same table on 30 rows.

Status columns

statusColumn turns a column into a state: a glyph, a word and a colour token together (โœ“ success, โš  warning, โœ— error, โ„น info, โ—Œ pending), so the state reads without colour. It sorts by severity โ€” failures first โ€” and a row with no state stays empty.