Skip to content
signoff-uiv0.14.1

Docs

Components

useDiffReview

DiffReview without its markup: the review’s state, keyboard and result, for your own design system.

Example

  1. @@ -1,6 +1,7 @@not decided
    Added: import { ratelimit } from '@/lib/ratelimit';
  2. @@ -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:  
  3. @@ -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 ai
TSX
import { useDiffReview, reviewToolOutput } from 'signoff-ui';
// Once per app (with Tailwind v4: @import 'signoff-ui/tailwind.css'; in your CSS)
import 'signoff-ui/styles.css';

The styles are once per app; Getting started has the Tailwind v4 and plain CSS options.

Usage

TSX
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.

TypeScript
useDiffReview({ files: changes, decisions: decisionsProp, defaultDecisions, onDecisionsChange, comments: commentsProp, defaultComments, onCommentsChange, viewed: viewedProp, defaultViewed, onViewedChange, onSubmit, readOnly, context, maxEditLength, diffWorker, autoAdvance, collapseViewed, labels }: UseDiffReviewOptions): UseDiffReviewResult

Parameter: UseDiffReviewOptions

filesrequired
readonly FileChange[]

The edits to review, one per file, with full contents (oldContent and newContent) or a unified patch. 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.

decisions
Readonly<Record<string, HunkDecision>>

Controlled decisions keyed by item id: a hunk's ${path}:${index}, or ${path}:file for a file with no hunks (binary, renamed or empty). A repeated path gets ${path}#2, #3 and so on, skipping a suffix another file's path already has.

defaultDecisions
Readonly<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>) => void

Called 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.

comments
readonly DiffReviewComment[]

Controlled comments.

defaultComments
readonly DiffReviewComment[]

The comments to start with, when the review keeps its own. Read on the first render only.

onCommentsChange
(comments: DiffReviewComment[]) => void

Called with every comment whenever one is added, edited or removed.

viewed
Readonly<Record<string, boolean>>

Controlled "Viewed" marks, by file id (the path, or path#2… for a repeated path).

defaultViewed
Readonly<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>) => void

Called 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 toPatch that 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, changed files, 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.

readOnly
booleanDefault false

Decide nothing, comment on nothing: only read.

context
numberDefault 3

Lines of context around each change.

maxEditLength
numberDefault 2000

Past 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.

diffWorker
DiffWorkerFactory | false

Where 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 Worker running signoff-ui's diff worker, or false to diff them on the main thread after the first paint. Without a working worker the latter is the fallback.

autoAdvance
booleanDefault true

After accepting or rejecting with the keyboard, move to the next undecided item.

collapseViewed
booleanDefault true

Fold a file away once it is marked viewed.

labels
SignoffLabelsInput

Words to use instead of the English defaults, for what the hook announces and names: see SignoffLabelsProvider.

Returns: UseDiffReviewResult

filesrequired
DiffReviewFileState[]

Every file, in the order of files: its id, diff, items, decision, and viewed and folded state.

itemsrequired
ReviewItem[]

Every item to decide, in file order; index in the getters refers to this list.

activeIndexrequired
number

The 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.

comparingrequired
boolean

Some file is still being diffed in the background; applying waits for it.

decisionsrequired
Readonly<Record<string, HunkDecision>>

The decisions, keyed by item id; an item missing from it is pending.

commentsrequired
readonly DiffReviewComment[]

The comments, in the order they were added.

viewedrequired
Readonly<Record<string, boolean>>

The "Viewed" marks, by file id.

selectionrequired
DiffReviewSelection

The lines selected in a hunk, with Shift and the arrows or a click on a line number.

draftrequired
DiffReviewDraft

The comment being written: render the editor (getDraftProps()) while it is set.

shownrequired
Readonly<Record<string, ShownContext>>

Unchanged lines shown around hunks, by ${fileId}:${gap.before}.

announcementrequired
string

What to announce: render it in a polite live region.

readOnlyrequired
boolean

The readOnly option. While it is true the keys, comments and submit do nothing: hide your decision buttons too, as DiffReview does.

isMacrequired
boolean

true on a Mac, to print ⌘ rather than Ctrl in your shortcut hints. false on the server and while hydrating.

deciderequired
(id: string, decision: HunkDecision, options?: { advance?: boolean; }) => void

Decide one item by id: accept, reject, or pending to reset it. With advance, focus moves on to the next undecided item, as A and R do with autoAdvance.

decideFilerequired
(fileId: string, decision: Exclude<HunkDecision, 'pending'>) => void

Accept or reject every item of a file, as Alt+A and Alt+R do.

decideAllrequired
(decision: Exclude<HunkDecision, 'pending'>) => void

Accept or reject every item, as Shift+A and Shift+R do. Does nothing while a file is being compared.

