Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/matrix-grid.md
Original file line number Diff line number Diff line change
@@ -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).
3 changes: 3 additions & 0 deletions docs/guides/renderer-kit-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions docs/guides/renderer-kit-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
55 changes: 55 additions & 0 deletions docs/reference/renderer-kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -4553,6 +4553,61 @@ KitRenderer(__0: { "component": ComponentClass<RendererProps<undefined>, 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<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, string \| { (props: any): Iterable<ReactNode, any, any> \| Promise \| Promise<ReactNode> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement<unknown, string \| { (props: any): Iterable<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise<ReactNode> \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined` | What the axis is ("os", "compiler"), in the label role. |
| `values` | `"values": Array<MatrixValue>` | 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<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, string \| { (props: any): Iterable<ReactNode, any, any> \| Promise \| Promise<ReactNode> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement<unknown, string \| { (props: any): Iterable<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise<ReactNode> \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> \| 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<ReactNode, any, any> | Promise<Iterable<ReactNode, any, any> | ReactElement<unknown, string | { (props: any): Iterable<ReactNode, any, any> | Promise | Promise<ReactNode> | ReactElement<unknown, recursive> | ReactPortal | bigint | false | null | number | string | true | undefined } | { new (props: any, context: any): Component<any, any, any> }> | ReactPortal | bigint | false | null | number | string | true | undefined> | ReactElement<unknown, string | { (props: any): Iterable<ReactNode, any, any> | Promise<Iterable<ReactNode, any, any> | ReactElement<unknown, recursive> | ReactPortal | bigint | false | null | number | string | true | undefined> | Promise<ReactNode> | ReactElement | ReactPortal | bigint | false | null | number | string | true | undefined } | { new (props: any, context: any): Component<any, any, any> }> | 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<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, string \| { (props: any): Iterable<ReactNode, any, any> \| Promise \| Promise<ReactNode> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement<unknown, string \| { (props: any): Iterable<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise<ReactNode> \| ReactElement \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined` | What the heading says; the key when left out. |

### `MountOptions`

```ts
Expand Down
1 change: 1 addition & 0 deletions packages/renderers/kit/react.css
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 1 addition & 0 deletions packages/renderers/kit/src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
158 changes: 158 additions & 0 deletions packages/renderers/kit/src/react/matrix-grid.tsx
Original file line number Diff line number Diff line change
@@ -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<ChangeClassification, 'unchanged'> =>
value !== undefined && value !== 'unchanged'

function Heading({value, code}: {value: MatrixValue; code: boolean}) {
const changed = said(value.change)
return (
<span className={changed ? 'pk-matrix-value pk-region' : 'pk-matrix-value'} {...change(value.change)}>
<span className="pk-label pk-diff-text" {...(code ? identifierAttributes : {})}>
{said(value.change) && <span className="sr-only">{statusLabels[value.change]}: </span>}
{value.label ?? value.key}
</span>
</span>
)
}

/**
* 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<string, number>)
const body = (row: MatrixValue, column: MatrixValue | undefined) => {
const found = cell(row.key, column?.key)
if (!found) {
return (
<td key={column?.key ?? 'cell'}>
<span className="pk-matrix-cell" data-vacant="">
<span className="sr-only">{vacant}</span>
</span>
</td>
)
}
const changed = said(found.change)
const content = shows(found.content)
return (
<td key={column?.key ?? 'cell'}>
<span
className={changed ? 'pk-matrix-cell pk-region' : 'pk-matrix-cell'}
data-marked={content ? undefined : ''}
title={found.title}
{...change(found.change)}
>
{content && <span className="pk-matrix-content pk-diff-text">{found.content}</span>}
<span className="sr-only">
{said(found.change) ? `${statusLabels[found.change]}: ` : ''}
{found.title ?? (content ? '' : present)}
</span>
</span>
</td>
)
}
return (
<figure className="pk-matrix" aria-label={label} style={style}>
{caption !== undefined && caption !== null && <figcaption className="pk-matrix-caption">{caption}</figcaption>}
<table className="pk-matrix-grid" aria-label={label}>
<thead>
<tr>
<th scope="col" className="pk-matrix-corner pk-type-label">
{columns ? (
<>
{rows.name} × {columns.name}
</>
) : (
rows.name
)}
</th>
{columns?.values.map(value => (
<th key={value.key} scope="col">
<Heading value={value} code={columns.code ?? false} />
</th>
))}
</tr>
</thead>
<tbody>
{rows.values.map(row => (
<tr key={row.key}>
<th scope="row">
<Heading value={row} code={rows.code ?? false} />
</th>
{columns ? columns.values.map(column => body(row, column)) : body(row, undefined)}
</tr>
))}
</tbody>
</table>
</figure>
)
}
Loading
Loading