All files / diff-viewer/src DiffViewer.tsx

100% Statements 144/144
98.47% Branches 129/131
100% Functions 32/32
100% Lines 129/129

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746                                                                          2x                 354x 352x 350x                                                                                                                                                                                             2x       76x 78x   87x                                           177x   177x 108x 1x 1x   107x 1x 1x   106x 105x 105x   1x 1x 1x 1x 108x     177x                             2x 2x       119x                                                   177x   177x 134x 134x   121x 120x 120x       177x                                                                                                                                     177x       177x 177x                           177x 177x 177x   177x 123x 120x     123x                             177x       177x   177x 177x                 177x                           177x               18x 18x 18x   18x           19x       18x         177x   177x                                   177x 142x   131x   131x 150x 131x 46050x     19x 197x         131x 252x 101x 101x     252x   252x 101x 85x   101x 1x       131x 126x 126x 126x     131x     177x 177x           177x 177x     177x 177x   177x             177x 134x 1x                                             177x   177x 108x 108x 96x 4x   96x   1x   96x 96x 96x 96x 96x       177x 141x 141x 141x     1x         1x       177x               177x 11x 11x                     4x                     166x 5x                           161x                                                                                                                                               6530x 6530x 6530x 109x                     18x                     6421x 6098x                         323x                                                
"use client";
 
/**
 * `<DiffViewer />` — the React shell (SPEC §3.3, §4.5).
 *
 * The component is a *rendering of the model and nothing more*: it computes
 * a {@link DiffResult} (or takes one), flattens it into an explicit row
 * list, and draws that list. All the diff intelligence lives in
 * `./headless`; everything here is presentation, expansion state, and
 * accessibility.
 *
 * The row list is deliberately materialised rather than nested-mapped: it is
 * what makes `aria-rowcount`/`aria-rowindex` truthful, and it is the array
 * Phase 3's virtualization will window over.
 */
 
import {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
  type ReactNode,
} from "react";
 
import styles from "./DiffViewer.module.scss";
import { Gap, type ExpandDirection, type GapLabels } from "./components/Gap";
import {
  SplitRowView,
  UnifiedRowView,
  cx,
  type RenderContent,
  type RowClassNames,
} from "./components/Row";
import { observeSize, useVirtualRows } from "./components/useVirtualRows";
import { computeDiff } from "./diff/computeDiff";
import { resolveOptions, type DiffLine, type DiffOptions, type DiffResult } from "./types";
import { alignRows, type SplitRow } from "./view/rows";
 
/**
 * Coerce a numeric prop, keeping `Infinity` only where it is meaningful.
 *
 * Mirrors the headless `toExtent`/`toFinite` split (SPEC resolved decision
 * #8) for the two props that never reach `resolveOptions`.
 */
function toPositive(value: number | undefined, fallback: number, allowInfinity = false): number {
  if (typeof value !== "number" || Number.isNaN(value)) return fallback;
  if (!Number.isFinite(value)) return allowInfinity && value > 0 ? value : fallback;
  return value;
}
 
/** Every user-facing string, so the viewer can be localised (SPEC §3.4). */
export type DiffViewerLabels = Partial<{
  /** Visible text on the "reveal from the top of the gap" control. */
  expandUp: string;
  /** Visible text on the "reveal from the bottom of the gap" control. */
  expandDown: string;
  /** Visible text on the "reveal everything" control. */
  expandAll: string;
  /** Accessible name for the up control — the visible text is only a glyph. */
  ariaExpandUp: (count: number) => string;
  /** Accessible name for the down control. */
  ariaExpandDown: (count: number) => string;
  /** Accessible name for the expand-all control. */
  ariaExpandAll: string;
  /** Receives the number of hidden lines. */
  hiddenLines: (count: number) => string;
  /** Accessible name for the diff table itself. */
  tableLabel: string;
  /** Accessible name for the horizontally scrollable code region. */
  scrollRegionLabel: string;
  noChanges: string;
  noChangesDetail: string;
  tooLarge: string;
  tooDifferent: string;
  diffAnyway: string;
  added: string;
  removed: string;
  /** Receives the stats; renders the header summary. */
  stats: (stats: { added: number; removed: number; unchanged: number }) => ReactNode;
}>;
 
