Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-command-wheel-hold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/virtual-core': patch
---

Fix a smooth `scrollToIndex` or `scrollToOffset` in WebKit stopping short of its target when rows are prepended or measured during wheel input while it travels. The held correction is now written when the wheel rests, and the scroll continues to its target.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
with:
main-branch-name: main
- name: Install Playwright browsers
run: pnpm exec playwright install chromium
run: pnpm exec playwright install --with-deps chromium webkit
- name: Run Checks
run: pnpm run test:pr
preview:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Setup Tools
uses: tanstack/config/.github/setup@e4b48f16568324f76f467aa4c2aac2f05db632c3 # main
- name: Install Playwright browsers
run: pnpm exec playwright install chromium
run: pnpm exec playwright install --with-deps chromium webkit
- name: Run Tests
run: pnpm run test:ci
- name: Run Changesets (version or publish)
Expand Down
76 changes: 76 additions & 0 deletions packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { expect, test } from '@playwright/test'
import type { Page } from '@playwright/test'

// The wheel hold is WebKit only (#1400), so this runs in WebKit.
test.use({ browserName: 'webkit' })

const scrollTop = (page: Page) =>
page.evaluate(() => document.querySelector('#scroll-container')!.scrollTop)

// Where row 0 is drawn, relative to the top of the viewport.
const rowZeroTop = (page: Page) =>
page.evaluate(() => {
const container = document.querySelector('#scroll-container')!
const row = container.querySelector('[data-index="0"]')
if (!row) return null
return (
row.getBoundingClientRect().top -
container.getBoundingClientRect().top -
container.clientTop
)
})

// #1406: a smooth scrollToIndex(0) travels, a wheel event comes in, and rows
// are prepended within the wheel's 150ms, so the anchor is held. A real wheel
// does not stop the smooth animation in WebKit, so the element reaches 0
// while the layout is the held 250px away. The command must still land on
// the new row 0, with the hold written.
test('a prepend held under the wheel does not strand a smooth scrollToIndex', async ({
page,
}) => {
await page.goto('/smooth-prepend/')
await expect
.poll(() =>
page.evaluate(() => {
const c = document.querySelector('#scroll-container')!
return Math.abs(c.scrollHeight - c.scrollTop - c.clientHeight)
}),
)
.toBeLessThan(1.01)

// All of it inside one real wheel event, so no round trip has to fit into
// WebKit's ~250ms animation: a capture listener on the window starts the
// smooth command before the virtualizer's own wheel listener opens the
// wheel window, and a listener after it prepends, which is then held. The
// wheel's own scroll lands while the command travels.
await page.evaluate(() => {
const container = document.querySelector('#scroll-container')!
window.addEventListener(
'wheel',
() => document.querySelector<HTMLButtonElement>('#smooth-to-0')!.click(),
{ capture: true, once: true },
)
container.addEventListener(
'wheel',
() => {
;(window as any).prependedAt = container.scrollTop
document.querySelector<HTMLButtonElement>('#prepend')!.click()
},
{ once: true },
)
})
const box = (await page.locator('#scroll-container').boundingBox())!
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2)
await page.mouse.wheel(0, -40)

// The prepend came while the command was still travelling, not after it.
const prependedAt = await page.evaluate(() => (window as any).prependedAt)
expect(prependedAt).toBeGreaterThan(500)

await expect.poll(() => rowZeroTop(page), { timeout: 3000 }).toBe(0)
expect(await scrollTop(page)).toBeLessThan(1.01)
await expect(page.locator('[data-index="0"]')).toHaveAttribute(
'data-message-id',
'm--5',
)
})
47 changes: 39 additions & 8 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ export class Virtualizer<
if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer)
this._wheelTimer = win.setTimeout(() => {
this._wheelTimer = null
this._flushHoldIfReady(true)
this._flushHoldIfReady(true, true)
this._retryClampedAdjustment()
}, 150)
// At the top the element stops moving, so no scroll event comes.
Expand Down Expand Up @@ -1439,14 +1439,31 @@ export class Virtualizer<
}

// Write the command that `_startScroll` left waiting for the momentum
// tail, against the layout as it is now (rows measured during the fling
// have moved an index target).
// tail.
private _writeAwaitingCommand = () => {
const state = this.scrollState
if (state === null || state.phase !== 'queued') return
this._writeCommand(state)
}

