wrolpi/app/src/components/ui/DataTable.tsx
Roland ee657d950f ui: give tables a real header, drop the column grid, quiet the row rules
Three things the file browser made obvious.

The header was not a surface.  Mantine gives thead no background at all, so
the header painted whatever was behind the table -- which is exactly what an
unstriped body row paints.  Against a striped body it read as one more stripe,
and bold type was the only thing distinguishing it.  It now carries `--head`,
the token that already means "recessed surface", with a full-strength 2px rule
under it: the header/body division is the one line in a table that carries
meaning, so it is the one allowed to be loud.

Column borders are gone.  A line around every cell is a lot of ink to repeat
what the column headings already say, and it was half of why the header
disappeared -- a bordered header cell looks like a cell in a grid.

Row rules were `--border`, the same weight as the outline around the table, so
a long table read as a stack of boxes.  They now use `--table-line`, a new
token per theme sitting between the surface and `--border`.  The table outline
is not a row rule and keeps `--border`; Mantine draws both from
`--table-border-color`, so that had to be restated.

In the file browser an icon and a filename are inline siblings in one cell.  An
inline SVG sits on the text baseline, so a 24px glyph beside 13px text hung
above it -- the icon looked centred in the row and the name looked like it was
resting on the icon's floor.  `vertical-align: middle` centres it, and the
margin is a gap that was never there at all: the folder icon touched the first
letter of its own name.

Cypress now loads App.css, as the app does.  Without it a spec covering the
file browser rows measures a component the app never renders.

One jest test and eleven browser tests, each checked red against a planted
regression.  Two notes for the next person:

  - `html .mantine-Table-table` is specificity, not decoration.  Mantine
    declares --table-border-color on its own hashed class, which ties with
    `.mantine-Table-table` and wins on source order.
  - the "quieter than the outline" test measures both lines against `--panel`.
    Nothing paints <html> under a mounted component, so measuring against the
    root background compares them to black, which ranks the *paler* line as the
    louder one -- the assertion passed backwards until it was pointed at the
    surface the rules actually sit on.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-01 17:10:03 -06:00

114 lines
3.8 KiB
TypeScript

import React from 'react';
import {Table as MTable, TableProps as MTableProps} from '@mantine/core';
import {Icon} from './Icon';
/*
* Tables.
*
* Rows are separated by a horizontal rule and nothing else. There are no column
* borders: a full grid draws a line around every cell, which is a lot of ink to
* say something the column headings already say, and it made the header row --
* which had no surface of its own -- read as one more cell in the grid. The
* header now carries `--head` and a full-strength rule under it, and the rules
* between body rows are `--table-line`, deliberately fainter. See ui.css.
*
* Wide tables scroll in their own container so the page body never scrolls
* sideways.
*
* The compound names mirror Semantic's (Header/Body/Row/Cell/HeaderCell/Footer)
* so migrating a call site is a rename, not a rewrite.
*/
export interface TableProps extends MTableProps {
/** Wrap in a horizontally scrollable container. On by default. */
scrollable?: boolean;
}
function TableBase({scrollable = true, ...props}: TableProps) {
const table = <MTable
withTableBorder
striped
highlightOnHover={false}
verticalSpacing={7}
horizontalSpacing={10}
{...props}
/>;
return scrollable ? <div className='wrolpi-table-scroll'>{table}</div> : table;
}
export interface RowProps extends React.ComponentPropsWithoutRef<'tr'> {
/** A failed row: takes the dashed danger border, in every theme. */
failed?: boolean;
}
function Row({failed, className, ...props}: RowProps) {
return <MTable.Tr
className={[failed ? 'wrolpi-row-failed' : '', className].filter(Boolean).join(' ') || undefined}
{...props}
/>
}
export interface CellProps extends React.ComponentPropsWithoutRef<'td'> {
/** Right-align and use tabular figures, so digits line up in the column. */
numeric?: boolean;
}
function Cell({numeric, style, ...props}: CellProps) {
return <MTable.Td
style={numeric ? {textAlign: 'right', fontVariantNumeric: 'tabular-nums', ...style} : style}
{...props}
/>
}
export type SortDirection = 'ascending' | 'descending';
export interface HeaderCellProps extends React.ComponentPropsWithoutRef<'th'> {
/** This column's current sort, or null/undefined when it is not the sorted one. */
sorted?: SortDirection | null;
/** Makes the header a sort control. Receives nothing; the caller knows its column. */
onSort?: () => void;
}
/**
* A header cell, optionally a sort control.
*
* When `onSort` is given the label becomes a real `<button>` — a click handler on
* the `<th>` alone is unreachable by keyboard — and the cell carries `aria-sort`,
* so the sort state is announced rather than left to the arrow glyph.
*/
function HeaderCell({sorted, onSort, children, ...props}: HeaderCellProps) {
if (!onSort) return <MTable.Th {...props}>{children}</MTable.Th>
return <MTable.Th
// `none` (not omitted) on the other columns tells assistive tech they are
// sortable but not currently sorted.
aria-sort={sorted ? sorted : 'none'}
{...props}
>
<button type='button' className='wrolpi-th-sort' onClick={onSort}>
{children}
<Icon
name={sorted === 'descending' ? 'arrow down' : 'arrow up'}
size={14}
className={sorted ? undefined : 'wrolpi-th-sort-idle'}
/>
</button>
</MTable.Th>
}
export const Table = Object.assign(TableBase, {
Header: MTable.Thead,
Body: MTable.Tbody,
Footer: MTable.Tfoot,
Row,
Cell,
HeaderCell,
// Mantine's own names, for new code.
Thead: MTable.Thead,
Tbody: MTable.Tbody,
Tfoot: MTable.Tfoot,
Tr: Row,
Td: Cell,
Th: HeaderCell,
ScrollContainer: MTable.ScrollContainer,
});