/** Class-name slots (SPEC §4.5). */
export type DiffViewerClassNames = Partial<
  Record<
    "root" | "header" | "row" | "gutter" | "lineNumber" | "content" | "gap" | "marker",
    string
  >
>;
 
export interface DiffViewerProps extends DiffOptions {
  /** The "before" document. Ignored when `diff` is supplied. */
  oldText?: string;
  /** The "after" document. Ignored when `diff` is supplied. */
  newText?: string;
  /**
   * A pre-computed model — skips `computeDiff` entirely (e.g. computed in a
   * worker or on the server). Expansion still works: the hidden lines travel
   * inside the model (SPEC resolved decision #11).
   */
  diff?: DiffResult;
  /** @default "split" */
  view?: "split" | "unified";
  /** @default true */
  showLineNumbers?: boolean;
  /**
   * Wrap long lines instead of scrolling horizontally.
   * @default false
   */
  wrap?: boolean;
  /**
   * Row count above which only the visible window is rendered (SPEC §3.3).
   *
   * Virtualization needs a bounded viewport to window against, so above the
   * threshold the component also caps its own height
   * (`--usefy-diff-max-height`, default `70vh`) and scrolls internally.
   * Below it the diff renders in full and grows with its content.
   *
   * `Infinity` disables windowing; so does `wrap` (resolved decision #6).
   * @default 200
   */
  virtualizeThreshold?: number;
  /**
   * Height of one row in pixels — the unit windowing counts in.
   *
   * Must match the rendered row height, which is why it is written to
   * `--usefy-diff-row-height` rather than merely assumed.
   * @default 22
   */
  rowHeight?: number;
  /** Reveal every collapsed region on first render. @default false */
  defaultExpandAll?: boolean;
  /** The syntax-highlighting seam (SPEC §4.5). */
  renderContent?: RenderContent;
  labels?: DiffViewerLabels;
  /** `"system"` follows `prefers-color-scheme`. @default "system" */
  theme?: "light" | "dark" | "system";
  classNames?: DiffViewerClassNames;
  className?: string;
  /** Fired after a gap is expanded, with how many lines were revealed. */
  onExpand?: (hunkIndex: number, lines: number) => void;
}
 
const DEFAULT_LABELS = {
  expandUp: "↑ {count}",
  expandDown: "↓ {count}",
  expandAll: "Expand all",
  ariaExpandUp: (count: number) => `Expand ${count} lines above`,
  ariaExpandDown: (count: number) => `Expand ${count} lines below`,
  ariaExpandAll: "Expand all hidden lines",
  hiddenLines: (count: number) => `⋯ ${count} unchanged ${count === 1 ? "line" : "lines"}`,
  tableLabel: "Diff",
  scrollRegionLabel: "Diff content, scrollable horizontally",
  noChanges: "No changes",
  noChangesDetail: "The two versions are identical.",
  tooLarge: "This diff is too large to display.",
  tooDifferent: "These two versions have too little in common to diff quickly.",
  diffAnyway: "Diff anyway",
  added: "Added",
  removed: "Removed",
} as const;
 
/**
 * Resolve the effective dark-mode boolean for the chosen theme.
 *
 * `"system"` is resolved in **CSS** first (a `prefers-color-scheme` media
 * query in the stylesheet), so the very first paint — including a server
 * render, and including a page with JS disabled — is already correct. This
 * hook exists only so a *live* OS theme change repaints; it therefore starts
 * from `false` for `"system"` and never causes the initial flash.
 */
