Diff
Highlight differences between two strings
Source
LLM docs
Docs
Package
License
Usage
Diff component highlights differences between two strings. By default, it uses
word-level diff granularity and unified display mode:
| 1 | - import { useState } from 'react'; | |
| 1 | + import { useReducer } from 'react'; | |
| 2 | 2 | |
| 3 | 3 | function Counter() { |
| 4 | - const [count, setCount] = useState(0); | |
| 4 | + const [count, dispatch] = useReducer(reducer, 0); | |
| 5 | 5 | return ( |
| 6 | - <button onClick={() => setCount(count + 1)}> | |
| 6 | + <button onClick={() => dispatch({ type: 'increment' })}> | |
| 7 | 7 | Count: {count} |
| 8 | 8 | </button> |
| 9 | 9 | ); |
| 10 | 10 | } |
Split mode
Set mode="split" to display the diff in a side-by-side layout:
| 1 | - import { useState } from 'react'; | 1 | + import { useReducer } from 'react'; | |
| 2 | 2 | |||
| 3 | function Counter() { | 3 | function Counter() { | |
| 4 | - const [count, setCount] = useState(0); | 4 | + const [count, dispatch] = useReducer(reducer, 0); | |
| 5 | return ( | 5 | return ( | |
| 6 | - <button onClick={() => setCount(count + 1)}> | 6 | + <button onClick={() => dispatch({ type: 'increment' })}> | |
| 7 | Count: {count} | 7 | Count: {count} | |
| 8 | </button> | 8 | </button> | |
| 9 | ); | 9 | ); | |
| 10 | } | 10 | } |
Diff method
Use the diffMethod prop to change how changed lines are highlighted: by characters,
by words (default) or by whole lines:
Characters
| 1 | - The quick brown fox jumps over the lazy dog. | |
| 1 | + The quick red cat leaps over the lazy dog. |
Words (default)
| 1 | - The quick brown fox jumps over the lazy dog. | |
| 1 | + The quick red cat leaps over the lazy dog. |
Lines
| 1 | - The quick brown fox jumps over the lazy dog. | |
| 1 | + The quick red cat leaps over the lazy dog. |
Context lines
Set contextLines prop to control how many unchanged lines are displayed around changes.
Other unchanged lines are collapsed and can be expanded with the expand button:
| 4 | 4 | line 4 |
| 5 | 5 | line 5 |
| 6 | - old line 6 | |
| 6 | + new line 6 | |
| 7 | 7 | line 7 |
| 8 | 8 | line 8 |
Use expandLabel prop to change the expand button label:
Custom colors
Use addColor and removeColor props to change the highlight colors:
| 1 | - The quick brown fox | |
| 1 | + The quick red fox |
Configurator
| 1 | - import { useState } from 'react'; | |
| 1 | + import { useReducer } from 'react'; | |
| 2 | 2 | |
| 3 | 3 | function Counter() { |
| 4 | - const [count, setCount] = useState(0); | |
| 4 | + const [count, dispatch] = useReducer(reducer, 0); | |
| 5 | 5 | return ( |
| 6 | - <button onClick={() => setCount(count + 1)}> | |
| 6 | + <button onClick={() => dispatch({ type: 'increment' })}> | |
| 7 | 7 | Count: {count} |
| 8 | 8 | </button> |
| 9 | 9 | ); |
| 10 | 10 | } |
Mode
Diff method