diff --git a/changelog.d/gha-single-axis.md b/changelog.d/gha-single-axis.md new file mode 100644 index 000000000..b3e11328d --- /dev/null +++ b/changelog.d/gha-single-axis.md @@ -0,0 +1,5 @@ +--- +section: renderers +--- + +- **Single-dimension GitHub Actions matrices render horizontally**. A matrix that varies one dimension (such as `node: [22, 24]`) now places its values across one row with each run's mark beneath them instead of one row per value, restoring the layout from before the renderer moved to `MatrixGrid`. This keeps short workflows completely visible in short stages. `MatrixGrid` adds an `across` prop for this layout, and the harness now checks that compact scenarios open in a 1056 × 520 stage without scrolling. diff --git a/docs/guides/renderer-kit-type.md b/docs/guides/renderer-kit-type.md index 15fd77fda..633cf2ddb 100644 --- a/docs/guides/renderer-kit-type.md +++ b/docs/guides/renderer-kit-type.md @@ -68,4 +68,6 @@ 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. +extra dimension. With one axis (no `columns`), cells run down the side by +default. Pass `across` to place them in one row under their values instead, +keeping a few short values (such as versions) to two rows. diff --git a/docs/reference/renderer-kit.md b/docs/reference/renderer-kit.md index 602263f63..28d441869 100644 --- a/docs/reference/renderer-kit.md +++ b/docs/reference/renderer-kit.md @@ -4584,7 +4584,7 @@ What one cell holds: whether something is there, its change, and anything its he ### `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 +MatrixGrid(__0: { "across"?: false | true; "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 @@ -4592,7 +4592,8 @@ edge, and each cell what `cell(row, column)` returns: by default a small mark th 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). +cells along `rows`: down the side, or with `across`, in one row under the values. `caption` names the grid when +several stand together (one per further dimension). ### `MatrixValue` diff --git a/docs/reference/renderers.md b/docs/reference/renderers.md index a18ebdba6..4b576f019 100644 --- a/docs/reference/renderers.md +++ b/docs/reference/renderers.md @@ -90,7 +90,7 @@ Built-in Projections GitHub Actions renderer: a workflow's triggers, its jobs an - Handles: `github-actions` - Comparison modes: `diff`, `side-by-side` - Package: `@githubnext/projections-renderer-github-actions` -- Example scenarios ([`scenarios.json`](../../packages/renderers/github-actions/scenarios.json)): Narrow a build matrix to one runtime; Sign a release before publishing it; A build and test workflow +- Example scenarios ([`scenarios.json`](../../packages/renderers/github-actions/scenarios.json)): Narrow a build matrix to one runtime; Sign a release before publishing it; Test on newer Node.js and deploy the worker on its own; A build and test workflow ## `graph` diff --git a/packages/extractors/github-actions-jobs/README.md b/packages/extractors/github-actions-jobs/README.md index ee1a0ea1a..5bad7dd2e 100644 --- a/packages/extractors/github-actions-jobs/README.md +++ b/packages/extractors/github-actions-jobs/README.md @@ -60,11 +60,12 @@ missing or empty `jobs` mapping, `needs` references to an undeclared job or an expression, and `needs` cycles, reported as a path such as `build → test → build`. -`tests/fixtures` contains two before-and-after pairs with golden extractions -(rewritten with `PROJECTIONS_UPDATE_GOLDEN=1`): a build matrix narrowed to one -runtime and a release pipeline that gains a signing job. The -[`github-actions` renderer](../../renderers/github-actions/README.md) uses these -same fixtures for its scenarios and tests. +`tests/fixtures` contains three before-and-after pairs with golden extractions +(update with `PROJECTIONS_UPDATE_GOLDEN=1`): a build matrix narrowed to one +runtime, a release pipeline that adds a signing job, and a delivery workflow +whose single-dimension test matrix updates to newer Node.js versions. The +[`github-actions` renderer](../../renderers/github-actions/README.md) reuses +these fixtures for its scenarios and tests. If you need an evaluated pipeline (for another CI system or for what actually ran), write a custom extractor that emits `dag/v0` jobs and `precedes` diff --git a/packages/extractors/github-actions-jobs/tests/extractions.rs b/packages/extractors/github-actions-jobs/tests/extractions.rs index ace5c2e98..2c57771a8 100644 --- a/packages/extractors/github-actions-jobs/tests/extractions.rs +++ b/packages/extractors/github-actions-jobs/tests/extractions.rs @@ -107,3 +107,20 @@ fn a_pipeline_keeps_its_needs_reusable_calls_and_dynamic_matrices() { json!({"event": "workflow_dispatch", "filters": [{"key": "inputs", "values": ["dry-run"]}]}) ); } + +#[test] +fn a_matrix_over_one_dimension_moves_to_newer_versions() { + let before = case("delivery", "before"); + let after = case("delivery", "after"); + let versions = |extraction: &Value| { + entity(extraction, "job:test")["data"]["matrix"]["runs"] + .as_array() + .unwrap() + .iter() + .map(|run| run["values"]["node"].clone()) + .collect::>() + }; + assert_eq!(versions(&before), [json!(20), json!(22)]); + assert_eq!(versions(&after), [json!(22), json!(24)]); + assert!(edges(&after, "precedes").contains(&("job:test".into(), "job:deploy-worker".into()))); +} diff --git a/packages/extractors/github-actions-jobs/tests/fixtures/delivery/after.expected.json b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/after.expected.json new file mode 100644 index 000000000..5658bf2b4 --- /dev/null +++ b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/after.expected.json @@ -0,0 +1,194 @@ +{ + "entities": [ + { + "confidence": "authoritative", + "data": { + "events": [ + { + "event": "push", + "filters": [ + { + "key": "branches", + "values": [ + "main" + ] + } + ] + } + ], + "workflow": "Delivery" + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "trigger:on", + "kind": "trigger", + "label": "push" + }, + { + "confidence": "authoritative", + "data": { + "runsOn": [ + "ubuntu-latest" + ], + "steps": { + "actions": [ + { + "action": "actions/checkout", + "count": 1, + "ref": "v4" + } + ], + "run": 1, + "total": 2 + } + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "job:deploy", + "kind": "job", + "label": "deploy" + }, + { + "confidence": "authoritative", + "data": { + "runsOn": [ + "ubuntu-latest" + ], + "steps": { + "actions": [ + { + "action": "actions/checkout", + "count": 1, + "ref": "v4" + } + ], + "run": 1, + "total": 2 + } + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "job:deploy-worker", + "kind": "job", + "label": "deploy-worker" + }, + { + "confidence": "authoritative", + "data": { + "matrix": { + "dimensions": [ + { + "key": "node", + "values": [ + 22, + 24 + ] + } + ], + "runs": [ + { + "values": { + "node": 22 + } + }, + { + "values": { + "node": 24 + } + } + ] + }, + "runsOn": [ + "ubuntu-latest" + ], + "steps": { + "actions": [ + { + "action": "actions/checkout", + "count": 1, + "ref": "v4" + }, + { + "action": "actions/setup-node", + "count": 1, + "ref": "v4" + } + ], + "run": 1, + "total": 3 + } + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "job:test", + "kind": "job", + "label": "test" + } + ], + "metadata": { + "limitations": [ + "Declaration graph only: a matrix expands and its values fill names and runners, but other expressions, reusable workflows' own jobs, conditions and runtime scheduling are not evaluated." + ], + "source": "GitHub Actions workflow declaration", + "workflow": ".github/workflows/delivery.yml" + }, + "protocolVersion": 0, + "relationships": [ + { + "confidence": "authoritative", + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "from": "job:test", + "id": "precedes:job%3Atest:job%3Adeploy", + "kind": "precedes", + "to": "job:deploy" + }, + { + "confidence": "authoritative", + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "from": "job:test", + "id": "precedes:job%3Atest:job%3Adeploy-worker", + "kind": "precedes", + "to": "job:deploy-worker" + }, + { + "confidence": "authoritative", + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "from": "trigger:on", + "id": "triggers:trigger%3Aon:job%3Atest", + "kind": "triggers", + "to": "job:test" + } + ], + "schema": "dag/v0" +} diff --git a/packages/extractors/github-actions-jobs/tests/fixtures/delivery/after/.github/workflows/delivery.yml b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/after/.github/workflows/delivery.yml new file mode 100644 index 000000000..a3c593850 --- /dev/null +++ b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/after/.github/workflows/delivery.yml @@ -0,0 +1,35 @@ +name: Delivery +on: + push: + branches: [main] + +jobs: + # The worker runs its TypeScript as is, with Node's type stripping, which needs Node 22 or later. + test: + runs-on: ubuntu-latest + strategy: + matrix: + node: [22, 24] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + cache: npm + - run: npm ci && npm run lint && npm test + + # deploy.sh builds each app's image from the repository root, so it gets the workspace packages it imports. + deploy: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: ./deploy.sh api storefront + + # The worker charges orders, so it ships on its own and can be rolled back without the storefront. + deploy-worker: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: ./deploy.sh worker diff --git a/packages/extractors/github-actions-jobs/tests/fixtures/delivery/before.expected.json b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/before.expected.json new file mode 100644 index 000000000..41def8042 --- /dev/null +++ b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/before.expected.json @@ -0,0 +1,153 @@ +{ + "entities": [ + { + "confidence": "authoritative", + "data": { + "events": [ + { + "event": "push", + "filters": [ + { + "key": "branches", + "values": [ + "main" + ] + } + ] + } + ], + "workflow": "Delivery" + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "trigger:on", + "kind": "trigger", + "label": "push" + }, + { + "confidence": "authoritative", + "data": { + "runsOn": [ + "ubuntu-latest" + ], + "steps": { + "actions": [ + { + "action": "actions/checkout", + "count": 1, + "ref": "v4" + } + ], + "run": 1, + "total": 2 + } + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "job:deploy", + "kind": "job", + "label": "deploy" + }, + { + "confidence": "authoritative", + "data": { + "matrix": { + "dimensions": [ + { + "key": "node", + "values": [ + 20, + 22 + ] + } + ], + "runs": [ + { + "values": { + "node": 20 + } + }, + { + "values": { + "node": 22 + } + } + ] + }, + "runsOn": [ + "ubuntu-latest" + ], + "steps": { + "actions": [ + { + "action": "actions/checkout", + "count": 1, + "ref": "v4" + }, + { + "action": "actions/setup-node", + "count": 1, + "ref": "v4" + } + ], + "run": 1, + "total": 3 + } + }, + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "id": "job:test", + "kind": "job", + "label": "test" + } + ], + "metadata": { + "limitations": [ + "Declaration graph only: a matrix expands and its values fill names and runners, but other expressions, reusable workflows' own jobs, conditions and runtime scheduling are not evaluated." + ], + "source": "GitHub Actions workflow declaration", + "workflow": ".github/workflows/delivery.yml" + }, + "protocolVersion": 0, + "relationships": [ + { + "confidence": "authoritative", + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "from": "job:test", + "id": "precedes:job%3Atest:job%3Adeploy", + "kind": "precedes", + "to": "job:deploy" + }, + { + "confidence": "authoritative", + "evidence": [ + { + "kind": "github-actions-workflow", + "source": ".github/workflows/delivery.yml" + } + ], + "from": "trigger:on", + "id": "triggers:trigger%3Aon:job%3Atest", + "kind": "triggers", + "to": "job:test" + } + ], + "schema": "dag/v0" +} diff --git a/packages/extractors/github-actions-jobs/tests/fixtures/delivery/before/.github/workflows/delivery.yml b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/before/.github/workflows/delivery.yml new file mode 100644 index 000000000..1b6f350f7 --- /dev/null +++ b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/before/.github/workflows/delivery.yml @@ -0,0 +1,26 @@ +name: Delivery +on: + push: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + node: [20, 22] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + cache: npm + - run: npm ci && npm run lint && npm test + + # deploy.sh builds each app's image from the repository root, so it gets the workspace packages it imports. + deploy: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: ./deploy.sh api storefront diff --git a/packages/extractors/github-actions-jobs/tests/fixtures/delivery/inputs.json b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/inputs.json new file mode 100644 index 000000000..bcd678845 --- /dev/null +++ b/packages/extractors/github-actions-jobs/tests/fixtures/delivery/inputs.json @@ -0,0 +1 @@ +{"workflowPath": ".github/workflows/delivery.yml"} diff --git a/packages/renderers/github-actions/README.md b/packages/renderers/github-actions/README.md index 6d984c99b..e63dffff7 100644 --- a/packages/renderers/github-actions/README.md +++ b/packages/renderers/github-actions/README.md @@ -31,11 +31,13 @@ change regions in its layered graph layout. - **Matrix grid.** Varying dimensions form the grid axes. The runner's dimension runs down the rows, or the dimension with the most values otherwise; the next runs across the columns, and any additional dimensions produce one grid per - combination of their values. Dimension names appear as words and their values - in monospace. Each cell represents one run, showing only a small mark or the - values its row and column don't already name, with its evaluated name on - hover. Combinations removed by `exclude` appear as hatched gaps, and - standalone runs added by `include` are listed after the grid. + combination of their values. If there is only one dimension and it isn't the + runner, its values run across the top with one row of runs underneath + (`node 22 24`). Dimension names appear as words and their values in monospace. + Each cell represents one run, showing only a small mark or the values its row + and column don't already name, with its evaluated name on hover. Combinations + removed by `exclude` appear as hatched gaps, and standalone runs added by + `include` are listed after the grid. - **Runtime expressions.** Expressions that can only be evaluated at run time stay as written in muted monospace. A matrix built with `fromJSON`, a `needs` output, or an `if:` condition is shown verbatim rather than guessed. @@ -63,6 +65,8 @@ npm run tools -- test packages/renderers/github-actions npm run tools -- test packages/renderers/github-actions --browser chromium-desktop # layout and diff in the real frame ``` -[`scenarios.json`](scenarios.json) defines the gallery's scenarios from the +[`scenarios.json`](scenarios.json) defines four gallery scenarios from the extractor's fixtures: a build matrix narrowed to one runtime, a release pipeline -that gains a signing job, and a single revision of a build and test workflow. +that adds a signing job, a short delivery workflow that updates its Node.js test +versions and adds a deploy job, and a single revision of a build-and-test +workflow. diff --git a/packages/renderers/github-actions/scenarios.json b/packages/renderers/github-actions/scenarios.json index 5c9a47ba3..7ba975e74 100644 --- a/packages/renderers/github-actions/scenarios.json +++ b/packages/renderers/github-actions/scenarios.json @@ -18,6 +18,13 @@ "before": "../../extractors/github-actions-jobs/tests/fixtures/pipeline/before.expected.json", "after": "../../extractors/github-actions-jobs/tests/fixtures/pipeline/after.expected.json" }, + { + "id": "delivery", + "title": "Test on newer Node.js and deploy the worker on its own", + "description": "A short delivery workflow whose test job runs on two Node.js versions moves from 20 and 22 to 22 and 24, and gains a deploy job for the worker beside the existing one. Compact enough to open whole in a short, wide stage, with nothing to scroll.", + "before": "../../extractors/github-actions-jobs/tests/fixtures/delivery/before.expected.json", + "after": "../../extractors/github-actions-jobs/tests/fixtures/delivery/after.expected.json" + }, { "id": "build", "title": "A build and test workflow", diff --git a/packages/renderers/github-actions/src/matrix.tsx b/packages/renderers/github-actions/src/matrix.tsx index 7ae461567..27ec71356 100644 --- a/packages/renderers/github-actions/src/matrix.tsx +++ b/packages/renderers/github-actions/src/matrix.tsx @@ -74,7 +74,8 @@ function Grid({ strength: number job: string }) { - // One dimension across lays its values down the side instead. + // A lone dimension lies as the axes put it: across, in one row under its values, unless it's the runner, whose long + // labels go down the side. const side = rows ?? columns if (!side) return null const across = rows ? columns : undefined @@ -106,6 +107,7 @@ function Grid({ label={`${job} runs`} rows={axisOf(side, frame, strength)} {...(across ? {columns: axisOf(across, frame, strength)} : {})} + across={!rows} cell={(row, column) => cellOf(find(row, column), frame, strength)} vacant="No run: this combination is excluded" {...(caption ? {caption} : {})} diff --git a/packages/renderers/harness/README.md b/packages/renderers/harness/README.md index 7017f1a8b..bc2e7fb22 100644 --- a/packages/renderers/harness/README.md +++ b/packages/renderers/harness/README.md @@ -89,6 +89,8 @@ Inspector on an OS-assigned loopback port. The harness's own suites check that every scenario opens in the gallery without renderer errors (`browser/gallery.spec.ts`), that every scenario passes the built-in checks (`browser/checks.spec.ts`, Chromium desktop; known renderer issues are listed -there with their cause and must keep failing until fixed), and that hover and +there with their cause and must keep failing until fixed), that hover and selection stay inside their unit for every scenario -(`browser/affordances.spec.ts`). +(`browser/affordances.spec.ts`), and that compact scenarios open in full without +scrolling in a 1056 × 520 stage, the size of a carousel slide +(`browser/short-stage.spec.ts`; add a scenario there when it should fit). diff --git a/packages/renderers/harness/browser/short-stage.spec.ts b/packages/renderers/harness/browser/short-stage.spec.ts new file mode 100644 index 000000000..e3e6d6f00 --- /dev/null +++ b/packages/renderers/harness/browser/short-stage.spec.ts @@ -0,0 +1,45 @@ +import {expect, test} from '@playwright/test' +import {openStage} from '@githubnext/projections-renderer-testing/playwright' +import {harnessInspector} from '@githubnext/projections-renderer-harness/playwright' + +/** + * Compact examples open whole in a short, wide stage, as a slide in a carousel of examples shows them: 1056 by 520 + * (a 520 px stage is the shortest such a host draws a live slide in; it scales a shorter one down as a picture). A + * slide can't be scrolled, so nothing in the renderer's document may scroll vertically there, in the diff or at either + * end (Before and After). `opens-fitted` lets a large view open at the text floor and scroll; these scenarios promise more, so a card + * that grows (a matrix laid out down the side instead of across, say) fails here rather than on a host's homepage. + */ +const cases = ['github-actions/delivery', 'github-actions/build', 'dependency-injection/orders'] + +const stage = {width: 1056, height: 520} + +for (const key of cases) { + test(`${key}: opens whole in a short, wide stage, with nothing to scroll`, async ({page}, info) => { + test.skip(info.project.name !== 'chromium-desktop', 'The stage is a fixed size; one engine measures it.') + // The diff, then each end as a progress presentation draws it (Before at 0, After at 1); a diff never seeks. + const presentations = [ + {name: 'the diff', comparisonMode: 'diff', progress: 1}, + {name: 'Before', comparisonMode: 'animated', progress: 0}, + {name: 'After', comparisonMode: 'animated', progress: 1}, + ] as const + for (const {name, comparisonMode, progress} of presentations) { + const opened = await openStage(page, harnessInspector(), {scenario: key, ...stage, progress, comparisonMode}) + const scrolling = () => + opened.frame.locator('body').evaluate(() => + [...new Set([document.scrollingElement, ...document.querySelectorAll('*')])] + .filter((element): element is Element => { + if (!element || element.scrollHeight <= element.clientHeight + 1) return false + if (element === document.scrollingElement) return true + return ['auto', 'scroll'].includes(getComputedStyle(element).overflowY) + }) + .map( + element => + `${element.tagName.toLowerCase()}.${[...element.classList].join('.')} ${element.scrollHeight}>${element.clientHeight}`, + ), + ) + // A renderer may draw before its stage settles to the frame's height, so this waits for the fit. + await expect.poll(scrolling, {message: `${key}, ${name}, scrolls vertically`, timeout: 3000}).toEqual([]) + await opened.iframe.screenshot({path: info.outputPath(`${key.replace('/', '--')}-${name.replace(' ', '-')}.png`)}) + } + }) +} diff --git a/packages/renderers/kit/src/react/matrix-grid.tsx b/packages/renderers/kit/src/react/matrix-grid.tsx index 98e6b4494..8c767d7a8 100644 --- a/packages/renderers/kit/src/react/matrix-grid.tsx +++ b/packages/renderers/kit/src/react/matrix-grid.tsx @@ -65,7 +65,8 @@ function Heading({value, code}: {value: MatrixValue; code: boolean}) { * 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). + * cells along `rows`: down the side, or with `across`, in one row under the values. `caption` names the grid when + * several stand together (one per further dimension). */ export function MatrixGrid({ label, @@ -75,6 +76,7 @@ export function MatrixGrid({ caption, vacant = 'None', present = 'Present', + across = false, emphasis, }: { /** The grid's accessible name. */ @@ -87,6 +89,12 @@ export function MatrixGrid({ vacant?: string /** What assistive technology hears in a marked cell without a `title`. */ present?: string + /** + * With one axis (no `columns`), lays its values across in one row, its name before them and each cell under its + * value, rather than down the side: two rows however many values, for a few short ones (versions). Long values + * (runner labels) read better down the side. + */ + across?: boolean emphasis?: number }) { const style = emphasis === undefined ? undefined : ({'--emphasis': emphasis} as Record) @@ -94,7 +102,7 @@ export function MatrixGrid({ const found = cell(row.key, column?.key) if (!found) { return ( - + {vacant} @@ -104,7 +112,7 @@ export function MatrixGrid({ const changed = said(found.change) const content = shows(found.content) return ( - + ) } + const line = across && !columns return (
{caption !== undefined && caption !== null &&
{caption}
} @@ -135,22 +144,32 @@ export function MatrixGrid({ rows.name )} - {columns?.values.map(value => ( + {(line ? rows : columns)?.values.map(value => ( - + ))} - {rows.values.map(row => ( - + {line && ( + + {/* The row's heading is the grid itself: its name, heard but not shown, as the axis's is above it. */} - + {label} - {columns ? columns.values.map(column => body(row, column)) : body(row, undefined)} + {rows.values.map(value => body(value, undefined))} - ))} + )} + {!line && + rows.values.map(row => ( + + + + + {columns ? columns.values.map(column => body(row, column)) : body(row, undefined)} + + ))}
diff --git a/packages/renderers/kit/test/matrix-grid.test.mjs b/packages/renderers/kit/test/matrix-grid.test.mjs index 005f5d728..9f095f8ea 100644 --- a/packages/renderers/kit/test/matrix-grid.test.mjs +++ b/packages/renderers/kit/test/matrix-grid.test.mjs @@ -61,3 +61,22 @@ test('a cell with nothing to show is marked and said present, and a zero is cont /0<\/span><\/span><\/span>/u, ) }) + +test('a single axis laid across is one row of cells under its values, its name before them', () => { + const markup = renderToStaticMarkup( + createElement(MatrixGrid, { + label: 'test runs', + across: true, + rows: {name: 'node', code: true, values: [{key: '22'}, {key: '24', change: 'added'}]}, + cell: row => ({title: `node ${row}`, ...(row === '24' ? {change: 'added'} : {})}), + }), + ) + const text = plain(markup) + assert.equal(markup.match(//gu)?.length, 2) + assert.match(markup, /node<\/th>/u) + assert.match( + markup, + /test runs<\/span><\/th>/u, + ) + assert.match(text, /Added: node 24/u) +})