Diff

Compare two texts line by line, with the words that changed highlighted. Use it to review a change before it is saved, such as an edited document, a config file or a generated answer.

General

Pass the text before in previousValue and the text after in currentValue. Removed lines are marked with a minus and added lines with a plus.

Split

mode="split" puts the previous text on the left and the current text on the right.

Context lines

With contextLines, the unchanged lines far from a change fold into a button that brings them back.

Highlight and colors

diffMethod sets what is highlighted inside a changed line: words, chars, or lines for none.

APIs

Diff.Props

AttributeDescriptionTypeAccepted valuesDefault
previousValuethe text before the changestring--
currentValuethe text after the changestring--
modehow the two texts are laid outstring'unified', 'split'unified
diffMethodwhat is highlighted inside a changed linestring'words', 'chars', 'lines'words
contextLinesunchanged lines kept around each change, the rest foldsnumber-all of them
withLineNumbersshow the line numbersboolean-true
addColorcolor of the added linesstring-theme success
removeColorcolor of the removed linesstring-theme error
expandLabeltext of the button of a folded run(hidden: number) => string-Show N hidden lines
...native props, passed to the rootHTMLAttributes'aria-label', 'className', ...-

Ref

The ref points at the root div.

Accessibility

Each line starts with a + or a -, so a change reads as text and not only as color. Folded lines are behind a real button, with its text set by expandLabel.

The diff is a table of lines. For a long change, say what changed in a sentence next to it.

Edit this page on GitHub