Diff Viewer
The DiffViewer component provides a powerful way to display file differences with syntax highlighting. It supports both Split (side-by-side) and Unified (inline) display modes, making it perfect for code review interfaces, version comparison tools, and change visualization.
Variations
Unified mode
By default the diff renders in Split mode (old on the left, new on the right). Pass diffViewMode={DiffModeEnum.Unified} to render the diff inline, stacked in a single column — better for narrow layouts, side panels, and focused review of small changes.
Installation
import { DiffViewer } from "@harnessio/ui/components";
function MyComponent() { const oldCode = `function hello() {\n console.log("Hello");\n}`; const newCode = `function hello(name) {\n console.log(\`Hello \${name}\`);\n}`;
return <DiffViewer oldCode={oldCode} newCode={newCode} lang="typescript" />;}Note: The DiffViewer requires ThemeProvider from @harnessio/ui/context to be present in your component tree for theme detection to work properly.
Anatomy
DiffViewer is a single, self-contained component — you give it the two versions of a file and it renders the comparison. It’s built on @git-diff-view/react, with syntax highlighting supplied by Shiki.
<DiffViewer oldCode={oldCode} newCode={newCode} lang="typescript" diffViewMode={DiffModeEnum.Split}/>- Old / new code —
oldCodeandnewCodeare the two strings being compared; the component computes the diff between them. - Layout —
diffViewModeswitches betweenSplit(side-by-side, the default) andUnified(inline). Each line carries a line number for the old and new sides, and additions, deletions, and modifications are marked visually. - Syntax highlighting —
langsets the language grammar. Shiki supports 200+ languages (typescript,tsx,javascript,jsx,python,go,java,yaml,json,bash,sql, and many more); it defaults toyaml. - Theme — the component reads the current theme from
ThemeProviderviauseTheme()and renders light or dark highlighting to match, updating in real time when the theme changes. It must be rendered inside aThemeProvider.
Best practices
Do
- Set
langto match the file you’re diffing so the syntax highlighting is accurate. - Give enough surrounding context in
oldCode/newCodefor a change to make sense on its own. - Reach for
Unifiedmode in narrow containers (side panels, mobile) and keepSplitfor wide, detailed review. - Turn on
diffViewWrapwhen lines are long and horizontal scrolling would hide changes.
Don’t
- Don’t drop a very large file into a single viewer — page or limit the visible range so review stays manageable.
- Don’t render
DiffVieweroutside aThemeProvider; theme detection won’t work.
Content
- Format both versions of the code before passing them in — the viewer shows exactly what it’s given, so pre-existing formatting noise reads as changes.
- Use
\nfor line breaks when building code strings inline.
Accessibility
DiffViewer is a read-only, presentational view of a comparison. There are no interactive controls within it.
Labeling
- Provide context around the viewer (a heading or caption naming the file or comparison), since the component itself doesn’t announce what’s being diffed.
Known limitations
- Changes are conveyed visually. Additions, deletions, and modifications are shown through color and line markers; there’s no textual, screen-reader-specific summary of what changed. Where the diff carries important meaning, pair it with a written summary of the change.
Properties
Prop | Required | Default | Type |
|---|---|---|---|
| oldCode | true | string | |
| newCode | true | string | |
| lang | false | 'yaml' | DiffHighlighterLang |
| diffViewWrap | false | false | boolean |
| diffViewMode | false | DiffModeEnum.Split | DiffModeEnum |