function useResolvedDark(theme: "light" | "dark" | "system"): boolean {
  const [dark, setDark] = useState(theme === "dark");
 
  useEffect(() => {
    if (theme === "light") {
      setDark(false);
      return;
    }
    if (theme === "dark") {
      setDark(true);
      return;
    }
    if (typeof window === "undefined" || !window.matchMedia) {
      setDark(false);
      return;
    }
    const mq = window.matchMedia("(prefers-color-scheme: dark)");
    const update = () => setDark(mq.matches);
    update();
    mq.addEventListener?.("change", update);
    return () => mq.removeEventListener?.("change", update);
  }, [theme]);
 
  return dark;
}
 
/**
 * How much of one collapsed region has been revealed.
 *
 * `top` counts lines revealed from the start of the run, `bottom` from the
 * end, so the remaining gap marker stays *between* them — the shape every
 * code-review tool uses.
 */
interface Revealed {
  top: number;
  bottom: number;
}
 
const NOTHING_REVEALED: Revealed = { top: 0, bottom: 0 };
const NO_REVEALS: Record<string, Revealed> = {};
 
/** Key for one expandable region: which hunk, and which side of it. */
function regionKey(hunkIndex: number, side: "before" | "after"): string {
  return `${hunkIndex}:${side}`;
}
 
/** One rendered table row. The list of these is the render plan. */
type RenderRow =
  | { kind: "split"; key: string; row: SplitRow }
  | { kind: "unified"; key: string; line: DiffLine }
  | {
      kind: "gap";
      key: string;
      hunkIndex: number;
      side: "before" | "after";
      hidden: number;
      state: Revealed;
    };
 
/**
 * Track whether the scroll container actually overflows.
 *
 * A scrollable region has to be keyboard-reachable or a keyboard-only user
 * simply cannot see the right-hand side of a long line (Chromium only
 * scrolls a container that can take focus). But an always-tabbable div adds
 * a dead tab stop to every short diff, so the affordance is applied only
 * when there is something to scroll.
 */
