diff --git a/.changeset/ios-hold-command-below-hold.md b/.changeset/ios-hold-command-below-hold.md new file mode 100644 index 000000000..ff438d595 --- /dev/null +++ b/.changeset/ios-hold-command-below-hold.md @@ -0,0 +1,5 @@ +--- +'@tanstack/virtual-core': patch +--- + +Fix a scroll command stopping short of a target near the top while a scroll correction is held (iOS touch, or a bounce). For example, `scrollToIndex(0)` right after a tap that grew a row above the viewport ended the size of that growth below the top. The held correction is now written first, then the command. diff --git a/packages/react-virtual/e2e/app/test/ios-hold-below-target.spec.ts b/packages/react-virtual/e2e/app/test/ios-hold-below-target.spec.ts new file mode 100644 index 000000000..47683cd5a --- /dev/null +++ b/packages/react-virtual/e2e/app/test/ios-hold-below-target.spec.ts @@ -0,0 +1,57 @@ +import { expect, test } from '@playwright/test' + +// An iPhone UA plus touch emulation puts Chromium on the iOS code path (see +// ios-programmatic-landing.spec.ts). Only the hold bookkeeping is real here, +// not WebKit's touch physics. +test.use({ + hasTouch: true, + userAgent: + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1', +}) + +// #1405: a row above the viewport grows during a tap (held), and the app +// jumps to the top from the tap's touchend. The target is less than the +// hold, so written the hold short it would clamp at 0 and the list would end +// the hold below the top. +for (const behavior of ['auto', 'smooth'] as const) { + test(`scrollToIndex(0) (${behavior}) after a tap reaches the top under a held correction`, async ({ + page, + browserName, + }) => { + test.skip(browserName !== 'chromium', 'touch emulation runs in Chromium') + // 50px rows, 400px viewport, at 1000: rows 15-19 are mounted above it. + await page.goto('/ios-deferral/?initialOffset=1000') + await expect + .poll(() => page.evaluate(() => (window as any).__t.el?.scrollTop)) + .toBe(1000) + + // The row grows under a finger, which holds the correction for as long + // as it is down, so no timing matters until the lift. The tap's tail + // then lasts 150ms: the jump comes in the same task as the touchend. + const result = await page.evaluate(async (b) => { + const t = (window as any).__t + const touch = (type: string) => + t.el.dispatchEvent(new TouchEvent(type, { bubbles: true })) + touch('touchstart') + t.setH(17, 90, true) + for (let i = 0; i < 120 && t.v._holdOffset === 0; i++) { + await t.frames(1) + } + const hold = t.v._holdOffset + touch('touchend') + t.v.scrollToIndex(0, { align: 'start', behavior: b }) + for (let i = 0; i < 60 && t.el.scrollTop !== 0; i++) await t.frames(1) + await t.wait(300) + return { + hold, + top: t.el.scrollTop, + row0: t.rowTop(0), + holdAfter: t.v._holdOffset, + } + }, behavior) + + // The correction was held when the jump was made. + expect(result.hold).toBe(40) + expect(result).toEqual({ hold: 40, top: 0, row0: 0, holdAfter: 0 }) + }) +} diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 033b9fb9e..8598bac27 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -2692,7 +2692,8 @@ export class Virtualizer< // before the command (below). One held for a touch or a bounce is not // dropped by a command: the write lands the element `_holdOffset` short // of the target, under the rows exposed shifted by the same amount, and - // the flush moves both together once a write is possible. + // the flush moves both together once a write is possible. Unless the + // target is less than the hold (below). // // An index or end target that is already at its offset starts a smooth // scroll as landed; an offset target never does, so its first scroll @@ -2773,6 +2774,21 @@ export class Virtualizer< // scroll: leave it. this._iosGesture.end() } + // A target less than the hold is out of the element's reach: written + // the hold short, the write clamps at the element's 0, and the read-back + // looks like a scroll away from the target (#1405). Write the hold first + // where a write is possible now (the command has closed a tap's tail), + // as for the wheel above. Its clamp retry would write over the command. + // The target was clamped to a max that included the hold; a consumer + // that commits the sizer after the notify leaves the element its old + // max, so clamp it again, as for a command issued now. Reconcile + // retargets an index or end target once the sizer grows. + if (offset < this._holdOffset && this._canWriteScroll()) { + this.scrollState = null + this._flushHoldIfReady(true) + this._clampedAdjustment = null + offset = Math.min(offset, Math.max(this.getMaxScrollOffset(), 0)) + } this.scrollState = { ...target, startedAt: this.now(), diff --git a/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap b/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap index 4ff59755c..c045fee11 100644 --- a/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap +++ b/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap @@ -257,7 +257,7 @@ exports[`scroll behaviour snapshot > predicate web 1`] = ` [ "0: a888515d 54daaa9d e2050bb4 44d5f4d8 218505be 45a8c314 3bacba65 a3c61d73 be3ad9c9 65341162", "10: c449bc54 664cd3c5 ee8f5a46 b879192a 1850e117 89be8bb7 662a68a0 96f784f1 8134fb03 f9a8d78d", - "20: c6008fee 2548afff 0f7ebcdd 5d61493a d4ec59b1 19b8fe5a fe239908 4bc9f749 042a505f 8caf71f9", + "20: c6008fee 2548afff 4fe4eb99 5d61493a d4ec59b1 19b8fe5a fe239908 4bc9f749 042a505f 8caf71f9", "30: 883b3a0c 7cab4af4 9250e3e4 c4617ee9 a6405c93 046b74f7 9eeab09f f357057a bc7d089d 65080957", "40: 81e56faa a1301eb6 d5dec481 c7358748 0a4b0bb8 03a1759f 83b18626 1b8c0d8e 8e8f3238 842f2579", "50: 3b219acb 3655a421 2ca0eb37 da0b01ac ba8a46f7 84937fa5 0f598b23 0ab9d71e 6428c749 aadf8cd0", @@ -266,7 +266,7 @@ exports[`scroll behaviour snapshot > predicate web 1`] = ` exports[`scroll behaviour snapshot > smooth ios 1`] = ` [ - "0: 413b0ced 76974b95 66c9821e 3bfbda41 6c5f17be 3bf7edae ea1516fd d2c2c7b5 206ab6a1 7e0be436", + "0: 09091f4c 76974b95 66c9821e 3bfbda41 6c5f17be 3bf7edae ea1516fd d2c2c7b5 206ab6a1 7e0be436", "10: 9cebb1b0 4551cde0 621cc58e 957921f5 f1a1ad23 a18fdfbb 105e890d 756893ae 4696c4a5 30a62565", "20: a052c4c5 a8692c66 982814d7 9b2cff0c 346d6565 84e55162 c3519085 7e222fc0 7f014674 058cdf95", "30: 71c0f874 b2f398b4 484a6a70 bf605550 a5b43ec3 ed5c0a5b d63bc86d 8843c6fe 08adf872 f2174e51", @@ -277,7 +277,7 @@ exports[`scroll behaviour snapshot > smooth ios 1`] = ` exports[`scroll behaviour snapshot > smooth ios27 1`] = ` [ - "0: 413b0ced 76974b95 66c9821e b31939e8 6c5f17be 3bf7edae ea1516fd d2c2c7b5 206ab6a1 90c45313", + "0: 09091f4c 76974b95 66c9821e b31939e8 6c5f17be 3bf7edae ea1516fd d2c2c7b5 206ab6a1 90c45313", "10: f3a5a705 c07b2aec 621cc58e 957921f5 d05d020e a18fdfbb 30e7bb4d 6e2a63b1 4696c4a5 30a62565", "20: a052c4c5 f52aa31d 9e38faf7 9b2cff0c 2a78f411 84e55162 18e799f0 7e222fc0 d587a000 058cdf95", "30: 71c0f874 b2f398b4 484a6a70 2ecd0e38 27f1c0e4 ed5c0a5b d63bc86d 93d54b36 08adf872 f2174e51", diff --git a/packages/virtual-core/tests/ios-deferral.test.ts b/packages/virtual-core/tests/ios-deferral.test.ts index 75f7ef92f..f47d93d05 100644 --- a/packages/virtual-core/tests/ios-deferral.test.ts +++ b/packages/virtual-core/tests/ios-deferral.test.ts @@ -1691,6 +1691,229 @@ test('iOS hold: a resize landing between a command and its read-back does not fl }) }) +// A command whose target is above the element's reach while a correction is +// held: written the hold short, it would clamp at the element's 0 (#1405). +for (const behavior of ['auto', 'smooth'] as const) { + test(`iOS hold (#1405): scrollToIndex(0) (${behavior}) after a tap reaches the top under a held correction`, () => { + withFakeIOSUserAgent(() => { + const { v, el, scrollToFn, scroll, touch } = makeIOSTouchVirtualizer({ + scrollTop: 200, + }) + // The element clamps a write to its range; a smooth one reports later. + scrollToFn.mockImplementation( + (offset: number, { adjustments = 0, behavior: b } = {}) => { + if (b !== 'smooth') el.scrollTop = Math.max(0, offset + adjustments) + }, + ) + touch('touchstart') + touch('touchend') // a tap: its tail holds the correction + v.resizeItem(0, 100) // above the viewport: held + expect(v['_holdOffset']).toBe(50) + + v.scrollToIndex(0, { align: 'start', behavior }) + // The hold is written first, where the reader is, then the command. + expect( + scrollToFn.mock.calls.map(([offset, opts]) => [ + offset + (opts.adjustments ?? 0), + opts.behavior, + ]), + ).toEqual([ + [250, undefined], + [0, behavior], + ]) + scroll(0, behavior === 'smooth') + scroll(0, false) + + expect(v['_holdOffset']).toBe(0) + expect(v.scrollOffset).toBe(0) + expect(v['getMeasurements']()[0]!.start - el.scrollTop).toBe(0) + expect(v.scrollState).not.toBeNull() + }) + }) +} + +// After a tap at 200 that grew row 0 by 50 (held), with an element that +// clamps writes to its range. +function tapHold50() { + const h = makeIOSTouchVirtualizer({ scrollTop: 200 }) + h.scrollToFn.mockImplementation( + (offset: number, { adjustments = 0, behavior } = {}) => { + if (behavior !== 'smooth') + h.el.scrollTop = Math.max(0, offset + adjustments) + }, + ) + h.touch('touchstart') + h.touch('touchend') + h.v.resizeItem(0, 100) + expect(h.v['_holdOffset']).toBe(50) + const writes = () => + h.scrollToFn.mock.calls.map( + ([offset, opts]) => offset + (opts.adjustments ?? 0), + ) + return { ...h, writes } +} + +for (const target of [0, 20, 49]) { + test(`iOS hold (#1405): scrollToOffset(${target}) below the hold lands`, () => { + withFakeIOSUserAgent(() => { + const { v, el, scroll, writes } = tapHold50() + v.scrollToOffset(target) + expect(writes()).toEqual([250, target]) + scroll(target, false) + expect(v['_holdOffset']).toBe(0) + expect(v.scrollOffset).toBe(target) + expect(el.scrollTop).toBe(target) + }) + }) +} + +test('iOS hold (#1405): a target equal to the hold keeps the old path', () => { + withFakeIOSUserAgent(() => { + const { v, writes } = tapHold50() + // Written the hold short: 50 - 50 = 0, which the element can take. + v.scrollToOffset(50) + expect(writes()).toEqual([0]) + expect(v['_holdOffset']).toBe(50) + }) +}) + +test('iOS hold (#1405): a negative hold keeps the old path', () => { + withFakeIOSUserAgent(() => { + const { v, scrollToFn, touch } = makeIOSTouchVirtualizer({ + scrollTop: 200, + }) + touch('touchstart') + touch('touchend') + v.resizeItem(0, 20) // above the viewport, shrinks: held + expect(v['_holdOffset']).toBe(-30) + v.scrollToIndex(0, { align: 'start' }) + // One write, the hold short (0 + 30); the hold is written later. + expect( + scrollToFn.mock.calls.map(([o, opts]) => o + (opts.adjustments ?? 0)), + ).toEqual([30]) + expect(v['_holdOffset']).toBe(-30) + }) +}) + +test('iOS hold (#1405): under a finger still down the hold is not written first', () => { + withFakeIOSUserAgent(() => { + const { v, scrollToFn, touch } = makeIOSTouchVirtualizer({ + scrollTop: 200, + }) + touch('touchstart') // the finger stays down: iOS ignores writes + v.resizeItem(0, 100) + expect(v['_holdOffset']).toBe(50) + v.scrollToIndex(0, { align: 'start' }) + // A known limit: written the hold short, as before. + expect( + scrollToFn.mock.calls.map(([o, opts]) => o + (opts.adjustments ?? 0)), + ).toEqual([-50]) + expect(v['_holdOffset']).toBe(50) + }) +}) + +test('iOS 27 hold (#1405): scrollToIndex(0) waiting for a fling lands when the tail closes', () => { + // A fling from 2000, now passing 1500, that grew row 0 by 50. + const h = makeDeferralHarness({ start: 2000, ios27: true }) + h.touch('touchstart') + h.run(() => h.scroll(true, 2010)) + h.touch('touchend') + h.run(() => h.scroll(true, 1500)) + h.run(() => h.v.resizeItem(0, 100)) + expect(h.v['_holdOffset']).toBe(50) + h.writes.length = 0 + + h.run(() => h.v.scrollToIndex(0, { align: 'start' })) + expect(h.writes).toEqual([]) + h.run(() => h.scroll(false)) + // The hold is written first, then the command, at the top. + h.expireTail() + expect(h.writes).toEqual([1550, 0]) + h.run(() => { + h.scroll(false, 0) + h.frame() + h.frame() + }) + expect(h.v['_holdOffset']).toBe(0) + expect(h.rowTop(0)).toBe(0) + expect(h.v.scrollState).toBeNull() +}) + +// The same with a consumer that commits the sizer after the hold's notify +// (not inside `onChange`): the element keeps its old max for the command's +// write. 10 rows of 50, viewport 200, max 300; 4 rows above the viewport +// grow by 200 each, so the hold is 800 and row 3 starts at 750. +function lateSizerUnderHold() { + const h = makeDeferralHarness({ ios: true, start: 200 }) + // As an adapter does: the sizer commits, then `_willUpdate` runs. + const commit = () => { + h.el.scrollHeight = h.v.getTotalSize() + h.run(() => h.v._willUpdate()) + } + h.run(() => { + h.v.setOptions({ + ...h.v.options, + count: 10, + scrollToFn: (offset, { adjustments = 0 }) => { + h.writes.push(offset + adjustments) + h.el.scrollTop = Math.max( + 0, + Math.min(offset + adjustments, h.el.scrollHeight - h.el.clientHeight), + ) + }, + }) + h.v._willUpdate() + }) + commit() + h.run(() => h.scroll(false, 200)) + h.touch('touchstart') + h.touch('touchend') + h.run(() => h.scroll(true, 200)) + h.run(() => { + for (let i = 0; i < 4; i++) h.v.resizeItem(i, 250) + }) + expect(h.v['_holdOffset']).toBe(800) + h.writes.length = 0 + return { h, commit } +} + +test('iOS hold (#1405): scrollToIndex below the hold lands once a late sizer commits', () => { + const { h, commit } = lateSizerUnderHold() + h.run(() => h.v.scrollToIndex(3, { align: 'start' })) + // Written at the max the element has, not past it. + expect(h.writes).toEqual([1000, 300]) + h.run(() => h.scroll(false)) + commit() + h.run(() => { + for (let i = 0; i < 10; i++) { + h.advance(16) + h.frame() + h.scroll(false) + } + }) + expect(h.el.scrollTop).toBe(750) + expect(h.rowTop(3)).toBe(0) + expect(h.v.scrollState).toBeNull() +}) + +test('iOS hold (#1405): scrollToOffset below the hold ends at the max a late sizer leaves', () => { + const { h, commit } = lateSizerUnderHold() + h.run(() => h.v.scrollToOffset(760)) + expect(h.writes).toEqual([1000, 300]) + h.run(() => h.scroll(false)) + commit() + h.run(() => { + for (let i = 0; i < 10; i++) { + h.advance(16) + h.frame() + h.scroll(false) + } + }) + // As if issued after the flush: clamped to the max, and done. + expect(h.el.scrollTop).toBe(300) + expect(h.v.scrollState).toBeNull() +}) + // A compensation write clamped by a not-yet-grown sizer is retried once the // sizer grows (#1258). That retry is a write like any other: it must wait // while a touch owns the scroll, and go out once the tail closes (#1327).