DiffViewer
Compare two versions of a text, unified or side by side, with changed words highlighted and unchanged lines folded.
Status: beta. Category: Viewers.
import { DiffViewer } from '@glyphui/viewers/diff'
Exports: DiffViewer, diffViewerStyles, computeDiff, toSplitRows.
Notes
- Install the optional peer dependency first: npm install diff.
- Each changed line says whether it was added or removed to screen readers; the header announces the totals.
- Lines that were mostly rewritten are shown whole rather than word by word, which reads better.
DiffViewer props
| Prop | Type | Default | Description |
|---|---|---|---|
newValue (required) | string | The text after the change. | |
oldValue (required) | string | The text before the change. | |
bordered | boolean | Draw the viewer as a bordered card. Default true. | |
className | string | ||
context | number | Unchanged lines kept around each change; longer stretches fold. Default 3. | |
defaultView | "split" | "unified" | unified (one column) or split (side by side). Users can switch. Default unified. | |
fileName | string | Shown in the header, e.g. the file path. | |
height | string | number | Height of the viewer; content scrolls inside. | |
wrap | boolean | Wrap long lines. Default false. |
Example: A code change
Unified view with folded context.
import { DiffViewer } from '@glyphui/viewers/diff'
const before = `export async function remind(invoice: Invoice) {
const days = Math.round((Date.now() - invoice.dueDate.getTime()) / 86_400_000)
if (days > 0) {
await sendEmail(invoice.id, \`Your invoice is \${days} days overdue\`)
}
}
export function isOverdue(invoice: Invoice) {
return invoice.dueDate.getTime() < Date.now()
}
export function label(invoice: Invoice) {
return invoice.id
}
`
const after = `export async function remind(invoice: Invoice): Promise<boolean> {
if (!invoice.remindersEnabled) return false
const days = Math.round((Date.now() - invoice.dueDate.getTime()) / DAY)
if (days <= 0) return false
await sendEmail(invoice.id, \`Your invoice is \${days} days overdue\`)
return true
}
export function isOverdue(invoice: Invoice) {
return invoice.dueDate.getTime() < Date.now()
}
export function label(invoice: Invoice) {
return \`\${invoice.id} · \${invoice.customer}\`
}
`
export default function Example() {
return (
<DiffViewer
oldValue={before}
newValue={after}
fileName="src/reminders.ts"
context={1}
className="w-full"
/>
)
}
Example: Side by side
Edits to a policy, compared in two columns.
import { DiffViewer } from '@glyphui/viewers/diff'
const before = `Refunds
Customers can ask for a refund within 14 days of payment.
Refunds go back to the original payment method.
Annual plans are refunded in full.`
const after = `Refunds
Customers can ask for a refund within 30 days of payment.
Refunds go back to the original payment method within 5 business days.
Annual plans are refunded for the unused months.`
export default function Example() {
return (
<DiffViewer
oldValue={before}
newValue={after}
fileName="refund-policy.md"
defaultView="split"
wrap
className="w-full"
/>
)
}
Live page: https://glyphui.xyz/components/diff-viewer