// Write a command again from where the element is, against the layout as
// it is now (rows measured during a fling have moved an index target).
private _writeCommand = (state: ScrollState) => {
const { behavior } = state
this.getMeasurements()
const offset = this.getScrollStateTarget(state)
// Clamped to the range the element has now, as for a command issued
// now (`getOffsetForAlignment`). After a hold's flush, a consumer that
// commits the sizer later leaves the element its old max, and a trim
// above can have moved an offset target below 0; reconcile does not
// write an unchanged offset target again. An index or end target is
// already clamped, and reconcile retargets it once the sizer grows.
const offset = Math.max(
Math.min(
this.getScrollStateTarget(state),
Math.max(this.getMaxScrollOffset(), 0),
),
0,
)
state.lastTargetOffset = offset
// The hold flushed just before can leave a clamp retry (#1258); it
// would write its old target over this command.
Expand Down Expand Up @@ -1543,7 +1560,7 @@ export class Virtualizer<
if (this._wheelTimer === null || !this._holdAtTop()) return
win.clearTimeout(this._wheelTimer)
this._wheelTimer = null
this._flushHoldIfReady(true)
this._flushHoldIfReady(true, true)
this._retryClampedAdjustment()
})
})
Expand All @@ -1559,19 +1576,31 @@ export class Virtualizer<
// (`_willUpdate`), where an update it schedules commits before paint
// anyway. A command's start passes true, although `_willUpdate` can reach
// it too (a follow on append).
private _flushHoldIfReady = (sync: boolean) => {
private _flushHoldIfReady = (sync: boolean, wheelEnded = false) => {
if (this._holdOffset === 0) return
if (!this.scrollElement || !this.options.enabled) return
if (!this._canWriteScroll()) return
// Let a travelling smooth scroll land first (see `_willUpdate`).
if (this.scrollState?.phase === 'smooth') return
// Let a travelling smooth scroll land first (see `_willUpdate`). Not as
// the wheel hold ends: a wheel does not stop the animation (Playwright
// WebKit and Chromium), and one written while the wheel held a
// correction was aimed the hold short, or clamped at the top. It would
// never see its target and would keep the hold until the safety valve
// (#1406). Write the hold, which stops the animation, and the command
// again from there. Other holds keep the wait: a touch stops the
// animation, and the command written again would pull the user back.
const state = this.scrollState
const reaim = wheelEnded && state?.phase === 'smooth'
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (state?.phase === 'smooth' && !reaim) return
this._holdOffset = 0
// A held shrink can leave the layout position above the top; the
// element clamps that to 0 without an event to say so (#1229).
if (this.scrollOffset !== null && this.scrollOffset < 0) {
this.scrollOffset = 0
}
this.notify(sync)
// `onChange` started another command during the notify: it has written
// its target, and a write here would stop it if it is smooth.
if (this.scrollState !== null && this.scrollState !== state) return
// A command whose write has not been reported back yet owns the target:
// `scrollOffset` is still the pre-command position until its read-back,
// and a command that closes the touch tail makes a write possible with
Expand All @@ -1581,6 +1610,8 @@ export class Virtualizer<
// (#1258).
this._clampedAdjustment = this._clampedBy(target)
this._scrollToOffset(target)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Not if `onChange` cancelled the command during the notify.
if (reaim && this.scrollState === state) this._writeCommand(state)
}

private rafId: number | null = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports[`scroll behaviour snapshot > anchorEnd ios27 1`] = `
"20: 4c1a00c0 5b17cebe 39f53c48 43b3722d 4c85db64 0d41dbae 319823eb 0cd61d7a ebd8a220 09a1874e",
"30: 0224b245 2a263abe beba95dc 6e007e5d 3ab04872 8000f929 a8da8fd8 4acd8bac 3c2c8710 93ac03b2",
"40: 351e701d 81b41eab 7e4c0d8a e8e6cf89 0018ede1 ee1edfdf 49ca9103 334646c6 3e95ee0d 8f0a8fe9",
"50: ca6b1fc6 8cc11ea8 90e19d07 7c8d21a0 11d479bb adc56879 e06a8ddb e46aa4df e9fb7a13 a865bfd7",
"50: ca6b1fc6 8cc11ea8 90e19d07 7c8d21a0 13ccf8d1 adc56879 e06a8ddb e46aa4df e9fb7a13 a865bfd7",
]
`;

Expand Down
Loading
Loading