Architecture decision record · ADR-0003

Scaled scroll (spike S3)

SAFE_MAX is 10,000,000 px.

Status
Proposed
Deciders
Joe Gaudet
Created
Updated
PRD
PRD
Related
None

Context

A browser has a maximum height for one element. The values that we know are approximately 33.5M px in Chrome and 17.9M px in Firefox. We did not confirm these values, and we do not know the Safari value. A list of 1M rows at 32 px is 32M px. That is near the Chrome cap and more than the Firefox cap. Above the cap, the sizer element gets shorter than the list, and the last rows cannot be reached.

The plan of record (section 11, S3) is a safe cap of 10M px. Above the cap, Lattice maps the virtual offset to scrollTop with a ratio. Wheel, keyboard and touch input move in virtual space. A "row N of M" hint shows during a scrollbar drag.

Decision drivers

Not recorded in the original ADR.

Options considered

Not recorded in the original ADR.

Decision

  1. SAFE_MAX is 10,000,000 px. The sizer element is min(totalSize, SAFE_MAX).
  2. The ratio is the ratio of the two scroll ranges: (totalSize - viewport) / (sizer - viewport). Thus offset 0 and the end map exactly. A ratio of total sizes does not map the end exactly. Below SAFE_MAX the ratio is 1 and the map is the identity.
  3. The virtual offset is the truth. ScaledScroll keeps it. When it writes scrollTop, the read-back of that write gives the same virtual offset, not a value mapped back from scrollTop. Only a scroll that ScaledScroll did not write (a scrollbar drag) maps the real offset back to a virtual offset. This is necessary because scrollTop has whole-pixel precision. At 100M rows of 32 px the ratio is approximately 320, so one real pixel is 10 rows.
  4. bindScaledInput moves wheel and key input in virtual space while the list is scaled. It listens with passive: false and calls preventDefault. One wheel step of N px moves the list N virtual px. A wheel event in lines (deltaMode 1, Firefox) is 16 px for each line. PageUp and PageDown move one viewport. Below SAFE_MAX the browser keeps all input. A ctrl+wheel (zoom) and a wheel with no vertical part go to the browser. The sideways part of a diagonal wheel goes to scrollLeft. A scroll listener sends a scroll that the layer did not write (a drag) to onScroll.
  5. The renderer gets two values from ScaledScroll: sizerSize for the sizer element, and shift (realOffset - virtualOffset). The real position of an item is item.start + shift. This puts the item at the virtual offset at the top of the viewport.
  6. rowAt(virtualOffset, count, totalSize) gives the 1-based row for the "row N of M" hint. It assumes rows of one size. With measured rows, use getVirtualItemForOffset.
  7. The scaled layer is Lattice code in src/scaled-scroll.ts. The ported src/core/ has one Lattice change for it: the scrollScale option (see the wire-up below).

The wire-up into the Virtualizer

The Virtualizer options observeElementOffset and scrollToFn are not sufficient. The Virtualizer also reads the element directly in two private methods:

  • _readElementOffset reads scrollTop to find its own writes.
  • _elementMaxScrollOffset reads scrollHeight - clientHeight for the end clamp.

Above the cap, both values are real, not virtual. Without a change, the end clamp stops scrollToIndex at the sizer height.

The Virtualizer has one new option, scrollScale: ScaledScroll. When it is set, four places go through it:

  1. The observeElementOffset callback: scale.onScroll(offset) gives the virtual offset.
  2. The write (_scrollToOffset): scale.scrollTo(target) gives the real offset for scrollToFn. The target includes the adjustments, so scrollToFn gets no adjustments.
  3. _readElementOffset: scale.toVirtual(offset). This read does not change the ScaledScroll. It gives the exact virtual offset for a read-back of an own write.
  4. _elementMaxScrollOffset: toVirtualOffset(max, scale.size), so the real end maps to the virtual end.

Before each of these, the Virtualizer sets scale.size to its getTotalSize() and its viewport size. Thus the owner does not keep the sizes current. The change is a Lattice change to src/core/index.ts, recorded in PORT_MAP.md. The renderer still gives the sizer element scale.sizerSize and moves each item by scale.shift. The VirtualizerState bridge does not set scrollScale yet.