function useOverflowing(ref: React.RefObject<HTMLElement | null>, deps: unknown[]): boolean {
  const [overflowing, setOverflowing] = useState(false);
 
  useEffect(() => {
    const node = ref.current;
    if (!node) return;
 
    const measure = () => setOverflowing(node.scrollWidth > node.clientWidth + 1);
    measure();
    return observeSize(node, measure);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, deps);
 
  return overflowing;
}
 
/**
 * Render a text diff as a side-by-side or unified table.
 *
 * ## What it renders
 *
 * A real `<table>`: rows are `<tr>`, the first line-number cell of each row
 * is a `<th scope="row">` and everything else is a `<td>`, so assistive
 * technology can navigate the diff as the tabular data it is (SPEC §8).
 * Every changed row carries a visually-hidden "Added"/"Removed" label **and**
 * a `+`/`−` gutter glyph, so the change type is never signalled by colour
 * alone. `aria-rowcount`/`aria-rowindex` describe the rows actually drawn.
 *
 * Content always renders as **text nodes** — never `dangerouslySetInnerHTML`
 * (SPEC §9). Pass `renderContent` to plug in a syntax highlighter; that is
 * explicitly your trust boundary.
 *
 * ## States
 *
 * - **Changes** — the normal table.
 * - **No changes** — an explicit message, not a blank box.
 * - **Refused** — when a guard trips, an honest explanation that distinguishes
 *   "too large" (`truncatedReason: "size"`) from "too different"
 *   (`"complexity"`), plus a "Diff anyway" button that retries with the
 *   guards off. That approval is scoped to the inputs that earned it: change
 *   the documents and the guards are back on.
 *
 * @example
 * ```tsx
 * import { DiffViewer } from "@usefy/diff-viewer";
 * import "@usefy/diff-viewer/styles.css";
 *
 * <DiffViewer oldText={before} newText={after} view="split" />
 * ```
 *
 * @example
 * ```tsx
 * // Bring your own highlighter — the seam hands you plain strings.
 * <DiffViewer
 *   oldText={before}
 *   newText={after}
 *   renderContent={({ text, segments }) =>
 *     segments ? segments.map((s, i) => <Token key={i} {...s} />) : <Token text={text} />
 *   }
 * />
 * ```
 */
export function DiffViewer({
  oldText = "",
  newText = "",
  diff,
  view = "split",
  showLineNumbers = true,
  wrap = false,
  virtualizeThreshold = 200,
  rowHeight = 22,
  defaultExpandAll = false,
  renderContent,
  labels,
  theme = "system",
  classNames = {},
  className,
  onExpand,
  ...diffOptions
}: DiffViewerProps): ReactNode {
  const isDark = useResolvedDark(theme);
 
  // The option object is spread from props, so memoise on the resolved
  // values rather than on an identity that changes every render.
  const resolved = resolveOptions(diffOptions);
  const optionsKey = JSON.stringify(resolved);
 
  /**
   * A token that changes whenever the *inputs* change.
   *
   * "Diff anyway" is approval for one specific pair of documents, so it is
   * stored as the token it was granted for rather than as a bare boolean.
   * A bare boolean stayed `true` across a document swap, and the next
   * refused pair would then be diffed with every guard set to `Infinity` —
   * re-opening the multi-second synchronous freeze the cost guard exists to
   * prevent, inside `useMemo`, during render. Comparing tokens resolves
   * `forced` back to `false` in the *same* render the inputs change, before
   * anything is computed.
   */
  const inputToken = useMemo(() => ({}), [oldText, newText, diff, optionsKey]);
  const [forcedFor, setForcedFor] = useState<object | null>(null);
  const forced = forcedFor === inputToken;
 
  const model = useMemo(() => {
    if (diff) return diff;
    const options: DiffOptions = forced
      ? { ...diffOptions, maxLines: Infinity, maxBytes: Infinity, maxEditDistance: Infinity }
      : diffOptions;
    return computeDiff(oldText, newText, options);
    // `optionsKey` stands in for the spread options object.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [diff, oldText, newText, optionsKey, forced]);
 
  /**
   * Expansion state, held together with the model it belongs to.
   *
   * Keys are `hunkIndex:side`, which say nothing about *which document* they
   * came from — so carrying the map across a model change made a fresh diff
   * render already-expanded, and a stale `top` larger than the new hidden
   * run drove the remaining count negative and made the gap vanish entirely.
   * Storing the model alongside makes a mismatch impossible to miss, with no
   * effect and no extra render.
   */
  const [reveal, setReveal] = useState<{
    model: DiffResult | null;
    map: Record<string, Revealed>;
  }>({ model: null, map: NO_REVEALS });
  const revealed = reveal.model === model ? reveal.map : NO_REVEALS;
 
  const text = { ...DEFAULT_LABELS, ...labels };
  const gapLabels: GapLabels = {
    expandUp: text.expandUp,
    expandDown: text.expandDown,
    expandAll: text.expandAll,
    ariaExpandUp: text.ariaExpandUp,
    ariaExpandDown: text.ariaExpandDown,
    ariaExpandAll: text.ariaExpandAll,
    hiddenLines: text.hiddenLines,
  };
  const rowLabels = { added: text.added, removed: text.removed };
 
  /**
   * Reveal part of a collapsed region.
   *
   * Everything is derived from the state the clicked gap actually rendered
   * with — nothing is computed inside the state updater. StrictMode invokes
   * updaters twice, so an updater that also decided how many lines to report
   * would double-fire `onExpand`; keeping the updater a pure merge makes
   * that impossible by construction.
   *
   * `remaining` is always ≥ 1 here: `Gap` renders nothing when there is
   * nothing left to reveal, so there is no button to click.
   */
  const handleExpand = useCallback(
    (
      hunkIndex: number,
      side: "before" | "after",
      current: Revealed,
      remaining: number,
      direction: ExpandDirection,
    ) => {
      const key = regionKey(hunkIndex, side);
      const step = Math.min(resolved.expandStep, remaining);
      const lines = direction === "all" ? remaining : step;
      const next: Revealed =
        direction === "all"
          ? { top: current.top + remaining, bottom: current.bottom }
          : direction === "up"
            ? { top: current.top + step, bottom: current.bottom }
            : { top: current.top, bottom: current.bottom + step };
 
      setReveal((previous) => ({
        model,
        map: { ...(previous.model === model ? previous.map : NO_REVEALS), [key]: next },
      }));
      onExpand?.(hunkIndex, lines);
    },
    [model, onExpand, resolved.expandStep],
  );
 
  const columnCount = view === "split" ? (showLineNumbers ? 6 : 4) : showLineNumbers ? 4 : 2;
 
  const rootClass = cx(
    styles.root,
    isDark && styles.dark,
    theme === "light" && styles.light,
    wrap && styles.wrap,
    classNames.root,
    className,
  );
 
  /* ---------------------------------------------------------------------- */
  /* The render plan                                                        */
  /* ---------------------------------------------------------------------- */
 
  /**
   * Flatten the model plus the current expansion state into the exact list
   * of rows to draw. Built even for the guarded states (where it is empty)
   * so the hook order never changes.
   */
  const rows = useMemo<RenderRow[]>(() => {
    if (model.truncated) return [];
 
    const out: RenderRow[] = [];
 
    const pushLines = (lines: readonly DiffLine[], keyPrefix: string) => {
      if (view === "split") {
        alignRows(lines).forEach((row, index) =>
          out.push({ kind: "split", key: `${keyPrefix}-${index}`, row }),
        );
      } else {
        lines.forEach((line, index) =>
          out.push({ kind: "unified", key: `${keyPrefix}-${index}`, line }),
        );
      }
    };
 
    const pushRegion = (hidden: DiffLine[], hunkIndex: number, side: "before" | "after") => {
      if (hidden.length === 0) return;
      const key = regionKey(hunkIndex, side);
      const state = defaultExpandAll
        ? { top: hidden.length, bottom: 0 }
        : (revealed[key] ?? NOTHING_REVEALED);
      const remaining = Math.max(0, hidden.length - state.top - state.bottom);
 
      if (state.top > 0) pushLines(hidden.slice(0, state.top), `${key}-top`);
      if (remaining > 0) {
        out.push({ kind: "gap", key, hunkIndex, side, hidden: remaining, state });
      }
      if (state.bottom > 0) {
        pushLines(hidden.slice(hidden.length - state.bottom), `${key}-bottom`);
      }
    };
 
    model.hunks.forEach((hunk, hunkIndex) => {
      pushRegion(hunk.hiddenBefore, hunkIndex, "before");
      pushLines(hunk.lines, `hunk-${hunkIndex}`);
      pushRegion(hunk.hiddenAfter, hunkIndex, "after");
    });
 
    return out;
  }, [model, view, revealed, defaultExpandAll]);
 
  const scrollerRef = useRef<HTMLDivElement | null>(null);
  const overflowing = useOverflowing(scrollerRef, [rows, view, showLineNumbers, wrap]);
 
  /* ---------------------------------------------------------------------- */
  /* Virtualization (SPEC §3.3, resolved decision #6)                       */
  /* ---------------------------------------------------------------------- */
 
  const safeRowHeight = Math.max(1, toPositive(rowHeight, 22));
  const threshold = Math.max(0, toPositive(virtualizeThreshold, 200, true));
  // `wrap` makes rows variable-height, which fixed-height windowing cannot
  // model — measured virtualization is a different, much larger problem.
  const wantsVirtual = rows.length > threshold;
  const virtualEnabled = wantsVirtual && !wrap;
 
  const rowWindow = useVirtualRows({
    enabled: virtualEnabled,
    total: rows.length,
    rowHeight: safeRowHeight,
    scrollRef: scrollerRef,
  });
 
  useEffect(() => {
    if (wrap && wantsVirtual && typeof console !== "undefined") {
      console.warn(
        `[@usefy/diff-viewer] wrap is on, so virtualization is disabled and all ${rows.length} rows are in the DOM. ` +
          "Wrapped rows have no fixed height, which fixed-height windowing cannot model (SPEC resolved decision #6). " +
          "Turn off wrap for large diffs.",
      );
    }
  }, [wrap, wantsVirtual, rows.length]);
 
  /**
   * Keep the keyboard somewhere sensible when the focused control is
   * windowed out — but never yank focus a user has deliberately moved away.
   *
   * Scrolling an expander out of the window unmounts it, and the browser
   * drops focus to `<body>`; catching that in the scroll container keeps a
   * keyboard user in the diff with arrow-key scrolling intact.
   *
   * The trap this avoids: a stale "focus was inside" flag would let an
   * *unrelated* resize (which also moves the window) steal focus off `<body>`
   * even though the user chose to leave. The discriminator is
   * `isConnected` — when a user tabs or clicks away the element stays in the
   * DOM, and `focusout` clears the ref; when windowing removes it the element
   * is already gone, so the ref survives for the window effect to act on.
   */
  const focusedInside = useRef<HTMLElement | null>(null);
 
  useEffect(() => {
    const node = scrollerRef.current;
    if (!node) return;
    const remember = (e: FocusEvent) => {
      focusedInside.current = e.target as HTMLElement;
    };
    const forget = (e: FocusEvent) => {
      // Still in the DOM ⇒ a real focus move, not an unmount ⇒ don't chase it.
      Eif ((e.target as HTMLElement).isConnected) focusedInside.current = null;
    };
    node.addEventListener("focusin", remember);
    node.addEventListener("focusout", forget);
    return () => {
      node.removeEventListener("focusin", remember);
      node.removeEventListener("focusout", forget);
    };
  }, []);
 
  useEffect(() => {
    const node = scrollerRef.current;
    const focused = focusedInside.current;
    if (!node || !focused) return;
    // Restore focus only when the element that held it was actually windowed
    // out (removed from the DOM) and focus landed on <body> as a result.
    Eif (
      !focused.isConnected &&
      typeof document !== "undefined" &&
      document.activeElement === document.body
    ) {
      node.focus({ preventScroll: true });
    }
  }, [rowWindow.start, rowWindow.end]);
 
  const visibleRows = rowWindow.virtualized
    ? rows.slice(rowWindow.start, rowWindow.end)
    : rows;
 
  /* ---------------------------------------------------------------------- */
  /* Guarded and empty states                                               */
  /* ---------------------------------------------------------------------- */
 
  if (model.truncated) {
    const canRetry = !diff;
    return (
      <div className={rootClass} data-usefy-diff-state="truncated">
        <div className={styles.notice} role="status">
          <strong className={styles.noticeTitle}>
            {model.truncatedReason === "complexity" ? text.tooDifferent : text.tooLarge}
          </strong>
          {canRetry && (
            <div className={styles.noticeAction}>
              <button
                type="button"
                className={styles.gapButton}
                onClick={() => setForcedFor(inputToken)}
              >
                {text.diffAnyway}
              </button>
            </div>
          )}
        </div>
      </div>
    );
  }
 
  if (model.hunks.length === 0) {
    return (
      <div className={rootClass} data-usefy-diff-state="unchanged">
        <div className={styles.notice} role="status">
          <strong className={styles.noticeTitle}>{text.noChanges}</strong>
          {text.noChangesDetail}
        </div>
      </div>
    );
  }
 
  /* ---------------------------------------------------------------------- */
  /* The diff itself                                                        */
  /* ---------------------------------------------------------------------- */
 
  return (
    <div className={rootClass} data-usefy-diff-state="diff" data-usefy-diff-view={view}>
      <div className={cx(styles.header, classNames.header)}>
        {labels?.stats ? (
          labels.stats(model.stats)
        ) : (
          <span className={styles.stats}>
            <span className={styles.statAdded}>+{model.stats.added}</span>
            <span className={styles.statRemoved}>−{model.stats.removed}</span>
            <span className={styles.statUnchanged}>{model.stats.unchanged} unchanged</span>
          </span>
        )}
      </div>
 
      <div
        // The height bound is driven by whether windowing is WANTED, not by
        // whether it has happened yet. Gating it on the outcome deadlocks:
        // an unbounded scroller grows to the full 20 000-row height, so it
        // always "fits" its own content, so the window never narrows, so the
        // bound is never applied. Intent first, measurement second.
        className={cx(styles.scroller, virtualEnabled && styles.virtualized)}
        ref={scrollerRef}
        style={{ "--usefy-diff-row-height": `${safeRowHeight}px` } as React.CSSProperties}
        data-usefy-diff-virtualized={rowWindow.virtualized ? "true" : undefined}
        // A scrollable region is only reachable by keyboard if it can take
        // focus; applied whenever there is actually something to scroll.
        {...(overflowing || virtualEnabled
          ? { tabIndex: 0, role: "group", "aria-label": text.scrollRegionLabel }
          : {})}
      >
        {/* aria-rowcount is the TRUE total so assistive tech never sees the
            window as the whole document. */}
        <table className={styles.table} aria-rowcount={rows.length}>
          <caption className={styles.srOnly}>
            {text.tableLabel}: {model.stats.added} added, {model.stats.removed} removed,{" "}
            {model.stats.unchanged} unchanged.
          </caption>
          {/* Explicit column widths — the first row is a collapsed-region
              row spanning every column and must not dictate the layout. */}
          <colgroup>
            {view === "split" ? (
              <>
                {showLineNumbers && <col className={styles.colNumber} />}
                <col className={styles.colMarker} />
                <col className={styles.colContent} />
                {showLineNumbers && <col className={styles.colNumber} />}
                <col className={styles.colMarker} />
                <col className={styles.colContent} />
              </>
            ) : (
              <>
                {showLineNumbers && (
                  <>
                    <col className={styles.colNumber} />
                    <col className={styles.colNumber} />
                  </>
                )}
                <col className={styles.colMarker} />
                <col className={styles.colContent} />
              </>
            )}
          </colgroup>
          <tbody>
            {/* Spacers stand in for the rows that are not in the DOM, so the
                scrollbar reflects the whole document rather than the
                window. */}
            {rowWindow.paddingTop > 0 && (
              <tr aria-hidden="true" className={styles.spacer}>
                <td colSpan={columnCount} style={{ height: rowWindow.paddingTop }} />
              </tr>
            )}
            {visibleRows.map((entry, offset) => {
              const index = rowWindow.start + offset;
              const ariaRowIndex = index + 1;
              if (entry.kind === "gap") {
                return (
                  <Gap
                    key={entry.key}
                    ariaRowIndex={ariaRowIndex}
                    hidden={entry.hidden}
                    columnCount={columnCount}
                    styles={styles}
                    classNames={classNames}
                    labels={gapLabels}
                    expandStep={resolved.expandStep}
                    onExpand={(direction) =>
                      handleExpand(
                        entry.hunkIndex,
                        entry.side,
                        entry.state,
                        entry.hidden,
                        direction,
                      )
                    }
                  />
                );
              }
              if (entry.kind === "split") {
                return (
                  <SplitRowView
                    key={entry.key}
                    ariaRowIndex={ariaRowIndex}
                    row={entry.row}
                    showLineNumbers={showLineNumbers}
                    styles={styles}
                    classNames={classNames}
                    labels={rowLabels}
                    renderContent={renderContent}
                  />
                );
              }
              return (
                <UnifiedRowView
                  key={entry.key}
                  ariaRowIndex={ariaRowIndex}
                  line={entry.line}
                  showLineNumbers={showLineNumbers}
                  styles={styles}
                  classNames={classNames}
                  labels={rowLabels}
                  renderContent={renderContent}
                />
              );
            })}
            {rowWindow.paddingBottom > 0 && (
              <tr aria-hidden="true" className={styles.spacer}>
                <td colSpan={columnCount} style={{ height: rowWindow.paddingBottom }} />
              </tr>
            )}
          </tbody>
        </table>
      </div>
    </div>
  );
}