DiffViewer Text Diff
A read-only text diff viewer: line-level differences with word-level highlights, unified and split views, collapsed unchanged regions, fixed-row-height virtual scrolling, and full keyboard/screen-reader support. Suited to config version comparison, prompt review, and formatted JSON before/after checks. It does not edit, merge, accept/reject changes, or compute structural JSON diffs.
Basic usage
Pass the two texts through old-text / new-text. The default unified view marks deleted lines with − and added lines with +, showing both line-number columns. Unchanged runs longer than context (default 3) collapse into an expandable row.
Config file versions
Word-level highlighting only marks the truly changed fragments of paired replacement lines; replacement blocks with unequal line counts stay line-level to avoid misleading matches.
Empty and identical
Empty inputs show a placeholder; identical inputs keep every row and show “Both sides are identical”. Both states downgrade ARIA semantics so no empty grid structure is exposed.
Empty and identical
Split view
mode="split" renders two columns: old text on the start side, new text on the end side (mirrored automatically in RTL), aligned by change block. old-label / new-label override the column headers.
Side-by-side review
When the container is narrower than split-min-width, the viewer degrades to unified (the mode attribute keeps its value) so neither side becomes unreadable. Split rows use data-op="replace"; the real insert/delete op lives on each before-code / after-code cell — read the cell when styling via ::part().
Folding and bulk actions
Fold rows are clickable, focusable with Tab, and expandable with Enter. expandAll() / collapseAll() switch all regions at once; fold state resets when the data changes so old positions cannot point at new content.
Expand all / collapse unchanged
Last event: —
Long files and virtual scrolling
When aligned rows exceed virtual-threshold (default 2000), fixed-row-height virtual scrolling keeps only viewport-near rows in the DOM. virtual="true" forces it on, virtual="false" turns it off. height and row-height control the viewport and row height.
3000 lines, viewport only
Generating data…
Comparison strategy
ignore-whitespace normalizes whitespace before comparing (runs collapsed, line edges trimmed) while displaying the original text. max-edits (default 1000) caps the line-level edit distance; beyond it the viewer degrades to block replacement and shows a notice, so huge diffs cannot block the main thread.
Ignore whitespace
Plain-text review
toolbar="false" hides stats and bulk actions, show-line-numbers="false" hides line numbers, and wrap allows long lines to wrap (wrapping disables virtual scrolling and lets row height follow content).
No toolbar, wrapped
Stats and events
The read-only stats property returns { added, removed, unchanged, hunks }; oas-diff-change is dispatched on mount and after data updates with the same shape. Expanding a fold dispatches oas-hunk-toggle; expandAll() / collapseAll() dispatch one event per changed fold region (expanded true / false) with { index, expanded, start, count }.
const viewer = document.querySelector('#dv-basic')
viewer.stats // { added: 3, removed: 3, unchanged: 3, hunks: 1 }
viewer.scrollToChange(0) // scroll to the first change block
viewer.expandAll()
viewer.addEventListener('oas-hunk-toggle', (event) => {
console.log(event.detail) // { index, expanded, start, count }
})Keyboard and accessibility
The viewport is a single Tab stop; aria-activedescendant points at the active row. Semantics are role="grid" + row + gridcell; line numbers are hidden from screen readers and changed rows carry "Added line N / Removed line M" accessible names. Additions and deletions are conveyed by the + / − prefix, background color, and accessible name together — never color alone.
| Key | Behavior |
|---|---|
↑ / ↓ | Previous / next row |
Home / End | First / last row |
PageUp / PageDown | Page up / down |
Enter / Space | Expand / collapse the current fold |
Theme and parts
Add/remove backgrounds and word highlights use dedicated opaque tokens (--oas-color-diff-add-bg / --oas-color-diff-del-bg plus their -word- variants, switched automatically in dark mode); line numbers use --oas-color-diff-gutter-text. Style with ::part(): viewer, toolbar, status, added, removed, summary, split-head, row, code, before-no, after-no, fold, fold-toggle, placeholder. RTL mirrors logical properties and keeps old text on the start side in split view.
API
Pure model helpers are also available from @oas-ui/ui/data/diff-viewer (or @oas-ui/ui) for host-side preprocessing or custom rendering: diffLines, layoutRows, buildDiffRows, layoutSplitRows, buildSplitRows, inlineSegments, splitLines. They do not depend on the DOM.
oas-diff-viewer
Attributes
| Attribute | Description | Type | Default |
|---|---|---|---|
aria-label | Accessible name of the diff table (defaults to “Diff”) | — | — |
context | Unchanged lines kept on each side when folding (default 3; 0 keeps only the changed lines) | string | 3 |
height | Viewport height in px (default 480) | string | 480 |
ignore-whitespace | Normalize whitespace before comparing (collapse runs, trim line edges) while displaying the original text | boolean | — |
max-edits | Line-level edit-distance cap (default 1000); beyond it the viewer degrades to block replacement and shows a notice | string | 1000 |
mode | View mode: unified (default) / split | string | unified |
new-label | Header text for the new-text split column (defaults to “After”) | string | — |
new-text | New text (attribute channel; SSR/DSD friendly) | string | — |
old-label | Header text for the old-text split column (defaults to “Before”) | string | — |
old-text | Old text (attribute channel; SSR/DSD friendly) | string | — |
row-height | Fixed row height in px (default 22, minimum 12) | string | 22 |
show-line-numbers | Line-number toggle; false hides the line-number cells | string | — |
split-min-width | Minimum container width in px for split view (default 640); narrower containers fall back to unified | string | 640 |
toolbar | Toolbar toggle; false hides stats and bulk actions | string | — |
virtual | Virtual scrolling: auto by default (enabled when aligned rows exceed virtual-threshold), true forces it on, false turns it off | string | — |
virtual-threshold | Aligned-row threshold for enabling virtual scrolling (default 2000) | string | 2000 |
wrap | Wrap long lines; wrapping disables virtual scrolling and lets row height follow content | boolean | — |
Property (JS property; see each row for attribute reflection)
| Property | Description | Type | Default |
|---|---|---|---|
newText | New-text property; writes reflect the new-text attribute | string | — |
oldText | Old-text property; writes reflect the old-text attribute | string | — |
stats | Diff statistics (read-only): { added, removed, unchanged, hunks } | DiffStats | — |
Methods
| Method | Signature | Description |
|---|---|---|
expandAll | expandAll(): void | Expand all unchanged regions |
collapseAll | collapseAll(): void | Collapse all unchanged regions |
scrollToChange | scrollToChange(index: number): boolean | Scroll to the given change block and focus its first row; returns false when out of range |
Events
| Event | Description |
|---|---|
oas-diff-change | Dispatched after data updates, detail: { added, removed, unchanged, hunks } |
oas-hunk-toggle | Dispatched after a fold expands/collapses, detail: { index, expanded, start, count } |
CSS Variables
| CSS Variable | Default |
|---|---|
--oas-diff-viewer-add-bg | var(--oas-color-diff-add-bg) |
--oas-diff-viewer-add-word-bg | var(--oas-color-diff-add-word-bg) |
--oas-diff-viewer-del-bg | var(--oas-color-diff-del-bg) |
--oas-diff-viewer-del-word-bg | var(--oas-color-diff-del-word-bg) |
--oas-diff-viewer-height | 480px |
--oas-diff-viewer-row-height | 22px |