diff --git a/changelog.d/matrix-grid.md b/changelog.d/matrix-grid.md new file mode 100644 index 000000000..f6a46253b --- /dev/null +++ b/changelog.d/matrix-grid.md @@ -0,0 +1,5 @@ +--- +section: kit +--- + +- **`MatrixGrid` renders two-axis tables without repeating headings**. The corner names both axes (`os × compiler`) and the edges list row and column values once. Each cell shows only what its headings don't already say, defaulting to a small mark with its full name as a tooltip and for screen readers; empty cells are hatched. Added or removed values and cells render as change regions. See [Renderer kit: type and values](docs/guides/renderer-kit-type.md#grids-over-two-axes). diff --git a/docs/guides/renderer-kit-components.md b/docs/guides/renderer-kit-components.md index 57c9afe7e..e2d67542d 100644 --- a/docs/guides/renderer-kit-components.md +++ b/docs/guides/renderer-kit-components.md @@ -324,6 +324,9 @@ accept token scale steps rather than raw lengths or colors—this is how - `Facts` and `Fact` (an item's facts as a key-value list) and `ValueList` (a list of values with changes marked inline, not in pills): see [Renderer kit: type and values](renderer-kit-type.md#facts-and-value-lists). +- `MatrixGrid` renders a two-axis table with row and column headings along the + edges so cells don't repeat them; see + [Renderer kit: type and values](renderer-kit-type.md#grids-over-two-axes). - `Stack` and `Inline` arrange children in a vertical column or wrapping row with `gap={1…8}` steps from the spacing scale. - `Text` renders text using a [type role](#type-roles) (`variant`), or `size` diff --git a/docs/guides/renderer-kit-type.md b/docs/guides/renderer-kit-type.md index 19f56096a..87a58247c 100644 --- a/docs/guides/renderer-kit-type.md +++ b/docs/guides/renderer-kit-type.md @@ -56,3 +56,15 @@ in monospace, inheriting the surrounding text color with no fill or border. `Slot` renders a named placeholder in a template (`Build on os`) as muted monospace without brackets. `Tag` renders a quiet category label (such as a lifetime or mode) with a recessed fill and no border. + +## Grids over two axes + +`MatrixGrid` renders a two-axis table (such as matrix runs by OS and compiler, +or flags by environment). The corner names both axes (`os × compiler`), the +edges list row and column values once, and each cell shows only what its +headings don't already say. `cell(row, column)` returns a cell, or `null` for an +empty hatched cell. A cell without `content` shows a small mark, using its full +name (`title`) as a tooltip and for screen readers (or `present`, "Present" by +default, when it has no title). Added or removed values and cells render as +change regions. Pass a `caption` to place several grids side by side, one per +extra dimension. diff --git a/docs/reference/renderer-kit.md b/docs/reference/renderer-kit.md index 8726a57f6..7561f6aee 100644 --- a/docs/reference/renderer-kit.md +++ b/docs/reference/renderer-kit.md @@ -4553,6 +4553,61 @@ KitRenderer(__0: { "component": ComponentClass, any> | Connects a kit renderer component to its host through the renderer frame protocol. +### `MatrixAxis` + +```ts +interface MatrixAxis +``` + +One axis of a `MatrixGrid`: its name, in words, and its values. + +| Member | Signature | Description | +|---|---|---| +| `code` | `"code"?: false \| true` | Sets its values as code (identifiers, versions), in the monospace font. | +| `name` | `"name": Iterable \| Promise \| ReactElement \| Promise \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined` | What the axis is ("os", "compiler"), in the label role. | +| `values` | `"values": Array` | The axis's values, in the order their headings run. | + +### `MatrixCell` + +```ts +interface MatrixCell +``` + +What one cell holds: whether something is there, its change, and anything its headings don't say. + +| Member | Signature | Description | +|---|---|---| +| `change` | `"change"?: "added" \| "modified" \| "removed" \| "renamed" \| "unchanged" \| undefined` | The cell's own change: it arrives or leaves while its row and column stay. | +| `content` | `"content"?: Iterable \| Promise \| ReactElement \| Promise \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined` | What tells the cell apart beyond its headings; a mark is drawn when there is nothing to show. | +| `title` | `"title"?: string` | The cell's whole name, for a pointer and assistive technology (its headings needn't repeat it on screen). | + +### `MatrixGrid` + +```ts +MatrixGrid(__0: { "caption"?: Iterable | Promise | ReactElement | Promise | Promise | ReactElement | ReactPortal | bigint | false | null | number | string | true | undefined } | { new (props: any, context: any): Component }> | ReactPortal | bigint | false | null | number | string | true | undefined> | ReactElement | Promise | ReactElement | ReactPortal | bigint | false | null | number | string | true | undefined> | Promise | ReactElement | ReactPortal | bigint | false | null | number | string | true | undefined } | { new (props: any, context: any): Component }> | ReactPortal | bigint | false | null | number | string | true | undefined; "cell": { (row: string, column: string | undefined): MatrixCell | null }; "columns"?: MatrixAxis; "emphasis"?: number; "label": string; "present"?: string; "rows": MatrixAxis; "vacant"?: string }): Element +``` + +A grid over two axes, as a table: the axes' names in its corner (`os × compiler`), each axis's values once along its +edge, and each cell what `cell(row, column)` returns: by default a small mark that something is there, with its whole +name (or `present`) as a tooltip and for assistive technology, so a cell never repeats its headings. A cell with nothing (`null`) +is vacant, on the kit's hatch. A value, row, column or cell that arrives or leaves is a kit change region, struck +through when it leaves, fading with `emphasis` (or the item's around it). Leave out `columns` for a single list of +cells along `rows`. `caption` names the grid when several stand together (one per further dimension). + +### `MatrixValue` + +```ts +interface MatrixValue +``` + +One value along a `MatrixGrid` axis. + +| Member | Signature | Description | +|---|---|---| +| `change` | `"change"?: "added" \| "modified" \| "removed" \| "renamed" \| "unchanged" \| undefined` | The value's own change: it arrives or leaves along with its row or column. | +| `key` | `"key": string` | Stable key: unique along its axis. | +| `label` | `"label"?: Iterable \| Promise \| ReactElement \| Promise \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined` | What the heading says; the key when left out. | + ### `MountOptions` ```ts diff --git a/packages/renderers/kit/react.css b/packages/renderers/kit/react.css index e9a64af31..cc49e2989 100644 --- a/packages/renderers/kit/react.css +++ b/packages/renderers/kit/react.css @@ -11,6 +11,7 @@ @import './styles/diff.css'; @import './styles/chips.css'; @import './styles/facts.css'; +@import './styles/matrix.css'; @import './styles/layout.css'; @import './styles/scope.css'; @import './styles/table.css'; diff --git a/packages/renderers/kit/src/react.ts b/packages/renderers/kit/src/react.ts index 8551f309d..9a537c726 100644 --- a/packages/renderers/kit/src/react.ts +++ b/packages/renderers/kit/src/react.ts @@ -25,6 +25,7 @@ export * from './react/status-tab.js' export * from './react/generated.js' export * from './react/diff-components.js' export * from './react/facts.js' +export * from './react/matrix-grid.js' export * from './react/icons.js' export {Fold, useFolds} from './react/fold.js' export {IdentifierText} from './react/identifiers.js' diff --git a/packages/renderers/kit/src/react/matrix-grid.tsx b/packages/renderers/kit/src/react/matrix-grid.tsx new file mode 100644 index 000000000..98e6b4494 --- /dev/null +++ b/packages/renderers/kit/src/react/matrix-grid.tsx @@ -0,0 +1,158 @@ +// A grid over two axes (a matrix's runs by OS and compiler, flags by environment): headings on each axis once, and in +// each cell only what its headings don't say, by default a mark that one exists there. +import type {ReactNode} from 'react' +import {identifier as identifierAttributes} from '@githubnext/projections-renderer-sdk/presentation' +import {statusLabels, type ChangeClassification} from '../emphasis.js' + +/** One value along a `MatrixGrid` axis. */ +export interface MatrixValue { + /** Stable key: unique along its axis. */ + key: string + /** What the heading says; the key when left out. */ + label?: ReactNode + /** The value's own change: it arrives or leaves along with its row or column. */ + change?: ChangeClassification | undefined +} + +/** One axis of a `MatrixGrid`: its name, in words, and its values. */ +export interface MatrixAxis { + /** What the axis is ("os", "compiler"), in the label role. */ + name: ReactNode + /** The axis's values, in the order their headings run. */ + values: readonly MatrixValue[] + /** Sets its values as code (identifiers, versions), in the monospace font. */ + code?: boolean +} + +/** What one cell holds: whether something is there, its change, and anything its headings don't say. */ +export interface MatrixCell { + /** What tells the cell apart beyond its headings; a mark is drawn when there is nothing to show. */ + content?: ReactNode + /** The cell's own change: it arrives or leaves while its row and column stay. */ + change?: ChangeClassification | undefined + /** The cell's whole name, for a pointer and assistive technology (its headings needn't repeat it on screen). */ + title?: string +} + +/** + * Whether a cell has content to show: anything React draws, so not nothing, a boolean, an empty string or a list of + * those (an empty `.map()`). A `0` shows. + */ +const shows = (content: unknown): boolean => + Array.isArray(content) ? content.some(shows) : content !== undefined && content !== null && typeof content !== 'boolean' && content !== '' + +const change = (value: ChangeClassification | undefined) => (value !== undefined && value !== 'unchanged' ? {'data-change': value} : {}) + +/** A change worth saying: anything but unchanged. */ +const said = (value: ChangeClassification | undefined): value is Exclude => + value !== undefined && value !== 'unchanged' + +function Heading({value, code}: {value: MatrixValue; code: boolean}) { + const changed = said(value.change) + return ( + + + {said(value.change) && {statusLabels[value.change]}: } + {value.label ?? value.key} + + + ) +} + +/** + * A grid over two axes, as a table: the axes' names in its corner (`os × compiler`), each axis's values once along its + * edge, and each cell what `cell(row, column)` returns: by default a small mark that something is there, with its whole + * name (or `present`) as a tooltip and for assistive technology, so a cell never repeats its headings. A cell with nothing (`null`) + * is vacant, on the kit's hatch. A value, row, column or cell that arrives or leaves is a kit change region, struck + * through when it leaves, fading with `emphasis` (or the item's around it). Leave out `columns` for a single list of + * cells along `rows`. `caption` names the grid when several stand together (one per further dimension). + */ +export function MatrixGrid({ + label, + rows, + columns, + cell, + caption, + vacant = 'None', + present = 'Present', + emphasis, +}: { + /** The grid's accessible name. */ + label: string + rows: MatrixAxis + columns?: MatrixAxis + cell: (row: string, column: string | undefined) => MatrixCell | null + caption?: ReactNode + /** What assistive technology hears in a vacant cell. */ + vacant?: string + /** What assistive technology hears in a marked cell without a `title`. */ + present?: string + emphasis?: number +}) { + const style = emphasis === undefined ? undefined : ({'--emphasis': emphasis} as Record) + const body = (row: MatrixValue, column: MatrixValue | undefined) => { + const found = cell(row.key, column?.key) + if (!found) { + return ( + + + {vacant} + + + ) + } + const changed = said(found.change) + const content = shows(found.content) + return ( + + + {content && {found.content}} + + {said(found.change) ? `${statusLabels[found.change]}: ` : ''} + {found.title ?? (content ? '' : present)} + + + + ) + } + return ( +
+ {caption !== undefined && caption !== null &&
{caption}
} + + + + + {columns?.values.map(value => ( + + ))} + + + + {rows.values.map(row => ( + + + {columns ? columns.values.map(column => body(row, column)) : body(row, undefined)} + + ))} + +
+ {columns ? ( + <> + {rows.name} × {columns.name} + + ) : ( + rows.name + )} + + +
+ +
+
+ ) +} diff --git a/packages/renderers/kit/styles/matrix.css b/packages/renderers/kit/styles/matrix.css new file mode 100644 index 000000000..4a0a6d1ae --- /dev/null +++ b/packages/renderers/kit/styles/matrix.css @@ -0,0 +1,127 @@ +/* + * Matrix grids (`MatrixGrid`): a table of small cells spaced apart, its axes' names in the corner in the label role, + * each axis's values once along its edge, centred over their columns. A cell its headings fully name shows a small + * mark, drawn as its own background so it centres at any zoom; a vacant one sits on the kit's hatch (a fill, never an + * edge). A heading or cell that arrives or leaves is a kit change region; a removed one is dashed, as removals are. + */ +.pk .pk-matrix { + margin: 0; + min-width: 0; +} +.pk .pk-matrix + .pk-matrix { + margin-top: var(--projections-space-3); +} +.pk .pk-matrix-caption { + margin: 0 0 var(--projections-space-1); +} +.pk .pk-matrix-grid { + border-collapse: separate; + border-spacing: var(--projections-space-1); + margin: calc(-1 * var(--projections-space-1)); +} +.pk .pk-matrix-grid th { + padding: 0 var(--projections-space-2) 0 0; + font-weight: var(--projections-font-weight-normal); + text-align: start; + vertical-align: middle; + white-space: nowrap; +} +.pk .pk-matrix-grid thead th:not(.pk-matrix-corner) { + padding: 0 var(--projections-space-2); + text-align: center; +} +.pk .pk-matrix-grid td { + padding: 0; + vertical-align: middle; +} +.pk .pk-matrix-value { + display: inline-flex; + border-radius: var(--projections-radius-sm); +} +.pk .pk-matrix-value[data-change] { + margin: 0 calc(-1 * var(--projections-space-1)); + padding: 0 var(--projections-space-1); +} +.pk .pk-matrix-cell { + display: flex; + align-items: center; + justify-content: center; + box-sizing: border-box; + min-width: var(--projections-space-8); + min-height: var(--projections-control-height-sm); + padding: 2px var(--projections-space-2); + border-radius: var(--projections-radius-sm); + background: var(--raised); + box-shadow: inset 0 0 0 var(--stroke) var(--border); + white-space: nowrap; +} +.pk .pk-matrix-cell[data-marked] { + background-image: radial-gradient(circle, var(--subtle) 2.5px, transparent 3px); + background-position: center; + background-repeat: no-repeat; +} +/* A changed cell's mark takes the change's hue, fading back to the plain mark as `emphasis` falls to 0. */ +.pk .pk-matrix-cell[data-marked][data-change] { + background-image: radial-gradient( + circle, + color-mix(in srgb, var(--pk-change-ink) calc(var(--emphasis, 1) * 100%), var(--subtle)) 2.5px, + transparent 3px + ); +} +.pk .pk-matrix-cell[data-vacant] { + background: var(--pk-vacant); + box-shadow: none; +} +.pk .pk-matrix .pk-region[data-change='removed'] { + box-shadow: none; + outline: var(--stroke) dashed color-mix(in srgb, var(--pk-region-edge) calc(var(--emphasis, 1) * 100%), transparent); + outline-offset: calc(-1 * var(--stroke)); +} +/* + * A changed cell fades with `emphasis` to an unchanged one: its fill and edge return to the plain cell's, and a removed + * cell's solid edge returns as its dashed one fades. A removed heading's or cell's strike and muting fade with it, as + * a removed chip's do, so at an endpoint the grid reads plain. + */ +.pk .pk-matrix-cell.pk-region[data-change] { + background-color: color-mix(in srgb, var(--pk-region-fill) var(--pk-region-amount), var(--raised)); + box-shadow: inset 0 0 0 var(--stroke) color-mix(in srgb, var(--pk-region-edge) var(--pk-region-amount), var(--border)); +} +.pk .pk-matrix .pk-matrix-cell.pk-region[data-change='removed'] { + box-shadow: inset 0 0 0 var(--stroke) color-mix(in srgb, var(--border) calc((1 - var(--emphasis, 1)) * 100%), transparent); +} +.pk .pk-matrix [data-change='removed'] .pk-diff-text { + color: color-mix(in srgb, var(--muted) calc(var(--emphasis, 1) * 100%), var(--text)); + text-decoration: line-through color-mix(in srgb, currentColor calc(var(--emphasis, 1) * 100%), transparent); +} +/* + * Forced colors drop fills, shadows and gradients, the mark and the hatch among them: an occupied cell keeps an edge in + * the system's text color and its mark as a dot of text (silent to assistive technology, which hears the cell's name + * and change), and a vacant cell has neither. A changed cell's edge is thicker. + */ +@media (forced-colors: active) { + .pk .pk-matrix-cell:not([data-vacant]) { + outline: var(--stroke) solid CanvasText; + outline-offset: calc(-1 * var(--stroke)); + } + /* + * A cell that arrives or changes draws a thicker edge, thinning to the plain one as `emphasis` falls to 0 (forced + * colors can't mix colors, so the thickness carries it). A removed one stays dashed. + */ + .pk .pk-matrix .pk-matrix-cell[data-change]:not([data-change='removed']) { + --pk-matrix-edge: calc(var(--stroke) + 2 * (var(--stroke-strong) - var(--stroke)) * var(--emphasis, 1)); + outline: var(--pk-matrix-edge) solid CanvasText; + outline-offset: calc(-1 * var(--pk-matrix-edge)); + } + /* A heading that arrives or changes is outlined the same way, around it rather than over its text, and not at all at 0. */ + .pk .pk-matrix .pk-matrix-value[data-change]:not([data-change='removed']) { + outline: calc((var(--stroke) + 2 * (var(--stroke-strong) - var(--stroke))) * var(--emphasis, 1)) solid CanvasText; + outline-offset: 0; + } + .pk .pk-matrix-cell[data-marked]::before { + content: '●'; + content: '●' / ''; + color: CanvasText; + font-size: 0.5em; + line-height: 1; + } +} diff --git a/packages/renderers/kit/test/matrix-grid.test.mjs b/packages/renderers/kit/test/matrix-grid.test.mjs new file mode 100644 index 000000000..005f5d728 --- /dev/null +++ b/packages/renderers/kit/test/matrix-grid.test.mjs @@ -0,0 +1,63 @@ +import {test} from 'node:test' +import assert from 'node:assert/strict' +import {createElement} from 'react' +import {renderToStaticMarkup} from 'react-dom/server' +import {MatrixGrid} from '../dist/react.js' +import {plain} from './markup.mjs' + +const grid = props => + renderToStaticMarkup( + createElement(MatrixGrid, { + label: 'build runs', + rows: {name: 'os', code: true, values: [{key: 'windows'}, {key: 'macos', change: 'added'}]}, + columns: {name: 'compiler', values: [{key: 'Transparent'}, {key: 'Background'}]}, + cell: (row, column) => + row === 'macos' && column === 'Background' ? null : {title: `${row} · ${column}`, ...(row === 'macos' ? {change: 'added'} : {})}, + ...props, + }), + ) + +test('a matrix names its axes once and marks each cell without repeating its headings', () => { + const markup = grid() + assert.match(markup, /os × compiler<\/th>/u) + // The table carries the grid's name too, for moving between tables. + assert.match(markup, //u) + assert.match( + markup, + /windows · Transparent<\/span><\/span>/u, + ) + // The row that arrives is a change region, said in words; the excluded combination is vacant. + assert.match( + markup, + /Added: <\/span>macos/u, + ) + assert.match(markup, /None<\/span><\/span>/u) + // On screen the cells say nothing their headings say. + const visible = plain(markup.replaceAll(/.*?<\/span>/gu, '')) + assert.equal(visible, 'os × compilerTransparentBackgroundwindowsmacos') +}) + +test('a cell with content of its own shows it, and the grid takes its own emphasis', () => { + const markup = grid({emphasis: 0.5, cell: () => ({content: 'node 22'})}) + assert.match(markup, /^
/u) + assert.match(markup, /node 22<\/span>/u) +}) + +test('a cell with nothing to show is marked and said present, and a zero is content', () => { + const markup = renderToStaticMarkup( + createElement(MatrixGrid, { + label: 'flags', + rows: {name: 'flag', values: ['a', 'b', 'c', 'd', 'e', 'f', 'g'].map(key => ({key}))}, + cell: row => + ({a: {content: null}, b: {content: ''}, c: {}, d: {content: 0}, e: {content: true}, f: {content: []}, g: {content: [null, false]}})[ + row + ], + present: 'On', + }), + ) + assert.equal(markup.match(/On<\/span><\/span>/gu)?.length, 6) + assert.match( + markup, + /0<\/span><\/span><\/span>/u, + ) +})