Diff view

Slant / Content

A line diff, unified or split, with word marks, +/− totals, folded unchanged runs, keys from change to change, and a slot for line comments.

Demo

Usage

@tuttn/slant
import { Button, Checkbox, DiffView, PlusIcon, type DiffHunk, type DiffViewLayout } from '@tuttn/slant'

// Two texts, diffed by line: the changed words marked, three unchanged lines
// kept around each change, and the rest folded behind a button.
<DiffView before={oldSource} after={newSource} language="ts" title="src/app.ts" />

// Hunks you already have, from a patch parser: the lines between them show
// as counts. With no string title, label names the body.
const hunks: DiffHunk[] = parsePatch(patch)
<DiffView hunks={hunks} layout="split" label="Changes to notes.md" />

// Uncontrolled layout, with a Unified/Split toggle in the bar…
<DiffView before={a} after={b} layoutToggle defaultLayout="split" />

// …or controlled: you hold it, say to keep the reader's choice.
const [layout, setLayout] = useState<DiffViewLayout>('unified')
<DiffView before={a} after={b} layoutToggle layout={layout} onLayoutChange={setLayout} />

// More context, no folding at all, or none of the bar's extras.
<DiffView before={a} after={b} context={10} />
<DiffView before={a} after={b} context={Infinity} stats={false} navigation={false} />

// Line comments: a button in each line's gutter, and the thread under its
// line. index is the line's place in the whole diff, the same in both
// layouts and whatever is folded, so it keys the comments. A line with a
// thread is never folded away.
const [threads, setThreads] = useState<Record<number, Comment[]>>({})
<DiffView before={a} after={b} title="src/app.ts"
  header={<Checkbox>Viewed</Checkbox>}
  renderLineAction={(line, index) => <Button size="sm" variant="ghost" icon
    aria-label={`Comment on line ${line.newNumber ?? line.oldNumber}`} onClick={() => openThread(index)}>
    <PlusIcon size={14} />
  </Button>}
  renderLineAfter={(line, index) => threads[index] && <CommentThread comments={threads[index]} />} />

Props

