Skip to content
English
Code diff comes with Pro. Its preview shows once you unlock it.

How it works

The diff is computed in the component by longest common subsequence, then removed and added lines in the same block are paired by position. Within a pair, the common prefix and suffix are left plain and the middle gets a stronger tint, which is what people actually scan for. Lines are 22px monospace and wrap instead of scrolling, so the card never overflows its column; in split view each row is one four-column grid, so both sides stay level when a line wraps, and the missing side of an unpaired line is hatched. Unchanged runs longer than the context fold into a full-width button; rows it reveals fade in. Below minSplitWidth a ResizeObserver switches split to unified.

Use it for

  • Reviewing a change to code, configuration or any text with lines.
  • Showing what an automated fix or a template upgrade will change before people accept it.

Look elsewhere when

  • Prose where sentences move around: compare paragraphs or use tracked changes.
  • Files of tens of thousands of lines: diff on the server and pass the hunks.

Usage

import { CodeDiff } from './CodeDiff.jsx'

export function FileChange({ file }) {
  return (
    <CodeDiff
      fileName={file.path}
      before={file.previousText}
      after={file.text}
      layout="split"
      context={3}
    />
  )
}

Props

PropTypeDefaultWhat it does
before / afterstring—The two versions. Lines are compared exactly by longest common subsequence; fine for files up to a few thousand lines.
fileNameReactNode—Shows a header with the name and +/− counts with five proportion cells.
layout'split' | 'unified''split'Side by side or one column. Split falls back to unified when the component is narrower than minSplitWidth.
minSplitWidthnumber520Width in px under which split shows as unified.
contextnumber3Unchanged lines kept next to each change; longer runs fold into a button that reveals them.
wordDiffbooleantrueMarks the changed middle of each removed/added pair, after their common prefix and suffix.
labels{ added, removed, expand(n), stats(add, del), before, after }—Every built-in string, including what screen readers hear before each changed line.

Accessibility

  • Removed lines are del elements and added lines ins elements, each starting with visually hidden “Removed:” or “Added:”, so the change is never told by colour alone; the + and − signs are decorative.
  • Line numbers cannot be selected, so copying code takes only the code.
  • Each fold is a real button that says how many lines it hides; Tab reaches it and Enter or Space opens it.
  • The +/− summary has an accessible name with both counts.
  • Reduced motion removes the fade of revealed rows.

Motion

Rows revealed by a fold fade in over 260ms.

Add it to your project

Pro members copy a prompt and their coding agent installs the source, the same way as the free components.

CodeDiff.jsxcode-diff.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Data display
Flip countdown comes with Pro. Its preview shows once you unlock it.