Skip to Content
⭐️ Leave a star →
ComponentsDiff

Diff

Line-level diff viewer with both unified and side-by-side (split) layouts. The diff is computed with a built-in LCS algorithm — no dependencies — and exposed as render parts you style yourself.

Features

Computes line diffs with a built-in LCS algorithm — zero dependencies.
Render unified (single column) or split (side-by-side) layouts.
Exposes additions/deletions counts through a Stats render prop.
Each part is a render function — you own all markup and styling.
Tracks old and new line numbers for gutter rendering.

Example

src/math.ts
+33
1-export function add(a, b) {
2- return a + b;
1+export function add(...nums) {
2+ return nums.reduce((a, b) => a + b, 0);
33 }
44
5-const total = add(1, 2);
5+const total = add(1, 2, 3);
import { Diff } from '@wire-ui/react' <Diff.Root oldValue={before} newValue={after}> <Diff.Stats> {({ additions, deletions }) => ( <div>+{additions} −{deletions}</div> )} </Diff.Stats> <Diff.Unified> {({ line }) => ( <div>{line.oldLine} {line.newLine} {line.content}</div> )} </Diff.Unified> </Diff.Root>

Split layout

Diff.Split pairs deletes and inserts into side-by-side rows. Each row provides an optional left (old) and right (new) DiffLine — either may be absent when one side has no counterpart.

<Diff.Split> {({ left, right }) => ( <div className="grid grid-cols-2 divide-x divide-[#e5e7eb]"> <div className={left ? lineClass(left.type) : 'bg-[#fafafa]'}> {left?.oldLine} {left?.content} </div> <div className={right ? lineClass(right.type) : 'bg-[#fafafa]'}> {right?.newLine} {right?.content} </div> </div> )} </Diff.Split>

Styling

Diff ships no styles. Diff.Unified wraps each line in a data-diff-line element carrying data-type, and Diff.Split wraps each row in a data-diff-row element carrying data-left / data-right. Style the line/row wrappers — or branch on line.type inside the render function — to color insertions and deletions.

[data-diff-line][data-type="insert"] { background: #f0fdf4; } [data-diff-line][data-type="delete"] { background: #fef2f2; }

Using data attributes

Diff.Unified renders a <div data-diff-view="unified"> whose children carry data-diff-line and data-type. Diff.Split renders a <div data-diff-view="split"> whose children carry data-diff-row, data-left, and data-right.

[data-diff-view="split"] [data-left="delete"] { background: #fef2f2; } [data-diff-view="split"] [data-right="insert"] { background: #f0fdf4; }

Root props

PropTypeDefaultDescription
oldValuestringrequiredThe original (“before”) text
newValuestringrequiredThe updated (“after”) text

Root also accepts all standard div attributes (className, id, …).

Unified props

PropTypeDescription
children(props: { line: DiffLine }) => ReactNodeRender function for a single unified line

Split props

PropTypeDescription
children(props: DiffRow) => ReactNodeRender function for a single side-by-side row

Stats props

PropTypeDescription
children(props: DiffStats) => ReactNodeRender function for the additions/deletions summary

Types

type DiffLineType = 'equal' | 'insert' | 'delete' interface DiffLine { type: DiffLineType content: string // line text, no trailing newline oldLine?: number // 1-based line in the old text (absent for inserts) newLine?: number // 1-based line in the new text (absent for deletes) } interface DiffRow { left?: DiffLine // old side right?: DiffLine // new side } interface DiffStats { additions: number deletions: number }

Data attributes

AttributeElementValues
data-diff-viewUnified / Split container"unified" / "split"
data-diff-lineUnified line wrapperPresent on every line
data-typeUnified line wrapper"equal" / "insert" / "delete"
data-diff-rowSplit row wrapperPresent on every row
data-leftSplit row wrapperLeft line type, if present
data-rightSplit row wrapperRight line type, if present

Accessibility

Diff renders plain div elements with no interactive behavior — it is a presentational viewer. Provide your own semantics (for example, a caption naming the file being compared) when embedding it in a larger UI.

Last updated on

MIT License © 2026 wire-ui

Diff – Wire UI