setViewedrequired
(fileId: string, viewed: boolean) => void

Mark 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) => void

Fold a file away or unfold it, whatever its viewed mark. J and K skip a folded file's items.

focusItemrequired
(index: number) => void

Focus an item by index (clamped), as J and K do.

activaterequired
(index: number) => void

Make an item the one in the tab order, as focusing it does. Stable across renders.

registerItemrequired
(id: string, el: HTMLElement | null) => void

Record an item's element, for focus. Stable across renders.

focusFilerequired
(fileId: string) => void

Unfold a file and focus its first item, as the file navigator does.

selectLinesrequired
(itemId: string, from: number, to?: number) => void

Select lines from…to of a hunk item; to defaults to from.

clearSelectionrequired
() => void

Clear the line selection, as Escape does.

startCommentrequired
(options?: { itemId?: string; commentId?: string; }) => void

Start a comment: on the selected lines, else on the item (a hunk or a whole file), or edit one.

setDraftTextrequired
(text: string) => void

Set the text of the comment being written, for an editor that doesn't use getDraftProps().

registerDraftrequired
(el: HTMLTextAreaElement | null) => void

The comment editor's element, for focus. Stable across renders.

saveDraftrequired
() => void

Save 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
() => void

Close the comment editor without saving, as Escape does, and return focus to the item it was on.

removeCommentrequired
(id: string) => void

Delete a comment by id, and return focus to the item it was on.

showContextrequired
(fileId: string, gap: ContextGap, from: 'start' | 'end' | 'all') => void

Show more unchanged lines of a file's gap: from its start, its end, or all of it.

submitrequired
() => void

Send the review to onSubmit, once per review state.

resultrequired
() => DiffReviewResult

The review as onSubmit receives it, for the current state.

toPatchrequired
() => string

The 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-slot in it, your own controls, are left to them, and so are keys from a portal rendered inside it, such as a menu. tabIndex: -1 makes 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 own tabIndex after 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, is aria-disabled while a file is being compared, and has ⌘/Ctrl+Enter in aria-keyshortcuts.

getNavigatorItemPropsrequired
(fileId: string) => { type: 'button'; tabIndex: 0 | -1; ref: (el: HTMLButtonElement | null) => void; 'aria-current': true | undefined; onClick: () => void; onKeyDown: (event: KeyboardEvent<HTMLButtonElement>) => void; }

One button per file in a navigator list, with a roving tabindex: ↑ and ↓ move, Enter goes.

DiffReviewFileState

A file in the review, as it stands now.

idrequired
string

The file's id: its path, or path#2… for a path that repeats.

pathrequired
string

The file's path, from its FileChange.

changerequired
FileChange

The FileChange it comes from, as passed in files.

parsedrequired
ParsedFileDiff

undefined while the file is diffed in the background ("Comparing…").

itemsrequired
ReviewItem[]

What it asks to decide: its hunks, or the file as a whole.

decisionrequired
FileDecision
'accepted' | 'rejected' | 'partial' | 'pending' | 'unchanged'

accepted, rejected or pending when all its items are, partial for a mix, and unchanged with nothing to decide (or while it is being compared).

decidedrequired
number

How many of its items are decided.

viewedrequired
boolean

Marked "Viewed" by the reviewer.

collapsedrequired
boolean

Folded away: its items are skipped by J and K.

gapsrequired
ContextGap[]

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.

itemrequired
ReviewItem

The item the lines are in.

hunkrequired
DiffHunk

The hunk the lines are in: item.hunk.

anchorrequired
number

The index the selection started at: Shift and the arrows extend it from here.

headrequired
number

The index it ends at: the one Shift and the arrows move.

fromrequired
number

The first selected index.

torequired
number

The last selected index.

rangerequired
CommentRange

The 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.

fileIdrequired
string

The file it is on, by id.

targetrequired
DiffReviewComment['target']

What it is on: lines, a hunk or a whole file.

textrequired
string

What has been written so far.

commentId
string

The comment being edited; absent for a new one.

hunkId
string

The hunk it is on, for lines and hunk.

range
CommentRange

The lines it is on, for lines and hunk: 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).

idrequired
string

The decision key: the hunk's id, or ${fileId}:file.

filerequired
ParsedFileDiff

The parsed file it belongs to.

hunkrequired
DiffHunk

undefined for a whole file.

Accessibility

  • The hook has no markup of its own. Its getters bring what DiffReview relies on: a roving tabindex and a name for each item (getItemProps), aria-pressed on decision buttons, aria-keyshortcuts, aria-disabled on Apply while files are being compared, and a roving tabindex for a file list (getNavigatorItemProps).
  • Every key DiffReview has works once getRootProps() is on the element around the review and getItemProps(i) on each item. Render announcement in 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).