diff --git a/.changeset/clamped-corrections.md b/.changeset/clamped-corrections.md new file mode 100644 index 000000000..afeccd3a8 --- /dev/null +++ b/.changeset/clamped-corrections.md @@ -0,0 +1,8 @@ +--- +'@tanstack/virtual-core': patch +--- + +Fix two corrections that the browser clamps without a scroll event leaving `scrollOffset` wrong: + +- A correction past the top. The recorded write now matches the clamp, as `scrollOffset` already did, so a command in flight is no longer treated as waiting for a scroll event that will not come. +- A correction on a list that fits its viewport, where the only position is 0. `scrollOffset` used to keep the clamped target until an unrelated scroll event. It is now set to 0 once the sizer is committed, with a notify, and a later command to 0 no longer waits for a scroll event that cannot come. diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 4bd3531db..812d63e99 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -1010,6 +1010,12 @@ export class Virtualizer< adjustments: delta, behavior, }) + // A write past the top is clamped to 0 by the browser, and from an + // element already there no scroll event follows (#1229): record what + // the element can report back, as `scrollOffset` is clamped below, or + // the write would look like an unreported one for as long as a + // command is in flight. + if (write.target < 0) write.target = 0 // Eagerly carry the intended target in `scrollOffset` so callers that // read it before the next scroll event — notably the next `resizeItem` // tick's `getVirtualDistanceFromEnd()` / `wasAtEnd` check — see the @@ -1346,7 +1352,7 @@ export class Virtualizer< this._flushHoldIfReady(false) // The consumer has committed the new total size by now, so a clamped // compensation write may have room (#1258). - this._retryClampedAdjustment() + this._retryClampedAdjustment(true) } // Classify a scroll event (see `ScrollEventKind`) from the state as it was @@ -1378,7 +1384,7 @@ export class Virtualizer< // absence of one leave `_clampedAdjustment` set, so timing does not matter. // While the element cannot take a write (`_canWriteScroll`) the record is // kept for the next opportunity, the latest being the tail closing. - private _retryClampedAdjustment = () => { + private _retryClampedAdjustment = (committed = false) => { if ( this._clampedAdjustment === null || !this.scrollElement || @@ -1395,6 +1401,28 @@ export class Virtualizer< this._clampedAdjustment = target > max + 0.5 ? { target, maxAtWrite: max } : null this._scrollToOffset(target) + } else if (committed && this._isUnscrollable()) { + // The sizer is committed and did not grow, nor will it: the layout + // fits the viewport, so the element's one position is 0. The browser + // clamped the write there and, from an element already at 0, reported + // no scroll event (#1229 at the other edge). Adopt it: otherwise + // `scrollOffset` keeps the clamped target until some unrelated scroll + // event, and a command written to 0 afterwards can never read back, its + // target being where the element already is. + this._clampedAdjustment = null + const moved = this.scrollOffset !== null && this.scrollOffset > 0 + if (moved) { + this.scrollOffset = 0 + } + if ( + this.pendingWrite !== null && + approxEqual(this.pendingWrite.target, target) + ) { + this.pendingWrite.target = 0 + } + // The last notify carried the clamped target; no scroll event will + // report 0. + if (moved) this.notify(false) } } @@ -2406,6 +2434,16 @@ export class Virtualizer< ) } + // The layout fits the viewport and the element has no scroll range: its + // one position is 0, and no scroll event will ever report another. + private _isUnscrollable = () => { + const layoutMax = + this._getLayoutTotalSize() + this.options.scrollMargin - this.getSize() + if (layoutMax > 0) return false + const elementMax = this._elementMaxScrollOffset() + return elementMax !== null && elementMax <= 0.5 + } + // A compensation write past the element's current max (the sizer has not // grown yet) is clamped by the browser; record it for the retry (#1258). private _clampedBy = (target: number) => { diff --git a/packages/virtual-core/tests/scroll-commands.test.ts b/packages/virtual-core/tests/scroll-commands.test.ts index cf8f0ec74..102e74ab6 100644 --- a/packages/virtual-core/tests/scroll-commands.test.ts +++ b/packages/virtual-core/tests/scroll-commands.test.ts @@ -2150,3 +2150,69 @@ test.each([ expect(v.getVirtualItems().at(-1)!.index).toBeGreaterThanOrEqual(54) }, ) + +test('a correction clamped at the top during a command is not left waiting for a scroll event', () => { + // Three rows estimated at 100px in a 400px viewport, anchored to the end: + // the list fits, so it is at the end at offset 0. A row measuring smaller + // keeps the end pinned with a write to -8, which the browser clamps to 0 + // from an element already there, so no scroll event reports it. + const { v, el } = setupHandDrivenScroll({ + count: 3, + estimateSize: () => 100, + anchorTo: 'end', + observeElementRect: (_inst, cb) => { + cb({ width: 400, height: 400 }) + return () => {} + }, + }) + el.clientHeight = 400 + el.scrollHeight = 400 + + v.scrollToIndex(2, { align: 'end' }) + expect(el.scrollTop).toBe(0) + expect(v['_awaitingCommandReadBack']()).toBe(false) + + v.resizeItem(0, 92) + expect(el.scrollTop).toBe(0) + expect(v.scrollOffset).toBe(0) + // The recorded write matches the clamp, so it does not read as one the + // element has yet to report. + expect(v['_awaitingCommandReadBack']()).toBe(false) +}) + +test('a correction on a list that fits its viewport is adopted as 0, with a notify', () => { + // Three rows estimated at 100px in a 400px viewport, anchored to the end. + // The last row grows by 24px: the end stays pinned with a write to 24, + // which the browser clamps to 0 from an element already there, so no + // scroll event reports it and the sizer never grows past the viewport. + const onChange = vi.fn() + const { v, el } = setupHandDrivenScroll({ + onChange, + count: 3, + estimateSize: () => 100, + anchorTo: 'end', + observeElementRect: (_inst, cb) => { + cb({ width: 400, height: 400 }) + return () => {} + }, + }) + el.clientHeight = 400 + el.scrollHeight = 400 + + v.resizeItem(2, 124) + expect(el.scrollTop).toBe(0) + expect(onChange.mock.lastCall![0].scrollOffset).toBe(24) + + // The consumer renders: the sizer is committed, still shorter than the + // viewport, so 0 is the only position. + onChange.mockClear() + v._willUpdate() + expect(v.scrollOffset).toBe(0) + expect(onChange).toHaveBeenCalledTimes(1) + expect(onChange.mock.calls[0][0].scrollOffset).toBe(0) + + // A command to the end writes 0, where the element already is. It is not + // left waiting for a scroll event that cannot come. + v.scrollToIndex(2, { align: 'end' }) + expect(v['_awaitingCommandReadBack']()).toBe(false) +})