Text diff
Review prose edits inline or inside a standalone card, with suggestion popovers, keyboard shortcuts, and undo.
import { TextDiff } from '@uthereal/ui/text-diff'Text diff playground
Suggested changes
Overview
TextDiff presents suggested prose edits for word-level human review. Each pending change displays removed text (<del>) beside proposed text (<ins>) and opens an anchored suggestion popover to accept or reject the edit without mutating host data.
Use TextDiff for prose revisions, copyediting, and AI writing suggestions. Use Code (mode="diff") for line-based source code patches and Text editor for freeform authoring.
| Mode / Layout | Primary props | Behavior |
|---|---|---|
layout="card" | label, showToolbar | Standalone bordered review surface with a caption, inline prose changes, and footer navigation, bulk, and Undo. |
layout="inline" | showToolbar={false} | Unframed inline prose composed directly inside an existing paragraph or document surface. |
| Snapshot comparison | original, suggested | Derives word-level change segments with bounded diffWords (up to 20,000 characters each). |
| Explicit segments | segments, defaultDecisions | Renders host-prepared text and stable-ID change segments (with optional note) and exact-text initial state. |
Usage
'use client'
import { TextDiff } from '@uthereal/ui/text-diff'
export function DraftReview() {
return (
<TextDiff
label="Release note draft"
segments={[
{ kind: 'text', text: 'We ' },
{
kind: 'change',
id: 'verb',
before: 'utilize',
after: 'use',
note: 'Prefer a shorter, direct verb.',
},
{ kind: 'text', text: ' the updated summary.' },
]}
onReview={({ text, complete }) => {
if (complete) console.log('Reviewed text:', text)
}}
/>
)
}Supply original and suggested together to compute word-level changes automatically, or pass explicit segments. Raw snapshots take precedence when provided; clear both before switching an existing configuration back to segments. Export diffWords(original, suggested) for server-side preparation and reviewText(segments, decisions) to compute the current prose string.
Examples
Use <SchemaPlayground /> above to test the default card layout, toolbar controls, and popover actions. The examples below cover inline composition and snapshot diffing.
Inline document review
Set layout="inline" and showToolbar={false} to embed reviewable changes inside surrounding copy. Focus or click any pending change to open its anchored popover or decide from the keyboard.
Compare text snapshots
Pass original and suggested when the host holds two complete strings instead of precomputed change IDs.
Suggested changes
Decisions and undo
| Concept | Contract |
|---|---|
defaultDecisions | Serializable array of unique-ID { id, decision, before, after } entries; converts via reviewDecisionsFromEntries. |
decisions | React-only controlled record keyed by change ID (Record<id, { decision, before, after }>). |
| Exact-text binding | reconcileReview drops any stored or undone decision whose before or after no longer matches the current segment. |
| Undo history | Stores up to 200 snapshots in memory; Undo restores the previous decision map and re-validates against segments. |
| Host actions | acceptAction, rejectAction, and completeAction fire only after explicit user interaction, never on mount or edit. |
API reference
Serializable arguments and defaults come directly from textDiffConfigSchema.
| Argument | Type / default | Description |
|---|---|---|
original | string— | Original prose snapshot, up to 20,000 characters. Supply with suggested to derive word-level change segments automatically. |
suggested | string— | Proposed prose snapshot, up to 20,000 characters. Supply with original to derive word-level change segments automatically. |
label | stringDefault: "Suggested changes" | Accessible name of the prose review, shown as a caption above the text in card layout. |
segments | ReviewSegment[]Default: [] | Explicit unchanged text and stable before/after change segments (with optional note). Ignored when original and suggested are supplied. |
defaultDecisions | ReviewDecisionEntry[]Default: [] | Initial accepted or rejected choices as unique-ID entries bound to exact before and after text; later edits do not overwrite existing user decisions. |
acceptAction | string— | Host-registered action name sent with the change ID after accepting a change. |
rejectAction | string— | Host-registered action name sent with the change ID after rejecting a change. |
completeAction | string— | Host-registered action name sent with "complete" once the final pending change is resolved. |
layout | card | inlineDefault: "card" | card renders a bordered review surface with a label; inline renders unframed prose for document composition. |
showToolbar | booleanDefault: true | Show the footer toolbar with pending count, previous/next change navigation, bulk Accept all / Reject all, and Undo. |
recipe | studio | softDefault: "studio" | Visual treatment. Both recipes use the same interaction behavior. |
theme | TextDiffTheme— | Color mode, accent, and semantic design token overrides forwarded to the review surface, popover, and controls. |
motion | none | subtle | expressiveDefault: "subtle" | Animation preset for change settling and suggestion popovers; system reduced motion takes precedence. |
motionOptions | TextDiffMotionOptions— | Disclosure transition override for review state and popover motion (durations in milliseconds). |
dir | ltr | rtl— | Reading direction; omit to inherit the page direction. |
React integration
| Prop | Purpose |
|---|---|
decisions, onDecisionsChange | Controlled decision record and change callback; entries carry decision, before, and after. |
reviewRef | Imperative highlight(index), accept(index), reject(index), acceptAll(), rejectAll(), undo(), and reset() handle. |
onReview(result) | Reports { decisions, text, complete } after every user decision, bulk action, undo, or reset. |
onAction(action, itemId) | Receives acceptAction / rejectAction with the change ID, and completeAction with "complete". |
preset | Application defaults overridden by explicit instance props. |
tokens | --ui-* CSS custom properties applied to the root element and portalled suggestion popovers. |
ref, className, style, rest | Forwarded to the root <div> element (style takes precedence over theme and motion variables). |
Keyboard & accessibility
| Key | Interaction |
|---|---|
Tab | Moves focus across pending changes and toolbar controls. |
Space / click | Opens the anchored review popover for the focused change. |
Y, Enter | Accepts the focused or active change and advances focus to the next pending change. |
N, Backspace | Rejects the focused or active change and advances focus to the next pending change. |
Escape | Closes the active review popover without recording a decision. |
The root element sets role="region" in card layout (role="group" in inline layout) with aria-label={label}. All prose renders as escaped text. A pre-mounted role="status" region announces pending counts and completion; resolving the final change moves focus to Undo when the toolbar is visible. Keyboard shortcuts ignore events originating inside editable inputs or IME composition. Navigation chevrons automatically mirror in right-to-left (dir="rtl") layouts. Under forced-colors: active, additions underline, focus rings use Highlight, and shortcut hints switch to Canvas / CanvasText.
Motion and theming
Resolved changes collapse discarded words smoothly while kept words settle into normal prose; the toolbar status glyph transitions through the shared activity glyph swap, and suggestion popovers open on the anchored surface transition. Under prefers-reduced-motion: reduce, width collapse and travel are removed in favor of a 120ms opacity crossfade. Under motion="none", transitions and scroll jumps are immediate (0ms).
| Token group / key | Role |
|---|---|
core (surface, border, text) | Card frame, prose ink, muted deletion text, focus outline. |
control, borderStrong | Toolbar button sizing, hover underline, and popover controls. |
success, danger | Soft insertion and deletion tint bands (color-mix). |
radius, popupRadius, radiusSm | Card corner radius and portalled popover radius. |
shadowResting, shadowRaised | Popover elevation in light and dark modes. |
UtherealProvider supplies application-wide defaults. See Motion.
Composition
In agent compositions, TextDiff is a leaf component. User choices persist in host state at /components/instance-<id>/decisions and reconcile by stable change ID and exact before/after text. Register acceptAction, rejectAction, and completeAction on the host before referencing them.
{
"type": "TextDiff",
"props": {
"label": "Summary review",
"original": "The agent will utilize the notes to create a summary.",
"suggested": "The agent will use the notes to write a summary."
},
"children": []
}