Components
useDiffReview
DiffReview without its markup: the review’s state, keyboard and result, for your own design system.
Example
- @@ -1,6 +1,7 @@not decided
Added: import { ratelimit } from '@/lib/ratelimit'; - @@ -11,6 +12,15 @@not decided
Added: const ip = req.headers.get('x-forwarded-for') ?? 'anonymous';Added: const { success, reset } = await ratelimit.limit(ip);Added: if (!success) {Added: return new Response('Too many requests', {Added: status: 429,Added: headers: { 'Retry-After': String(Math.ceil((reset - Date.now()) / 1000)) },Added: });Added: }Added:
- @@ -18,7 +28,7 @@not decided
Removed: model: openai('gpt-4o'),Added: model: openai('gpt-4.1-mini'),
J and K move, A and R decide, C writes a note.
Interactive, and the same on the components page.
Installation
npm i signoff-ui aiimport { useDiffReview, reviewToolOutput } from 'signoff-ui';
// Once per app (with Tailwind v4: @import 'signoff-ui/tailwind.css'; in your CSS)
import 'signoff-ui/styles.css';npx shadcn@latest add https://signoff-ui.vercel.app/r/use-diff-review.jsonimport { useDiffReview } from '@/components/signoff-ui/use-diff-review';The styles are once per app; Getting started has the Tailwind v4 and plain CSS options.
Usage
function MyReview({ files, onSubmit }: { files: FileChange[]; onSubmit: (r: DiffReviewResult) => void }) {
const review = useDiffReview({ files, onSubmit });
return (
<section aria-label="Review" {...review.getRootProps()}>
{review.items.map((item, i) => (
<div key={item.id} {...review.getItemProps(i)}>
<MyHunk hunk={item.hunk} />
<button {...review.getDecisionProps(i, 'accepted')}>Keep</button>
<button {...review.getDecisionProps(i, 'rejected')}>Drop</button>
</div>
))}
{review.draft && <textarea aria-label="Comment" {...review.getDraftProps()} />}
<button {...review.getSubmitProps()}>Apply</button>
<p role="status">{review.announcement}</p>
</section>
);
}How it works
useDiffReview is DiffReview without its markup: the same parsing, decisions, comments, viewed files, unchanged context, keyboard and result, for a review drawn with your own components. DiffReview is this hook and its styled markup, so the two behave the same.
What to wire up
Spread getRootProps() on the element around the review: it holds the keyboard. Spread getItemProps(i) on each item's element: it brings a roving tabindex, the item's name and its decision. items lists everything to decide, in file order: each hunk, or a whole file when it has no hunks (binary, renamed or empty). The index the getters take is a position in that list. Render announcement in a polite live region: it says what each decision, selection, comment and viewed mark did.
Decisions
getDecisionProps(i, 'accepted') and getDecisionProps(i, 'rejected') are the two buttons for an item, with aria-pressed; pressing one again resets the item. getFileDecisionProps(fileId, decision) decides a whole file, and decideAll the whole review. Pass decisions and onDecisionsChange to control them, or defaultDecisions to start from some. counts has how many are accepted, rejected and pending. When a file's contents change under the review, its decisions, viewed mark and comments are dropped, as on DiffReview, and controlled ones are sent to onDecisionsChange, onViewedChange and onCommentsChange without them. That holds for a controlled value built as the component renders, such as { ...decisions }, too.
Comments
getLineNumberProps(i, line) selects a line on click, and Shift-click extends the selection in the same hunk; the keys select lines as in DiffReview. startComment() opens a draft on the selected lines, or on the item. Render the draft in a labeled textarea with getDraftProps(): it brings focus, the text and its keys (⌘ or Ctrl + Enter saves, Escape cancels). comments and onCommentsChange control them.
Files
files has each file's state: its id (the path, or path#2 for a path that repeats), its items, its decision, how many of its items are decided, whether it is viewed or folded away, and the unchanged lines around its hunks (gaps), which showContext shows. getViewedProps(fileId) is a file's "Viewed" checkbox, and getNavigatorItemProps(fileId) a button in a list of files that is one tab stop.
Applying
getSubmitProps() is the Apply button. It is aria-disabled while comparing is true, that is while a file is still diffed in a worker. onSubmit(result, { toPatch }) receives the same result as DiffReview's, once per state of the review, and result() and toPatch() read it at any time. reviewToolOutput(result) shapes it for the model.
Your own controls
Keys pressed inside an element with data-signoff-slot, or in a portal rendered from it such as a menu, are left to that element, so J, K, A and R don't act from your buttons there.
Large files
maxEditLength and diffWorker work as on DiffReview: past 2,000 lines added plus removed, a file's changed region is one hunk that replaces it, and a file that needs more than a few hundred edits is diffed in a worker. Its parsed is undefined until that is done.
API reference
Generated from the types the package ships, so it matches the version you install.
useDiffReview
The review without its markup: parsing (in a worker for large files), decisions, comments,
viewed files, unchanged context, the keyboard and the result, for teams that render their own.
DiffReview is this hook and its styled markup. Spread getRootProps() on the element that
contains the review, getItemProps(i) on each item's element, and render announcement in a
polite live region; the keys are DiffReview's.
useDiffReview({ files: changes, decisions: decisionsProp, defaultDecisions, onDecisionsChange, comments: commentsProp, defaultComments, onCommentsChange, viewed: viewedProp, defaultViewed, onViewedChange, onSubmit, readOnly, context, maxEditLength, diffWorker, autoAdvance, collapseViewed, labels }: UseDiffReviewOptions): UseDiffReviewResultParameter: UseDiffReviewOptions
filesrequiredreadonly FileChange[]The edits to review, one per file, with full contents (
oldContentandnewContent) or a unifiedpatch. Compared by content: a new array of the same edits, such as one written inline, is not diffed again. A file whose contents change starts its review over: its decisions, viewed mark and comments are dropped, and controlled ones are reported without them.decisionsReadonly<Record<string, HunkDecision>>Controlled decisions keyed by item id: a hunk's
${path}:${index}, or${path}:filefor a file with no hunks (binary, renamed or empty). A repeated path gets${path}#2,#3and so on, skipping a suffix another file's path already has.defaultDecisionsReadonly<Record<string, HunkDecision>>The decisions to start with, keyed like
decisions, when the review keeps its own: to restore a review in progress, say. Read on the first render only.onDecisionsChange(decisions: Record<string, HunkDecision>) => voidCalled with every decision, keyed like
decisions, whenever one changes: an item accepted, rejected or reset, or a whole file or the whole review decided at once.commentsreadonly DiffReviewComment[]Controlled comments.
defaultCommentsreadonly DiffReviewComment[]The comments to start with, when the review keeps its own. Read on the first render only.
onCommentsChange(comments: DiffReviewComment[]) => voidCalled with every comment whenever one is added, edited or removed.
viewedReadonly<Record<string, boolean>>Controlled "Viewed" marks, by file id (the path, or
path#2… for a repeated path).defaultViewedReadonly<Record<string, boolean>>The "Viewed" marks to start with, by file id, when the review keeps its own. Read on the first render only.
onViewedChange(viewed: Record<string, boolean>) => voidCalled with every "Viewed" mark, by file id, whenever one is set or cleared.
onSubmit(result: DiffReviewResult, review: { toPatch: () => string; }) => void | PromiseLike<void>Called with the reviewed result, and a
toPatchthat gives the accepted changes as a unified diff. Pending hunks are not applied. Fires once per review state, so a double click or a repeated ⌘/Ctrl+Enter sends one review: submitting again needs a changed decision, comment or viewed mark, changedfiles, the returned promise to settle, or the handler to throw. Files compare by content and the rest by value: an equal new array or object does not count as a change.readOnlybooleanDefaultfalseDecide nothing, comment on nothing: only read.
contextnumberDefault3Lines of context around each change.
maxEditLengthnumberDefault2000Past this many lines added plus lines removed in one file, its changed region (from the first changed line to the last) is shown as one hunk that replaces it, and the review says so. Diffing costs about the square of this number.
diffWorkerDiffWorkerFactory | falseWhere files too large to diff while rendering are diffed: by default in the package's own worker, which bundlers with worker support emit (webpack 5 and Next.js, Vite, Parcel). Pass a function that starts your own
Workerrunningsignoff-ui's diff worker, orfalseto diff them on the main thread after the first paint. Without a working worker the latter is the fallback.autoAdvancebooleanDefaulttrueAfter accepting or rejecting with the keyboard, move to the next undecided item.
collapseViewedbooleanDefaulttrueFold a file away once it is marked viewed.
labelsSignoffLabelsInputWords to use instead of the English defaults, for what the hook announces and names: see
SignoffLabelsProvider.
Returns: UseDiffReviewResult
filesrequiredDiffReviewFileState[]Every file, in the order of
files: its id, diff, items, decision, and viewed and folded state.itemsrequiredReviewItem[]Every item to decide, in file order;
indexin the getters refers to this list.activeIndexrequirednumberThe item in the tab order, focused by J and K.
countsrequired{ accepted: number; rejected: number; pending: number; total: number; }How many items are accepted, rejected and still pending, out of
total.comparingrequiredbooleanSome file is still being diffed in the background; applying waits for it.
decisionsrequiredReadonly<Record<string, HunkDecision>>The decisions, keyed by item id; an item missing from it is
pending.commentsrequiredreadonly DiffReviewComment[]The comments, in the order they were added.
viewedrequiredReadonly<Record<string, boolean>>The "Viewed" marks, by file id.
selectionrequiredDiffReviewSelectionThe lines selected in a hunk, with Shift and the arrows or a click on a line number.
draftrequiredDiffReviewDraftThe comment being written: render the editor (
getDraftProps()) while it is set.shownrequiredReadonly<Record<string, ShownContext>>Unchanged lines shown around hunks, by
${fileId}:${gap.before}.announcementrequiredstringWhat to announce: render it in a polite live region.
readOnlyrequiredbooleanThe
readOnlyoption. While it istruethe keys, comments andsubmitdo nothing: hide your decision buttons too, asDiffReviewdoes.isMacrequiredbooleantrueon a Mac, to print ⌘ rather than Ctrl in your shortcut hints.falseon the server and while hydrating.deciderequired(id: string, decision: HunkDecision, options?: { advance?: boolean; }) => voidDecide one item by id: accept, reject, or
pendingto reset it. Withadvance, focus moves on to the next undecided item, as A and R do withautoAdvance.decideFilerequired(fileId: string, decision: Exclude<HunkDecision, 'pending'>) => voidAccept or reject every item of a file, as Alt+A and Alt+R do.
decideAllrequired(decision: Exclude<HunkDecision, 'pending'>) => voidAccept or reject every item, as Shift+A and Shift+R do. Does nothing while a file is being compared.
setViewedrequired(fileId: string, viewed: boolean) => voidMark a file viewed, or clear the mark, as its checkbox and V do. With
collapseViewed, a viewed file folds away.setCollapsedrequired(fileId: string, collapsed: boolean) => voidFold a file away or unfold it, whatever its viewed mark. J and K skip a folded file's items.
focusItemrequired(index: number) => voidFocus an item by index (clamped), as J and K do.
activaterequired(index: number) => voidMake an item the one in the tab order, as focusing it does. Stable across renders.
registerItemrequired(id: string, el: HTMLElement | null) => voidRecord an item's element, for focus. Stable across renders.
focusFilerequired(fileId: string) => voidUnfold a file and focus its first item, as the file navigator does.
selectLinesrequired(itemId: string, from: number, to?: number) => voidSelect lines
from…toof a hunk item;todefaults tofrom.clearSelectionrequired() => voidClear the line selection, as Escape does.
startCommentrequired(options?: { itemId?: string; commentId?: string; }) => voidStart a comment: on the selected lines, else on the item (a hunk or a whole file), or edit one.
setDraftTextrequired(text: string) => voidSet the text of the comment being written, for an editor that doesn't use
getDraftProps().registerDraftrequired(el: HTMLTextAreaElement | null) => voidThe comment editor's element, for focus. Stable across renders.
saveDraftrequired() => voidSave the comment being written, or the edit of one, as ⌘/Ctrl+Enter in the editor does. With nothing written but spaces, it cancels instead.
cancelDraftrequired() => voidClose the comment editor without saving, as Escape does, and return focus to the item it was on.
removeCommentrequired(id: string) => voidDelete a comment by id, and return focus to the item it was on.
showContextrequired(fileId: string, gap: ContextGap, from: 'start' | 'end' | 'all') => voidShow more unchanged lines of a file's gap: from its start, its end, or all of it.
submitrequired() => voidSend the review to
onSubmit, once per review state.resultrequired() => DiffReviewResultThe review as
onSubmitreceives it, for the current state.toPatchrequired() => stringThe accepted changes as a unified diff.
getRootPropsrequired() => { tabIndex: -1; onKeyDown: (event: KeyboardEvent<HTMLElement>) => void; }The review's keyboard, for the element that contains it. Keys pressed inside an element with
data-signoff-slotin it, your own controls, are left to them, and so are keys from a portal rendered inside it, such as a menu.tabIndex: -1makes that element focusable, outside the tab order: Safari and every browser on iOS don't focus a button or a checkbox on click, so focus goes to the closest focusable element around it, this one rather than the page, and the keys keep working. Spread your owntabIndexafter it to replace it.getItemPropsrequired(index: number) => { ref: (el: HTMLElement | null) => void; role: 'group'; tabIndex: 0 | -1; 'aria-label': string; 'data-decision': HunkDecision; onFocus: () => void; }A focusable item: a roving tabindex, its accessible name and its decision.
getDecisionPropsrequired(index: number, decision: Exclude<HunkDecision, 'pending'>) => { type: 'button'; 'aria-pressed': boolean; onClick: () => void; }Accept or reject one item; pressing it again resets it.
getFileDecisionPropsrequired(fileId: string, decision: Exclude<HunkDecision, 'pending'>) => { type: 'button'; onClick: () => void; 'aria-keyshortcuts': string; }A button that accepts or rejects a whole file, with its shortcut, Alt+A or Alt+R, in
aria-keyshortcuts.getViewedPropsrequired(fileId: string) => { ref: (el: HTMLInputElement | null) => void; type: 'checkbox'; checked: boolean; onChange: (event: ChangeEvent<HTMLInputElement>) => void; 'aria-keyshortcuts': 'V'; }The checkbox that marks a file viewed, with V in
aria-keyshortcuts. Give it a label of your own.getLineNumberPropsrequired(index: number, line: number) => { onClick: (event: MouseEvent) => void; }Click a line number to select it; Shift-click extends the selection in the same hunk.
getDraftPropsrequired() => { ref: (el: HTMLTextAreaElement | null) => void; value: string; onChange: (event: ChangeEvent<HTMLTextAreaElement>) => void; onKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => void; }The comment editor's text area: its value, focus when it opens, and its keys (⌘/Ctrl+Enter saves, Escape cancels). Give it a label of your own.
getSubmitPropsrequired() => { type: 'button'; onClick: () => void; 'aria-disabled': true | undefined; 'aria-keyshortcuts': string; }The apply button: it calls
submit, isaria-disabledwhile a file is being compared, and has ⌘/Ctrl+Enter inaria-keyshortcuts.
DiffReviewFileState
A file in the review, as it stands now.
idrequiredstringThe file's id: its path, or
path#2… for a path that repeats.pathrequiredstringThe file's path, from its
FileChange.changerequiredFileChangeThe
FileChangeit comes from, as passed infiles.parsedrequiredParsedFileDiffundefinedwhile the file is diffed in the background ("Comparing…").itemsrequiredReviewItem[]What it asks to decide: its hunks, or the file as a whole.
decisionrequiredFileDecision'accepted' | 'rejected' | 'partial' | 'pending' | 'unchanged'accepted,rejectedorpendingwhen all its items are,partialfor a mix, andunchangedwith nothing to decide (or while it is being compared).decidedrequirednumberHow many of its items are decided.
viewedrequiredbooleanMarked "Viewed" by the reviewer.
collapsedrequiredbooleanFolded away: its items are skipped by J and K.
gapsrequiredContextGap[]Unchanged lines around its hunks that can be shown, when both contents are known.
DiffReviewSelection
Lines selected in a hunk, by index into hunk.lines.
itemrequiredReviewItemThe item the lines are in.
hunkrequiredDiffHunkThe hunk the lines are in:
item.hunk.anchorrequirednumberThe index the selection started at: Shift and the arrows extend it from here.
headrequirednumberThe index it ends at: the one Shift and the arrows move.
fromrequirednumberThe first selected index.
torequirednumberThe last selected index.
rangerequiredCommentRangeThe selected lines' numbers, in the proposed file or, for removed lines only, in the original, and with removed lines among lines in the proposed file, in the original too: what a comment on them records.
DiffReviewDraft
A comment being written: a new one, or an edit of commentId.
fileIdrequiredstringThe file it is on, by id.
targetrequiredDiffReviewComment['target']What it is on: lines, a hunk or a whole file.
textrequiredstringWhat has been written so far.
commentIdstringThe comment being edited; absent for a new one.
hunkIdstringThe hunk it is on, for
linesandhunk.rangeCommentRangeThe lines it is on, for
linesandhunk: the selected lines, or the hunk's changed lines.
ReviewItem
One thing to decide: a hunk, or a file with no hunks as a whole (binary, a rename, an empty file).
idrequiredstringThe decision key: the hunk's id, or
${fileId}:file.filerequiredParsedFileDiffThe parsed file it belongs to.
hunkrequiredDiffHunkundefinedfor a whole file.
Accessibility
- The hook has no markup of its own. Its getters bring what
DiffReviewrelies on: a roving tabindex and a name for each item (getItemProps),aria-pressedon decision buttons,aria-keyshortcuts,aria-disabledon Apply while files are being compared, and a roving tabindex for a file list (getNavigatorItemProps). - Every key
DiffReviewhas works oncegetRootProps()is on the element around the review andgetItemProps(i)on each item. Renderannouncementin a polite live region: it says what each decision, selection, comment and viewed mark did. - Give the comment editor a label:
getDraftProps()gives it focus, its value and its keys (⌘ or Ctrl + Enter saves, Escape cancels).