PropTypeDefaultDescription
beforestring—The old text, diffed line by line against `after`. Leave it out for a new file.
afterstring—The new text. Leave it out for a deleted file.
hunksreadonly DiffHunk[]—Hunks already cut, from a patch parser or a server, instead of `before` and `after` (which are then ignored). The unchanged lines between them are counted from their line numbers and shown as a count, not lines.
languagestring—The language, for highlighting: ts, tsx, js, jsx, swift, css, json, html, bash and sh are highlighted; anything else is plain.
highlightbooleantrueLightly highlights comments, strings, keywords, numbers and attributes, as CodeBlock does.
titleReactNode—The bar's title: a file name, say.
headerReactNode—Controls in the bar after the title and totals, such as a "Viewed" checkbox.
labelstringthe title, when it is a string; a title that is a node names the body through aria-labelledbyThe body's accessible name.
layoutDiffViewLayout—Unified (one column, old and new numbers) or split (old beside new). Pass it to control the layout.
defaultLayoutDiffViewLayout'unified'The layout an uncontrolled view starts with.
onLayoutChange(layout: DiffViewLayout) => void—Called with the new layout when the toggle changes it.
layoutTogglebooleanfalseShows a Unified/Split toggle in the bar.
contextnumber3With `before` and `after`, how many unchanged lines show around each change; the rest fold behind a button.
lineNumbersbooleantrueNumbers the lines, old and new.
wordMarksbooleantrueMarks the words that changed inside a changed line.
statsbooleantrueShows the +added and −removed totals in the bar.
navigationbooleantrueShows previous and next change buttons in the bar (Alt+↑ and Alt+↓ in the body do the same).
wrapbooleanfalseWraps long lines instead of scrolling sideways.
maxHeightnumber—The height in pixels at which the body starts to scroll.
renderLineAction(line: DiffLine, index: number) => ReactNode—A control for one line, such as a "Comment" button, drawn in the line's gutter: shown while the row is hovered or holds focus, and always in the tab order. Called for each line on show. `index` is the line's place in the whole list (from `before`/`after`, or every hunk's lines in order), the same in both layouts and whatever is folded, so it can key comments.
renderLineAfter(line: DiffLine, index: number) => ReactNode—Content under a line, across the full width: a comment thread, say. Called for every line; a line that gets a node is never folded away. `index` is the same as `renderLineAction`'s.
…restHTMLAttributes<HTMLDivElement>—className, style, the ref and other attributes reach the root <div>. The body (the scrolling region) is named by label or the title.

Keyboard

Tabmoves through the bar (header, previous and next change, layout), then the body, then each line action, fold button and control under a line, in reading order
Alt+↓ / Alt+↑in the body, moves to the next or previous change: from the line holding focus, else from the current change. Left alone inside an input, textarea or contenteditable, such as a comment box under a line
Enter / Spaceon a fold, shows the unchanged lines it holds and moves focus to the first of them; on Previous change or Next change, moves to that change
← → (on the layout toggle)switches between Unified and Split
← → ↑ ↓ / PageUp / PageDownon the body, scrolls it when it overflows

Accessibility

  • The body is a focusable region named by label, or the title when it is a string (a title that is a node names it through aria-labelledby), so a keyboard can scroll it. Without either it is still focusable, but unnamed: give it one.
  • Line numbers and the +/− signs are aria-hidden, and left out of a selection. Each added or removed line starts with a visually hidden “Added” or “Removed” (messages.diffAdded, diffRemoved), so a screen reader hears what changed line by line; changed words are <mark> elements.
  • Split, an unchanged line shows on both sides; the old copy is aria-hidden, so it is read once.
  • The totals in the bar are aria-hidden and read as one sentence instead (messages.diffStats, “4 lines added, 2 removed”).
  • Moving to a change focuses its first line (rows are tabIndex −1), scrolls it into view, marks it data-current and says “Change 2 of 5” (messages.diffChangeOf) through the root’s polite announcer. The bar’s buttons disable at the first and last change.
  • A line action sits in the line’s gutter, hidden until the row is hovered or holds focus (always shown on touch screens), but it is in the tab order and the accessibility tree all along: name it after its line, as the usage does.
  • Added and removed lines use the success and danger soft tints with the text colour; on them, and on a changed word’s stronger tint, the token colours lean towards the text colour, so every theme keeps 4.5:1, Paper included.

Two texts or hunks

Given before and after, DiffView diffs them by line (a Myers diff, with marks on the words that changed inside a changed line when the two share enough of their text), keeps context unchanged lines around each change, and folds every other unchanged run behind a button reading “Show 12 unchanged lines”. A fold opens in place and focus moves to its first line. Given hunks (from your own patch parser or a server: each a start and length per side and its DiffLines), it shows them as they are: lines without numbers are numbered from their hunk’s start, and the lines between two hunks, counted from the numbers, read “12 unchanged lines” as plain text, since there is nothing to show. With neither, or nothing changed, the body reads “No changes”.

Highlighting

The same tokenizer as CodeBlock’s, for ts, tsx, js, jsx, swift, css, json, html, bash and sh. With two texts, each is highlighted whole and each line takes its own side’s runs, so a block comment or template string that spans lines stays one; hunks are highlighted a line at a time. Word marks cut through the runs, so a changed word inside a string keeps the string’s colour.

Layouts

Unified shows each line once, with its old and new numbers and a sign. Split shows the old text beside the new, two equal halves that scroll sideways together; a change pairs its removed and added lines by position and pads the shorter side. Both keep every line’s index, its current change and its opened folds, and content under a line stays mounted across a switch, so a half-written comment survives it. wrap wraps long lines instead of scrolling sideways; maxHeight caps the body.

Line actions and content under a line

renderLineAction is called for each line on show and its node sits in the gutter, one line tall: a taller button overhangs the row rather than growing it. renderLineAfter is called for every line; a node gets a full-width row under its line, at the body’s visible width however far the code scrolls, in the page’s direction and type. Both get the DiffLine (its type, text, old and new numbers) and its index.

Direction

Code reads left to right in every language, so the body is always dir="ltr", numbers on the left. The bar follows the page, and so do the words of the page’s own inside the body: folds, counts, “No changes” and the content under a line.