virtual-core: on Android Chrome, a scrollToIndex issued during a touch fling overshoots its target #1346
Send to my agentDescribe the bug#
On Android Chrome, a scrollToIndex / scrollToOffset / scrollToEnd issued while a touch fling is still running does not land. The write reaches the element, and then the fling carries on from the written position.
#1332 stops the fling before such a command on iOS 27, where a write no longer ends the momentum either. On Android nothing stops it, because the post-touch tail that tells _startScroll a fling may be running is only opened on iOS.
A person tapping a button during a fling does not run into this on Android: Chrome uses that touch to stop the fling and swallows the tap. It is commands issued by code while the fling runs (a new chat message, a timer, a "jump to latest" triggered by data) that overshoot.
Your minimal, reproducible example#
No hosted link: a single plain-DOM page with no framework is inline under "Additional context". On the Pixel it rests 3,177–3,210px past the target, 3 runs of 3.
Steps to reproduce#
- Open the page below in Chrome on an Android phone.
- Fling the list upwards.
- 150ms after the finger lifts, the page calls
virtualizer.scrollToIndex(12345, { align: 'start' })from a timer. - The log shows where the list rests 1.5s later, and the first six frames after the call.
Expected behavior#
The list rests with row 12345 at the top (scrollTop 617250).
How often does this bug happen?#
Every time
Screenshots or Videos#
| Device | Scroller | 3.19.8, 3 runs |
|---|---|---|
| Pixel 6, Android 16, Chrome 156.0.8078.47 | element | missed 3/3: rested at 620458, 620460, 620459 (~3,200px, 64 rows past) |
| same | window | missed 3/3: 3,163–3,204px past |
| iPhone 11, iOS 26.2, Safari | element | landed 8/8 (the write stops the fling there) |
Frames after the call on the Pixel: 617309, 617369, 617427, 617484, 617542, 617598. The write lands, then the fling continues from it.
Platform#
- OS: Android 16 (Pixel 6)
- Browser: Chrome 156.0.8078.47
tanstack-virtual version#
3.19.8 (main at 18d08351 behaves the same)
TypeScript version#
n/a
Additional context#
Found through Kebechet.Blazor.TanStack.VirtualCore, a Blazor wrapper around virtual-core. The wrapper currently stops the fling itself before it calls a command, and drops that once virtual-core does it. A PR with a fix follows.
Repro page: one file, virtual-core 3.19.8 from jsDelivr. Serve it over HTTP and open it on the phone.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>virtual-core: scrollToIndex mid-fling</title>
<style>
body { font: 13px system-ui, sans-serif; margin: 0; }
#log { padding: 4px 6px; background: #ffe; border-bottom: 1px solid #aa8; height: 150px; overflow: hidden; }
#viewport { overflow: auto; height: 520px; }
#root { position: relative; }
.row { position: absolute; left: 0; right: 0; height: 50px; border-bottom: 1px solid #bbb; box-sizing: border-box; padding: 14px 8px; }
</style>
</head>
<body>
<div id="log">Fling the list. 150ms after the finger lifts, scrollToIndex(12345) is issued.</div>
<div id="viewport"><div id="root"></div></div>
<script type="module">
import { Virtualizer, elementScroll, observeElementOffset, observeElementRect } from 'https://cdn.jsdelivr.net/npm/@tanstack/virtual-core@3.19.8/+esm';
const count = 20000, size = 50, target = 12345, want = target * size;
const viewport = document.getElementById('viewport');
const root = document.getElementById('root');
const log = document.getElementById('log');
// Keyed like a framework adapter: the row a finger touched stays mounted while it is in range,
// so it still receives the touchend.
const mounted = new Map();
const render = virtualizer => {
root.style.height = `${virtualizer.getTotalSize()}px`;
const items = virtualizer.getVirtualItems();
const keep = new Set(items.map(item => item.index));
for (const [index, row] of mounted) {
if (!keep.has(index)) { row.remove(); mounted.delete(index); }
}
for (const item of items) {
let row = mounted.get(item.index);
if (!row) {
row = document.createElement('div');
row.className = 'row';
row.textContent = `Row ${item.index}`;
root.appendChild(row);
mounted.set(item.index, row);
}
row.style.transform = `translateY(${item.start}px)`;
}
};
const virtualizer = new Virtualizer({
count,
estimateSize: () => size,
getScrollElement: () => viewport,
scrollToFn: elementScroll,
observeElementOffset,
observeElementRect,
onChange: render,
});
virtualizer._didMount();
virtualizer._willUpdate();
render(virtualizer);
let armed = true;
let run = 0;
window.addEventListener('touchend', () => {
if (!armed) return;
armed = false;
setTimeout(async () => {
const frames = [];
let sampling = true;
const sample = () => { frames.push(Math.round(viewport.scrollTop)); if (sampling) requestAnimationFrame(sample); };
requestAnimationFrame(sample);
// A command issued by code while the fling still runs.
virtualizer.scrollToIndex(target, { align: 'start' });
await new Promise(resolve => setTimeout(resolve, 1500));
sampling = false;
const rest = Math.round(viewport.scrollTop);
log.insertAdjacentHTML('beforeend', `<div>run ${++run}: rested at ${rest}, want ${want} (${rest === want ? 'landed' : `${rest - want}px off`}); frames ${frames.slice(0, 6).join(', ')}</div>`);
viewport.scrollTop = 0;
setTimeout(() => { armed = true; }, 1000);
}, 150);
}, { passive: true, capture: true });
</script>
</body>
</html>
Terms & Code of Conduct#
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
piecyk closed this
Sign in to join this conversation.