Consequences

  • A list of any length can scroll to its last row, in every browser, with a sizer of 10M px or less.
  • Wheel and key steps feel the same at 1k rows and at 100M rows.
  • A scrollbar drag is coarse above the cap. One real pixel is ratio virtual pixels. At 1M rows of 32 px the ratio is approximately 3.2, so a drag can stop at each row. At 100M rows it can stop only at each tenth row. The drag hint shows the row.
  • Touch momentum stays in real pixels, so a fling above the cap moves ratio times further than below the cap. This is a known limit. A fix needs a custom fling (pointer events and our own momentum), and that is not in this spike.
  • Native "find in page" and anchor links do not work on rows that are not rendered. That is true for all virtual scroll, not only for scaled scroll.
  • The docs recommend the server row model above approximately 1M rows (plan section 11).
  • If the CI check shows that scaled scroll fails in a browser, the fallback is paged virtual scroll (a jump list).

Confirmation

Evidence

Tests: packages/virtual/tests/scaled-scroll.test.ts (Vitest and fast-check, jsdom).

  • Property tests for lists of 1 to 5M rows, with row sizes from 20 to 60 px:
    • Offset 0 and the end map exactly, in both directions.
    • The map is monotonic (not decreasing) after the real offset is rounded to whole pixels.
    • A virtual offset goes to a whole-pixel scrollTop and back within 1 row. The error of this round trip is at most ratio / 2, so the bound is true below approximately 20M rows. Above that, decision 3 is necessary.
  • ScaledScroll at 100M rows (ratio approximately 320): its own write reads back to the exact virtual offset for any target. One wheel delta of N moves N virtual px. 1000 steps of 3 px give 3000 px, with no drift.
  • bindScaledInput in jsdom: a wheel event and a PageDown key move in virtual space and call preventDefault. Below SAFE_MAX the input goes to the browser.

Tests: packages/virtual/tests/scaled-virtualizer.test.ts (Vitest, jsdom, an element with a whole-pixel scrollTop that clamps to the sizer).

  • At 100M px, scrollToIndex(count - 1, { align: 'end' }) puts scrollTop at the sizer end and scrollOffset at the virtual end. The last row is in the range.
  • scrollToOffset(50_000_001) reads back as exactly 50,000,001.
  • A scrollTop that the layer did not write (a drag) maps to toVirtualOffset.
  • With bindScaledInput, one wheel step of 100 px moves scrollOffset by 100 px.
  • Below SAFE_MAX the map is the identity.

jsdom has no layout, so these tests do not prove the browser caps or the scrollTop precision.

Real-browser checks (CI in Chrome, Firefox and Safari)

The CI test is not part of this spike. It must assert these items in each browser:

  1. The cap. A sizer of SAFE_MAX (10M px) gives scrollHeight equal to 10M px. Record the largest height that the browser keeps, to confirm or correct the values in this ADR.
  2. The scrollTop precision at large values. A write of a value near 10M px reads back within 1 px.
  3. The end. A scroll to the end (End key, and scrollToIndex(count - 1)) shows the last row fully, with its bottom edge at the bottom of the viewport.
  4. The start. A scroll to the start (Home key) shows row 1 at the top.
  5. The wheel. One wheel step moves the first visible row by the step in px divided by the row size (a few rows), not by the step times the ratio. In Firefox, check that deltaMode 1 (lines) gives the same distance as in Chrome.
  6. The listener. The wheel listener is not passive, so preventDefault stops the native scroll. There is no double move (native and virtual).
  7. The keys. PageDown moves one viewport. ArrowDown moves 40 px. The keys work only when the scroll element has focus.
  8. The drag. During a scrollbar drag the hint shows row N. After release, row N is the first visible row (within 1 row when the ratio is less than the row size).
  9. Touch (Safari on iOS). Record the distance of a fling above the cap. This is the known limit in the consequences.
  10. Each test runs at 1M rows and at 100M rows of 32 px.

Interview record

Each question of the interview with its answer and its source. A fact from the code or a document, a question that the author did not ask, and an assumption are rows too.

QuestionAnswerSource
Where does the text of this page come from?From the Markdown file docs/adr/0003-scaled-scroll.md at commit 4c77ac4. The text of each section is the text of the Markdown file. A section that the Markdown file does not have says so.Document: docs/adr/0003-scaled-scroll.md

Changelog

DateAuthorChange
2026-12-01Joe GaudetStatus in the Markdown file: Proposed. The math, the input layer and the wire-up into the Virtualizer have tests. The real-browser checks are not done.
2026-12-03Joe GaudetCommit 7ce4af2: Lattice 1.0 (#1)
2026-12-04Joe Gaudet (agent)Converted from docs/adr/0003-scaled-scroll.md to this page. The text is the text of the Markdown file.

The adr skill of skillsaw wrote this page. The HTML in this file is the source of truth. The diagrams are the Mermaid sources in it.