Diff Viewer
FreeCompare two versions of a text, or read a unified patch: unified or split view, both line numbers, a visible `+` / `−` on every changed line and an `sr-only` "Added:" / "Removed:" (never colour alone), long unchanged runs folded behind "Show N unchanged lines", the changed words highlighted inside a changed line, CRLF and LF treated alike, a lost final newline shown, and a size cap that says so instead of freezing the tab. `svLineDiff()` is exported; the engine is the kit's shared one in `@styloviz/core`.
Complexity tier: Primitive Presents a value: held to semantic markup and contrast, laid out with logical properties so it mirrors under right-to-left, and with what it does on paper decided.
import { SvDiffViewerComponent } from '@styloviz/code-block';Demo & usage
Basic usage
Usage example — open the gallery for the full interactive demo.
An interactive demo for this component lives in the gallery.
Open live demoMore examples
Side by side
<sv-diff-viewer [before]="v1" [after]="v2" view="split" [context]="2" />
A git patch
The file name comes from the `---` / `+++` lines and the numbers from the `@@` headers. Lines the patch leaves out are counted, not unfoldable.
<sv-diff-viewer [patch]="pullRequest.diff" />
The comparison on its own
import { svLineDiff } from '@styloviz/code-block'; const d = svLineDiff(before, after, { ignoreWhitespace: true, maxLines: 5000 }); // d.status: 'ok' | 'too-large' | 'too-different' // d.rows: { kind: 'add' | 'del' | 'context', text, oldNo, newNo, noEol? }[]
When to use
Showing what changed in a config, a template, a document or a record's text between two versions. Reviewing a patch or a pull request diff in an admin or developer tool. Use `sv-code-block variant="diff"` to let a user accept or reject AI-suggested hunks.
Compare with
- Code Block — Code Block's `diff` variant renders hunks you hand it, with syntax colour and per-hunk Accept / Reject for reviewing an AI edit. Diff Viewer computes the diff itself from two texts (or reads a git patch), folds unchanged runs and highlights the changed words.
- Audit Log — Audit Log shows who changed which fields of a record and when, and uses Diff Viewer for each long or structured value. Use Diff Viewer on its own to compare two versions of one text.
Async states
None — this component has nothing to load, so it has no loading, empty or error state to drive.
Inputs
| Name | Type | Default | Description |
|---|---|---|---|
| before | string | '' | The original text, compared against `after`. Ignored while `patch` is set. |
| after | string | '' | The new version of the text. Ignored while `patch` is set. |
| patch | string | null | null | A unified diff (`git diff`, `diff -u`) to show instead of comparing `before` and `after`. The first file is shown; lines the patch leaves out are counted, not unfoldable. |
| view | SvDiffViewerView (two-way) | 'unified' | Unified (one column, `+` / `−`) or split (old left, new right). Two-way bindable. |
| showViewToggle | boolean | true | Show the Unified / Split switch in the header. |
| context | number | 3 | Unchanged lines kept visible either side of a change; longer runs fold. |
| wordDiff | boolean | true | Highlight the words that changed inside a changed line. |
| lineNumbers | boolean | true | Show the old and new line numbers. |
| ignoreWhitespace | boolean | false | Compare lines with leading and trailing whitespace removed. |
| ignoreCase | boolean | false | Compare lines case-insensitively. |
| maxLines | number | 5000 | Refuse to compare when the two texts hold more lines than this between them, and say so instead. `0` lifts the cap — only for texts you know are small. |
| title | string | '' | File name shown in the header and used in the region's accessible name. |
| beforeLabel | string | 'Before' | Name of the old side in the split view. |
| afterLabel | string | 'After' | Name of the new side in the split view. |
| maxHeight | string | '' | Max height of the diff body, e.g. `'28rem'`; it scrolls past that. `''` for none. |
| ariaLabel | string | '' | Accessible name of the region. Falls back to the title, then to "Diff". |
Outputs
| Event | Payload type | Description |
|---|---|---|
| viewChange | SvDiffViewerView | Emits the new `view` whenever it changes — the output half of `[(view)]`. |
Customization & theming
Added and removed tints, markers and word highlights paint from the `accent` and `danger` tokens; notices from `warning` and `info`. `context`, `wordDiff`, `lineNumbers`, `showViewToggle`, `maxHeight`, `beforeLabel` / `afterLabel` tune the layout. `svLineDiff`, `svWordDiff`, `svParsePatch`, `svDiffViewItems` and `svDiffSplitSections` are exported for your own renderer. All visible strings are `codeBlock` labels in 12 language packs; the summary sentence uses the `core` diff labels.
Best practices
Do: Set `title` to the file name — it names the region and heads the diff. Do: Keep `maxLines` at its default unless you know the inputs are small; past it the viewer explains rather than computing. Don't: Do not use it to edit text — it is a display component. Tip: Turn on `ignoreWhitespace` for re-indented code, where every line would otherwise read as changed. Tip: `[(view)]` two-way binds the Unified / Split switch, so you can remember a reader's choice.
Accessibility
The diff is a named region described by a summary sentence ("2 changes, 5 lines added, 3 lines removed."), so its scale is known before it is read. Changed lines carry a visible `+` / `−` and an `sr-only` "Added:" / "Removed:"; unchanged lines stay silent. Line numbers and markers are `aria-hidden`. Each scroll pane is focusable and named (the diff, or Before / After in split view), so a keyboard can scroll a long line; split-view padding rows are hidden from screen readers. The layout switch is two `aria-pressed` buttons in a named group; fold buttons say how many lines they reveal. Every value renders as text — a patch adding `<script>` shows those characters.
Feedback on Diff Viewer
Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.