From 707933d5d7debb2e308af41f9ea431ac80a0b937 Mon Sep 17 00:00:00 2001 From: Ernest Tien Date: Mon, 23 Nov 2026 15:16:34 -0800 Subject: [PATCH 1/5] feat(core): DiffViewer moves to Core DiffViewer, parseUnifiedDiff, createDiffFile, and the diff model types move from Lab to Core, imported from `@astryxdesign/core/DiffViewer` or the package root. The component keeps its props, defaults, and behavior. Core's stricter rules bring two small visible changes: a `headerEnd` that renders nothing (`false`, `true`, or an empty string) no longer leaves an empty wrapper in the header, and the binary and no-change message takes the supporting text role's leading. The Lab exports become deprecated re-exports of the Core ones for one release. The Storybook stories move to Core/DiffViewer, the showcase block moves to the CLI's component blocks beside a new "Viewed files" example, and the ShadCN registry keeps the Lab route as an alias. The new `component:DiffViewer` record is a draft for an engineering owner's approval. --- .changeset/diffviewer-core-blocks.md | 7 + .changeset/promote-diffviewer-to-core.md | 7 + apps/sandbox/scripts/next-export-routes.json | 2 + .../pages/diffviewer-perf/benchmark.test.ts | 2 +- .../pages/diffviewer-perf/page.tsx | 2 +- apps/storybook/rtl-audit/targets.json | 2 +- apps/storybook/stories/DiffViewer.stories.tsx | 4 +- .../components/CodeBlock/CodeBlock.spec.md | 4 +- .../components/DiffViewer/DiffViewer.spec.md | 306 ++++++++++++++++++ .../no-comment-provenance.baseline.json | 1 - internal/shadcn-registry/routes.lock.json | 17 +- .../DiffViewer}/DiffViewerShowcase.doc.mjs | 2 +- .../DiffViewer}/DiffViewerShowcase.tsx | 4 +- .../DiffViewer/DiffViewerViewedFiles.doc.mjs | 14 + .../DiffViewer/DiffViewerViewedFiles.tsx | 74 +++++ .../__golden__/no-icon-set/search-close.json | 2 +- .../__golden__/no-icon-set/search-close.txt | 2 +- packages/cli/test/swizzle-proof/samples.mjs | 2 +- packages/core/locales/en.json | 44 +-- packages/core/package.json | 5 + .../src/DiffViewer/DiffViewer.doc.mjs | 130 +++++++- .../src/DiffViewer/DiffViewer.test.tsx | 13 +- .../src/DiffViewer/DiffViewer.tsx | 113 ++++--- .../DiffViewer.a11y.chromium.spec.ts | 8 +- ...ffViewerForcedColors.a11y.chromium.spec.ts | 4 +- .../src/DiffViewer/createDiffFile.test.ts | 0 .../src/DiffViewer/createDiffFile.ts | 6 +- .../{lab => core}/src/DiffViewer/diffTypes.ts | 6 +- packages/core/src/DiffViewer/index.ts | 32 ++ .../src/DiffViewer/inlineDiff.test.ts | 0 .../src/DiffViewer/inlineDiff.ts | 8 +- .../src/DiffViewer/parseUnifiedDiff.test.ts | 0 .../src/DiffViewer/parseUnifiedDiff.ts | 6 +- .../src/__tests__/hostDataAttributes.test.tsx | 10 + packages/core/src/index.ts | 1 + packages/lab/src/DiffViewer/index.test.ts | 57 ++++ packages/lab/src/DiffViewer/index.ts | 142 ++++++-- packages/themes/probe/src/probeTheme.ts | 36 +++ 38 files changed, 926 insertions(+), 149 deletions(-) create mode 100644 .changeset/diffviewer-core-blocks.md create mode 100644 .changeset/promote-diffviewer-to-core.md create mode 100644 docs/specs/components/DiffViewer/DiffViewer.spec.md rename packages/{lab/blocks => cli/assets/templates/blocks/components/DiffViewer}/DiffViewerShowcase.doc.mjs (96%) rename packages/{lab/blocks => cli/assets/templates/blocks/components/DiffViewer}/DiffViewerShowcase.tsx (95%) create mode 100644 packages/cli/assets/templates/blocks/components/DiffViewer/DiffViewerViewedFiles.doc.mjs create mode 100644 packages/cli/assets/templates/blocks/components/DiffViewer/DiffViewerViewedFiles.tsx rename packages/{lab => core}/src/DiffViewer/DiffViewer.doc.mjs (62%) rename packages/{lab => core}/src/DiffViewer/DiffViewer.test.tsx (98%) rename packages/{lab => core}/src/DiffViewer/DiffViewer.tsx (95%) rename packages/{lab => core}/src/DiffViewer/__tests__/DiffViewer.a11y.chromium.spec.ts (95%) rename packages/{lab => core}/src/DiffViewer/__tests__/DiffViewerForcedColors.a11y.chromium.spec.ts (97%) rename packages/{lab => core}/src/DiffViewer/createDiffFile.test.ts (100%) rename packages/{lab => core}/src/DiffViewer/createDiffFile.ts (98%) rename packages/{lab => core}/src/DiffViewer/diffTypes.ts (92%) create mode 100644 packages/core/src/DiffViewer/index.ts rename packages/{lab => core}/src/DiffViewer/inlineDiff.test.ts (100%) rename packages/{lab => core}/src/DiffViewer/inlineDiff.ts (95%) rename packages/{lab => core}/src/DiffViewer/parseUnifiedDiff.test.ts (100%) rename packages/{lab => core}/src/DiffViewer/parseUnifiedDiff.ts (98%) create mode 100644 packages/lab/src/DiffViewer/index.test.ts diff --git a/.changeset/diffviewer-core-blocks.md b/.changeset/diffviewer-core-blocks.md new file mode 100644 index 0000000000000..2c6745a4e34f3 --- /dev/null +++ b/.changeset/diffviewer-core-blocks.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/cli': patch +--- + +[docs] `astryx template` lists DiffViewer's blocks among Core's component blocks: the "Unified diff from a patch" showcase, and a new "DiffViewer — Viewed files" example, the file list of a review page, where a "Viewed" checkbox folds each file through `collapsible`. + +@ernestt diff --git a/.changeset/promote-diffviewer-to-core.md b/.changeset/promote-diffviewer-to-core.md new file mode 100644 index 0000000000000..45e3b1cf03b0e --- /dev/null +++ b/.changeset/promote-diffviewer-to-core.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[component] Promote `DiffViewer` to Core, imported from `@astryxdesign/core/DiffViewer` or the package root together with `parseUnifiedDiff`, `createDiffFile`, and the diff model types. DiffViewer shows a read-only, syntax-highlighted diff of one file in unified or split view: a file header with the path, status, and line counts over the hunks, review annotation rows under any line, optional line-number buttons, and folds for a change too large to show line by line. Every changed line reads as added or removed without colour, also under forced colors. The file header is a disclosure that starts open; `collapsible` takes the same `CollapsibleConfig` as Banner and Collapsible (`{defaultIsOpen: false}` starts it collapsed, `{isOpen, onOpenChange}` controls it, and `false` turns it off). + +@ernestt diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json index 8d16421526162..5016ad8d1695a 100644 --- a/apps/sandbox/scripts/next-export-routes.json +++ b/apps/sandbox/scripts/next-export-routes.json @@ -306,6 +306,8 @@ "/templates/DialogScrollingContent/", "/templates/DialogShowcase/", "/templates/DialogWithSubtitle/", + "/templates/DiffViewerShowcase/", + "/templates/DiffViewerViewedFiles/", "/templates/DividerFullBleed/", "/templates/DividerShowcase/", "/templates/DividerVariants/", diff --git a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.test.ts b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.test.ts index bee258494c9fc..a8ea264769ac3 100644 --- a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.test.ts +++ b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/benchmark.test.ts @@ -1,7 +1,7 @@ // Copyright (c) Meta Platforms, Inc. and affiliates. import {describe, expect, it} from 'vitest'; -import {createDiffFile} from '../../../../../../../packages/lab/src/DiffViewer/createDiffFile'; +import {createDiffFile} from '../../../../../../../packages/core/src/DiffViewer/createDiffFile'; import { countLines, createMultiFileWorkload, diff --git a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx index a3e88dee76785..abdab05d9e67b 100644 --- a/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx +++ b/apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx @@ -44,7 +44,7 @@ import { DiffViewer, type DiffViewerFile, type DiffViewerViewMode, -} from '@astryxdesign/lab'; +} from '@astryxdesign/core/DiffViewer'; import { createMultiFileWorkload, createSingleFileWorkload, diff --git a/apps/storybook/rtl-audit/targets.json b/apps/storybook/rtl-audit/targets.json index b1a8e46ac7495..8f06ae426b9a9 100644 --- a/apps/storybook/rtl-audit/targets.json +++ b/apps/storybook/rtl-audit/targets.json @@ -338,7 +338,7 @@ }, { "component": "DiffViewer", - "storyId": "lab-diffviewer--character-highlight", + "storyId": "core-diffviewer--character-highlight", "dims": ["D2"], "selectors": { "prev": ".astryx-diff-viewer-path", diff --git a/apps/storybook/stories/DiffViewer.stories.tsx b/apps/storybook/stories/DiffViewer.stories.tsx index a3da690e483c9..ec223d70e9bd1 100644 --- a/apps/storybook/stories/DiffViewer.stories.tsx +++ b/apps/storybook/stories/DiffViewer.stories.tsx @@ -7,7 +7,7 @@ import { createDiffFile, parseUnifiedDiff, type DiffViewerLineRef, -} from '@astryxdesign/lab'; +} from '@astryxdesign/core/DiffViewer'; import {Button} from '@astryxdesign/core/Button'; import {Card} from '@astryxdesign/core/Card'; import {CheckboxInput} from '@astryxdesign/core/CheckboxInput'; @@ -76,7 +76,7 @@ const FILES = parseUnifiedDiff(PATCH); const [TOTAL_FILE] = FILES; const meta: Meta = { - title: 'Lab/DiffViewer', + title: 'Core/DiffViewer', component: DiffViewer, tags: ['autodocs'], parameters: { diff --git a/docs/specs/components/CodeBlock/CodeBlock.spec.md b/docs/specs/components/CodeBlock/CodeBlock.spec.md index 8482c53a8874f..0200e11702afb 100644 --- a/docs/specs/components/CodeBlock/CodeBlock.spec.md +++ b/docs/specs/components/CodeBlock/CodeBlock.spec.md @@ -80,7 +80,7 @@ Consumer migration instructions belong in consumer docs and release notes. - Read-only multi-line code presentation, built-in and custom token rendering, line numbers and emphasis, header/title/language presentation, copy behavior and feedback, optional wrapping and bounded scrolling, optional collapse behavior, syntax-theme composition, root prop/ref composition, and the documented CodeBlock theme targets. -- The language value: what a `CodeLanguage` carries, the per-language modules under `@astryxdesign/core/CodeBlock/languages/`, `createCodeLanguage`, and how each Astryx surface that colours code with CodeBlock's tokenizer accepts one, including Lab `DiffViewer`'s `language` and `codeLanguages` (FR13). +- The language value: what a `CodeLanguage` carries, the per-language modules under `@astryxdesign/core/CodeBlock/languages/`, `createCodeLanguage`, and how each Astryx surface that colours code with CodeBlock's tokenizer accepts one, including `DiffViewer`'s `language` and `codeLanguages` (FR13). **Does not own / non-goals** @@ -204,7 +204,7 @@ Exact CodeBlock proportions and syntax colors are not contract requirements; the | Contract | Verification | Representative states | Mutation or failure expectation | Audit section | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------- | | FR1, PR1–PR3 | `tokenizer.test.ts`; `highlightRanges.test.ts`; CodeBlock focused tests and built Storybook; `CodeBlockSyntaxLayer.a11y.chromium.spec.ts` (a syntax colour set in the `astryx-theme` layer or by unlayered app CSS wins over the injected highlight rules) | short/long, built-in/custom, ranges/spans, empty lines | Text changes, stale tokens paint, ranges leak, source order changes, or long work stops yielding/canceling. | `audit:CodeBlock/behavior` | -| FR12, FR13, PR4 | `tokenizer.test.ts` (one representative snippet per built-in family and alias, and per module: the comment, string, number, and keyword tokens each language has; an unknown identifier yields no tokens, and a module's identifier passed as a string yields none); CodeBlock, Markdown, RichText, and Lab `DiffViewer` tests (a module passed as `language`, or listed in `codeLanguages`, paints and labels; `DiffViewer` infers a listed module from a file's extension or name; the same identifier or file without it stays plain; a custom language from `createCodeLanguage` paints, labels, and infers like a module; a listed value beats a built-in with the same identifier; the deprecated `tokenizer` still wins over a built-in identifier and a language value and still works as released; a language value reflects its first identifier as `data-language`; the codemod rewrites static non-`plaintext` uses, keeps their `data-language` and label, and reports the rest; server rendering paints in the first render); a size check over the minified, gzipped tokenizer and each module, and a bundle check that an unimported module adds nothing | every FR12 identifier and alias, every FR13 module passed as `language` and in `codeLanguages`, a diffed file named by a module's extension or file name, a module identifier as a plain string, a custom language passed and listed, a custom language reusing a built-in identifier, the deprecated `tokenizer` alone, with a built-in identifier, and with a language value, an unknown identifier, an empty string | A listed identifier renders without tokens, an alias paints differently from its family, a string resolves to a module, a module fails to paint or label on any of the four surfaces, a diffed file infers a module that was not listed, a custom language behaves differently from a module, a listed value loses to a built-in, the deprecated `tokenizer` stops working or loses to a language value, a language value reflects anything but its first identifier, the codemod rewrites a use it cannot prove or drops one silently, source text changes, the tokenizer exceeds 4,608 bytes, a module adds more than 512 bytes, or an unimported module reaches the bundle. | `audit:CodeBlock/behavior` | +| FR12, FR13, PR4 | `tokenizer.test.ts` (one representative snippet per built-in family and alias, and per module: the comment, string, number, and keyword tokens each language has; an unknown identifier yields no tokens, and a module's identifier passed as a string yields none); CodeBlock, Markdown, RichText, and the `DiffViewer` tests (a module passed as `language`, or listed in `codeLanguages`, paints and labels; `DiffViewer` infers a listed module from a file's extension or name; the same identifier or file without it stays plain; a custom language from `createCodeLanguage` paints, labels, and infers like a module; a listed value beats a built-in with the same identifier; the deprecated `tokenizer` still wins over a built-in identifier and a language value and still works as released; a language value reflects its first identifier as `data-language`; the codemod rewrites static non-`plaintext` uses, keeps their `data-language` and label, and reports the rest; server rendering paints in the first render); a size check over the minified, gzipped tokenizer and each module, and a bundle check that an unimported module adds nothing | every FR12 identifier and alias, every FR13 module passed as `language` and in `codeLanguages`, a diffed file named by a module's extension or file name, a module identifier as a plain string, a custom language passed and listed, a custom language reusing a built-in identifier, the deprecated `tokenizer` alone, with a built-in identifier, and with a language value, an unknown identifier, an empty string | A listed identifier renders without tokens, an alias paints differently from its family, a string resolves to a module, a module fails to paint or label on any of the four surfaces, a diffed file infers a module that was not listed, a custom language behaves differently from a module, a listed value loses to a built-in, the deprecated `tokenizer` stops working or loses to a language value, a language value reflects anything but its first identifier, the codemod rewrites a use it cannot prove or drops one silently, source text changes, the tokenizer exceeds 4,608 bytes, a module adds more than 512 bytes, or an unimported module reaches the bundle. | `audit:CodeBlock/behavior` | | FR2, AR1, AR3 | `CodeBlock.test.tsx`; component-scoped accessibility audit; the failed-write suite (a rejected `writeText`: cross icon, failure name, tooltip and announcement, `onCopyError` once with the rejection and no `onCopy`, the same 2 s reset, a later success clears it) | copy success, rapid repeat, localized feedback, empty title | Exact source is not copied, callback fires on failure, announcement is absent/duplicated, or a control is unnamed. | `audit:CodeBlock/a11y` | | FR3–FR5, AR2 | `CodeBlock.test.tsx`; `CodeBlock.stories.tsx`; exact-head browser evidence | headerless, titled, zero max height, line options, collapse/control removal | Header/copy placement changes, zero is dropped, disclosure state/wiring/keyboard behavior breaks, or inert outlives its control. | `audit:CodeBlock/behavior` | | FR6 | `themingTargets.test.ts`; consumer docs; source review | root, header, title, copy; current and deprecated names | A target leaves its painter, an axis stops reflecting, or a deprecated alias disappears before authorization. | `audit:CodeBlock/theming` | diff --git a/docs/specs/components/DiffViewer/DiffViewer.spec.md b/docs/specs/components/DiffViewer/DiffViewer.spec.md new file mode 100644 index 0000000000000..911ead7d61db5 --- /dev/null +++ b/docs/specs/components/DiffViewer/DiffViewer.spec.md @@ -0,0 +1,306 @@ +--- +schema_version: 3 +template_version: 7 +kind: component +id: component:DiffViewer +authority: draft +archive_reason: null +superseded_by: null +approved_by: null +approved_at: null +owners: [cixzhang, imdreamrunner] +review_triggers: + [public-api, behavior, layout, theming, accessibility, interaction] +verified_by: + [ + packages/core/src/DiffViewer/DiffViewer.test.tsx, + packages/core/src/DiffViewer/parseUnifiedDiff.test.ts, + packages/core/src/DiffViewer/createDiffFile.test.ts, + packages/core/src/DiffViewer/inlineDiff.test.ts, + packages/core/src/DiffViewer/__tests__/DiffViewer.a11y.chromium.spec.ts, + packages/core/src/DiffViewer/__tests__/DiffViewerForcedColors.a11y.chromium.spec.ts, + packages/lab/src/DiffViewer/index.test.ts, + scripts/check-knowledge.mjs, + ] +modules: [] +families: [] +design_specs: [] +architecture: + [ + architecture:public-component-api, + architecture:component-theming-surface, + architecture:internationalization, + ] +contributing: [] +system_specs: [spec:AST-017, spec:AST-025, spec:AST-094] +--- + +# DiffViewer component contract + + + +```json +{ + "paths": ["packages/lab/src/DiffViewer/index.ts"] +} +``` + +## Contract at a glance + +| Area | Contract | +| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Public contract | `DiffViewer`, `parseUnifiedDiff`, `createDiffFile`, and the diff model types from `@astryxdesign/core`. Props: `file`, `viewMode`, `language`, `codeLanguages`, `hasLineNumbers`, `isWrapped`, `changeIndicator`, `inlineHighlight`, `hasChangeBackground`, `collapsible`, `headerEnd`, `renderLineAnnotation`, `onLineNumberClick`, `maxHeight`, `syntaxTheme`, a root `ref`, and BaseProps. | +| Behavior | One read-only, syntax-highlighted file diff: a file header over the hunks in unified or split view, with a `collapsible` disclosure that starts open, a non-colour change cue on every changed line, caller annotation rows, optional line-number buttons, and folds for changes too large to show line by line. | +| End-user impact | Readers on a mouse, touch, keyboard, or screen reader can scan what changed, hear each change named, open and close a file, and reach every line-number button; changes stay visible under forced colors. | +| Builder impact | Build the file with `parseUnifiedDiff` or `createDiffFile` and render one DiffViewer per file. Lab callers change the import. | +| Compatibility/readiness | Additive Core export. The `@astryxdesign/lab` exports are deprecated re-exports of the Core ones for one release (FR14). `spec:AST-094` FR42 gap: under a coarse pointer, the disclosure and line-number buttons keep their own box, with no hit area toward 44 px. | +| Review checks | Reject a second disclosure vocabulary beside `collapsible`, a changed line whose change reads only through colour, a line-number button off its anchor side or under 24 px of press width, an annotation row for a note that renders nothing, a fold that hides an annotated line, or a hunk area that follows the page direction. | +| Governing rules | `architecture:public-component-api` INV19, INV21; `component:CodeBlock` FR12, FR13, DEC-1; `spec:AST-025` (scrollable areas); `spec:AST-017` FR29, FR29a (lifecycle); `spec:AST-094` FR42 (touch targets); WCAG 2.2 SC 1.4.1, 2.5.8, 4.1.2. | + +This table is a review projection; the body below is authoritative. + +## Intent + +DiffViewer shows the change to one file for code review surfaces. It owns the +file header, the unified and split layouts, the change marking, the inline +highlight, the folds, and the slots a review page fills: header actions, +annotation rows, and line-number buttons. CodeBlock owns the tokenizer, the +languages, and the syntax-theme diff slots that colour the rows. Consumer +usage lives in `DiffViewer.doc.mjs`. + +## Compatibility and migration + +- Released default preserved: not yet released in Core; the Lab component's + props keep their meaning and defaults +- Compatibility class: additive Core export; the Lab exports re-export the Core + ones, deprecated, for one release (FR14) +- Controlled/uncontrolled behavior: the disclosure is uncontrolled by default + and controlled through `collapsible={{isOpen, onOpenChange}}` (FR7) +- Migration decision: `spec:AST-017` FR29 and FR29a; Lab callers import from + `@astryxdesign/core` + +Consumer migration instructions belong in consumer docs and release notes. + +## Ownership boundary + +**Owns** + +- The file header: path, status token, line counts, `headerEnd`, and the + disclosure (FR1, FR7). +- The unified and split layouts, their scrolling, and their direction (FR2, + FR6). +- Change marking, inline highlight, and folds (FR4, FR5, FR10). +- The annotation rows and line-number buttons it places for the caller (FR8, + FR9). +- `parseUnifiedDiff`, `createDiffFile`, and the diff model types (FR13). + +**Does not own / non-goals** + +- Tokenizing, languages, and the syntax-theme diff slots — owned by + `component:CodeBlock` (FR12, FR13, DEC-1). +- Scroll measurement, scroll focus, and the scroll area's keyboard access — + owned by `spec:AST-025` through `useScrollableArea`. +- The disclosure state machine — the shared `useCollapsible` state that + `component:Collapsible` and Banner also use. +- Annotation content, header actions, and the action a line-number press + starts — product callsite. +- Editing, staging, or merging changes, and windowing very large files — + product callsite. + +## Public concepts + +| Concept | Closed values or states | Meaning | Availability by variant/orientation/state | Default | Owner | Stability | Invalid-value behavior | +| ---------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ------------------------------------------------------- | --------------------- | ---------------------- | --------- | -------------------------------- | +| View mode | `unified`, `split` | One column, or the old file beside the new file. | Every file with hunks. | `unified` | `component:DiffViewer` | Stable | TypeScript rejects other values. | +| Change indicator | `classic`, `bars`, `none` | How a changed line is marked beside its code. | Every changed line. | `classic` | `component:DiffViewer` | Stable | TypeScript rejects other values. | +| Inline highlight | `word`, `char`, `none` | How finely the change inside a paired line is marked. | Paired removed and added lines outside replaced blocks. | `word` | `component:DiffViewer` | Stable | TypeScript rejects other values. | +| Disclosure | `true`, `false`, or `CollapsibleConfig` (`defaultIsOpen`, `isOpen`, `onOpenChange`) | Whether the file header hides and shows the hunks, and who owns the open state. | A file with a path. | `true`, starting open | `component:DiffViewer` | Stable | TypeScript rejects other values. | +| File status | `modified`, `added`, `deleted`, `renamed` | The kind of change the file model reports. | Every file. | From the model | `component:DiffViewer` | Stable | TypeScript rejects other values. | + +## Behavioral and layout contract + +Draft requirements identify their basis so observed code is not mistaken for an +intentional decision. A `current` contract contains no unresolved rows. + +| ID | Candidate invariant | Basis | Draft review state | +| ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ------------------ | +| FR1 | DiffViewer renders one `DiffViewerFile`. Its file header shows the path (`newPath`, else `oldPath`; a renamed file shows the translated `{oldPath} → {newPath}`), a `Token` for an added (green), deleted (red), or renamed (blue) file, and the added and removed line counts, followed by `headerEnd` when it is renderable. The header renders when the file has a path or `headerEnd`. The path reads left to right in every page direction. | Documented promise; current source and tests | settled | +| FR2 | `viewMode="unified"` renders one table: each hunk's header row, then its lines, removed lines above the added lines that replace them. `viewMode="split"` renders two side-by-side panes, each a table in a group named for its side, and pairs each removed line with the added line that replaced it. The panes share one grid's row tracks, so a row is as tall as its taller side, and a hunk header takes the same width on both sides. | Documented promise; current source and tests | settled | +| FR3 | The language is `language` when set. Otherwise the file's name or extension infers it: a listed `codeLanguages` value first, where a later entry wins, then a built-in, else plain text. Rows take their tint, gutter, and hunk-header paint from the syntax-theme diff slots CodeBlock's diff layer uses, `syntaxTheme` sets a per-instance syntax theme, and highlighting never changes the code text. | `component:CodeBlock` FR12, FR13, DEC-1; tests | settled | +| FR4 | Every added or removed line carries a visually hidden "Added" or "Removed" label in every `changeIndicator` mode. `classic` shows a `+` or `-` glyph, `bars` shows a solid bar for an added line and a dashed bar for a removed line, and `none` shows neither. `hasChangeBackground` (default `true`) tints changed rows. Under forced colors each bar paints as a `CanvasText` border of its own width, and a change that only the tint marks shows the same bar. | Documented promise; WCAG 2.2 SC 1.4.1; tests and Chromium forced-colors evidence | settled | +| FR5 | `inlineHighlight="word"` (the default) or `"char"` marks what changed inside each removed line and the added line paired with it, paired run by run as split view pairs them. Changed words that only whitespace separates join into one mark. A pair that shares nothing but whitespace, a line inside an expanded replaced block, and `inlineHighlight="none"` get no inline mark. | Documented promise; tests | settled | +| FR6 | `hasLineNumbers` (default `true`) shows the old and new line-number gutters. Unwrapped, line numbers and change indicators stay pinned while the code scrolls sideways; in split view each pane scrolls sideways on its own at half the width, both panes take the widest line of either side, and they scroll sideways in lockstep. `isWrapped` wraps long lines at spaces first and inside a word only when the word cannot fit. While code overflows sideways, sideways overscroll is off. `maxHeight` caps the hunk area, which then scrolls. The hunk area reads left to right in every page direction; the file header and annotation content follow the page direction, and annotation rows scroll with the code. | Documented promise; tests | settled | +| FR7 | When the file has a path, `collapsible` (default `true`) makes the file header a disclosure: a native button around the chevron and the path, with `aria-expanded` and `aria-controls` naming the hunk region. It takes the shared `CollapsibleConfig`: `true` starts open, `{defaultIsOpen: false}` starts collapsed, and `{isOpen, onOpenChange}` is controlled, where a press only calls `onOpenChange`. Uncontrolled, a press updates the open state and also calls `onOpenChange`. With `false`, or for a file without a path, there is no disclosure and the hunks always show; with `false` the path is plain text. Collapsed hunks are not rendered. The chevron points to the inline end while collapsed and down while open. | DEC-1; `architecture:public-component-api` INV19, INV21; tests | settled | +| FR8 | DiffViewer calls `renderLineAnnotation` once for each line with its `DiffViewerLineRef`, and renders a full-width annotation row under the line only when the result is renderable. In split view the row sits under the line's own side. | Documented promise; tests | settled | +| FR9 | With `onLineNumberClick`, only the number on each line's anchor side is a native button: the original number of a removed line, and the changed-file number of an added or unchanged line. The button is named "Original line N" or "Changed line N" and calls `onLineNumberClick` with the line's `DiffViewerLineRef` and the event. Every button is its own Tab stop, with no arrow-key movement. Each button takes presses across at least 24 px of width, and its press area stays in its own row. | Documented promise; WCAG 2.2 SC 2.5.8; tests and Chromium hit-test evidence | settled | +| FR10 | A hunk marked `isReplacedBlock`, and a file with more than 2,000 changed lines outside replaced blocks, start folded behind a "Large change: N lines" row, where N counts removed plus added lines, with a Show full diff and Hide full diff button. Split view shows one control across both sides. A fold opens by itself when a line it hides has a renderable annotation. The reader's toggles start over for a new `file`. A file with 2,000 such lines shows no fold. | Documented promise; tests | settled | +| FR11 | A binary file shows "Binary file not shown" in place of hunks, and a file with no hunks shows "No content changes". | Current source and tests | settled | +| FR12 | `ref`, `xstyle`, `className`, `style`, and other DOM props reach the root element, and styling inputs compose. | `architecture:public-component-api` INV5, INV6, INV8; tests | settled | +| FR13 | `parseUnifiedDiff(patch)` returns one `DiffViewerFile` for each file in a git or `diff -u` patch, or for a bare hunk. `createDiffFile` builds a file from two versions of a text and marks a change too large to compare line by line as one `isReplacedBlock` hunk that lists every old line as removed and every new line as added. | Documented promise; tests | settled | +| FR14 | The `@astryxdesign/lab` `DiffViewer`, `parseUnifiedDiff`, `createDiffFile`, and type exports re-export the Core ones, marked deprecated with static metadata, for one release, and are then removed. | `spec:AST-017` FR29, FR29a | settled | +| FR15 | The root carries `diff-viewer` and reflects `viewMode`; the file header carries `diff-viewer-header`; the path carries `diff-viewer-path`. | Current source; `architecture:component-theming-surface` | settled | + +### Allowed variation + +- **AV1 — Syntax paint.** A syntax theme may restyle tokens, row tints, gutters, + and hunk headers through the diff slots CodeBlock owns. +- **AV2 — Caller content.** `headerEnd` and annotation content vary; DiffViewer + places them and never inspects them. + +### Representative states + +| State | Required invariant | Allowed variation | +| ---------------------------- | ------------------------------------------------------------------------------------------- | ----------------------------- | +| Unified, open | Header, hunk headers, numbered lines, glyphs, and tints; disclosure `aria-expanded="true"`. | Language, syntax theme. | +| Split | Two named side tables with paired rows of equal height that scroll sideways together. | Wrapped or unwrapped. | +| Collapsed | Header only; `aria-expanded="false"`; no hunk rows rendered. | Controlled or uncontrolled. | +| Not collapsible | Path as plain text; hunks always shown. | — | +| Annotated, with line buttons | Annotation rows under their lines; anchor-side numbers are named buttons in the Tab order. | Annotation content. | +| Folded | "Large change" row with its button; an annotated line opens its fold. | Replaced block or large file. | +| Binary or no hunks | The matching message in place of hunks. | — | +| Right-to-left page | Header and annotation content follow the page; the hunk area stays left to right. | — | +| Forced colors | Every changed line keeps a visible marker. | Glyphs or bars. | + +### Transformation and precedence order + +- **ORD1 — Language.** `language`, then a listed `codeLanguages` value (a later + entry wins), then a built-in, then plain text. +- **ORD2 — Open state.** A controlled `isOpen`, then the internal state seeded by + `defaultIsOpen`, which defaults to open. +- **ORD3 — Fold.** A reader's toggle for the current file, then an annotated + hidden line (open), then folded. + +### Performance and resources + +- **PR1 — No windowing.** Every row renders as plain DOM. A file with more than + 2,000 changed lines outside replaced blocks renders its folded lines only + when the reader opens the fold (FR10). +- **PR2 — No resource of its own.** DiffViewer adds no document listener, timer, + or observer beyond the scroll areas `useScrollableArea` measures and the + shared highlight styles CodeBlock injects once. + +Current measurements belong in the audit record; this subsection owns only +durable constraints and their verification target. + +## Accessibility contract + +- **AR1 — The diff is a named table.** Unified view is one table and each split + side is a table in a group named "Original code" or "Changed code". Every + column has a screen-reader header: "Original line number", "Changed line + number", "Change", and "Code", "Original code", or "Changed code". The hunk + area is a scroll area named by the path, else "Diff", that takes keyboard + focus while it overflows (`spec:AST-025`). +- **AR2 — A change never reads through colour alone.** Every changed line + carries a hidden "Added" or "Removed" label, and a visible marker stays under + forced colors (FR4). The line counts are hidden from assistive technology, + which hears one sentence such as "12 lines added, 3 lines removed". +- **AR3 — The disclosure is a native button.** It follows the WAI-ARIA APG + disclosure pattern: a `