Cómo funciona
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.
Úsalo para
- 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.
Busca otra opción cuando
- 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.
Uso
import { CodeDiff } from './CodeDiff.jsx'
export function FileChange({ file }) {
return (
<CodeDiff
fileName={file.path}
before={file.previousText}
after={file.text}
layout="split"
context={3}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
before / after | string | — | The two versions. Lines are compared exactly by longest common subsequence; fine for files up to a few thousand lines. |
fileName | ReactNode | — | 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. |
minSplitWidth | number | 520 | Width in px under which split shows as unified. |
context | number | 3 | Unchanged lines kept next to each change; longer runs fold into a button that reveals them. |
wordDiff | boolean | true | Marks 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. |
Accesibilidad
- 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.
Movimiento
Rows revealed by a fold fade in over 260ms.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
CodeDiff.jsxcode-diff.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.



