Diff viewer
Shows what changed between two texts: a config before and after, two versions of a document, a generated file. Lines are compared, then the changed words inside them are marked, side by side or in one column. To show one version, use a code block instead.
Install
npx shadcn@latest add @thrum-pro/diff-viewerThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Automatically installs icon chevrons-up-down from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
before | string | Required | The old text. |
after | string | Required | The new text. |
title | string | Required | A name for the change, such as a file name. It heads the frame and names the diff for screen readers. |
view | "split" | "unified" | – | The view, when the parent holds it. Use it with onViewChange. |
defaultView | "split" | "unified" | Default "split" | The view at first, when the viewer holds it itself. Below 560 pixels wide split shows as unified, and the switch is hidden. |
onViewChange | (view: "split" | "unified") => void | – | Called with the view picked in the header. |
context | number | Default 3 | Unchanged lines kept on each side of a change. Longer runs fold behind a Show N unchanged lines button. |
lineNumbers | boolean | Default true | Show the old and the new line numbers. |
wordDiff | boolean | Default true | Mark the changed words inside a changed line, pairing removed and added lines of one change in order. |
emptyLabel | string | Default "No changes" | Shown when the two texts are the same. |
sound | boolean | Default true | Play a click when the view changes, if the visitor has sound on. |
className | string | – | Extra classes for the frame, merged last. Give it a height, such as h-80, to scroll a long diff inside it. |
diffSequences(), diffLines(), diffWords(), foldUnchanged() | functions | – | More exports: the shortest edit script of two lists (Myers' algorithm), the lines of a text diff, the word marks of a changed pair, and the folding of unchanged runs. |
Motion and sound
Motion
- Nothing moves but the view switch's thumb, which slides over 150 ms.
- Reduced motion: the thumb moves at once.
Sound
- A click when the view changes. Opening a fold is silent.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- Each view is a table named by the title and the counts, such as sync.config.ts, 4 added, 3 removed.
- Removed lines carry a minus and added lines a plus as well as their tint, and screen readers hear Removed or Added before the line.
- Changed words are ins and del elements, which some screen readers announce as inserted or deleted.
- The counts in the header read 4 lines added, 3 lines removed to screen readers.
- The frame is a panel surface (data-surface="panel"): under a glass material it becomes translucent while keeping diff tints and code readable.
- Long lines wrap anywhere, so nothing is cut at 160 pixels wide.
| Keys | Action |
|---|---|
| Tab | Move to the view switch, the frame, which then scrolls with the arrow keys, and each Show unchanged lines button. |
| ← / → | On the view switch: pick split or unified. |
| Enter / Space | On a fold: show the lines behind it. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/diff-viewer.tsx- Packages
- None
- Brings along
- Segmented controlChart foundations
- Icons
chevrons-up-down
Changelog
- 1.0.0 · 2026-12-03First release.