Skip to content

Commit 2fc5cf5

Browse files
authored
fix: stop scrollbar touchstart warnings during momentum scroll (#396)
1 parent 47882c1 commit 2fc5cf5

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎src/ScrollBar.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
162162

163163
onStartMove();
164164
e.stopPropagation();
165-
e.preventDefault();
165+
if (e.cancelable) {
166+
e.preventDefault();
167+
}
166168
});
167169

168170
// ======================== Effect ========================
@@ -172,7 +174,9 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
172174
// ref: https://github.com/facebook/react/issues/9809
173175
React.useEffect(() => {
174176
const onScrollbarTouchStart = (e: TouchEvent) => {
175-
e.preventDefault();
177+
if (e.cancelable) {
178+
e.preventDefault();
179+
}
176180
};
177181

178182
const scrollbarEle = scrollbarRef.current;
@@ -272,6 +276,9 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>
272276
const containerStyle: React.CSSProperties = {
273277
position: 'absolute',
274278
visibility: visible ? null : 'hidden',
279+
// Touches on the scrollbar must never pan the page. Unlike `preventDefault` on touchstart,
280+
// this also holds when the touchstart is not cancelable (e.g. during a momentum scroll).
281+
touchAction: 'none',
275282
};
276283

277284
const thumbStyle: React.CSSProperties = {

‎tests/touch.test.js‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,54 @@ describe('List.Touch', () => {
256256
const preventDefault = jest.fn();
257257
const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) });
258258

259-
const touchEvent = new Event('touchstart');
259+
const touchEvent = new Event('touchstart', { cancelable: true });
260260
touchEvent.preventDefault = preventDefault;
261261
container.querySelector('.rc-virtual-list-scrollbar').dispatchEvent(touchEvent);
262262

263263
expect(preventDefault).toHaveBeenCalled();
264264
});
265265

266+
describe('scrollbar touch during a momentum scroll', () => {
267+
// While a fling is running the browser dispatches touchstart with cancelable=false
268+
function dispatchUncancelableTouchStart(element) {
269+
const preventDefault = jest.fn();
270+
const touchEvent = new Event('touchstart', { bubbles: true, cancelable: false });
271+
touchEvent.touches = [{ pageX: 0, pageY: 0 }];
272+
touchEvent.preventDefault = preventDefault;
273+
act(() => {
274+
element.dispatchEvent(touchEvent);
275+
});
276+
return preventDefault;
277+
}
278+
279+
it('does not preventDefault on the track', () => {
280+
const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) });
281+
282+
const preventDefault = dispatchUncancelableTouchStart(
283+
container.querySelector('.rc-virtual-list-scrollbar'),
284+
);
285+
286+
expect(preventDefault).not.toHaveBeenCalled();
287+
});
288+
289+
it('does not preventDefault on the thumb but still starts dragging', () => {
290+
const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) });
291+
292+
const preventDefault = dispatchUncancelableTouchStart(
293+
container.querySelector('.rc-virtual-list-scrollbar-thumb'),
294+
);
295+
296+
expect(preventDefault).not.toHaveBeenCalled();
297+
expect(container.querySelector('.rc-virtual-list-scrollbar-thumb-moving')).toBeTruthy();
298+
});
299+
300+
it('disables native panning on the scrollbar', () => {
301+
const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) });
302+
303+
expect(container.querySelector('.rc-virtual-list-scrollbar').style.touchAction).toBe('none');
304+
});
305+
});
306+
266307
it('nest touch', async () => {
267308
const { container } = render(
268309
<List component="ul" itemHeight={20} height={100} data={genData(100)}>

0 commit comments

Comments
 (0)