Skip to content

Commit 52186f0

Browse files
committed
fix: make touchmove cancelable so touch scrolling does not scroll the page
1 parent 17ca15a commit 52186f0

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎src/hooks/useMobileTouchMove.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { useRef } from 'react';
44

55
const SMOOTH_PTG = 14 / 15;
66

7+
// Handling stays on the touch target (see `onTouchStart`): virtualized items can be unmounted
8+
// mid-gesture, and events on a detached target never bubble up to the list.
9+
const onListTouchMove = () => {};
10+
711
export default function useMobileTouchMove(
812
inVirtual: boolean,
913
listRef: React.RefObject<HTMLDivElement>,
@@ -40,7 +44,9 @@ export default function useMobileTouchMove(
4044
}
4145

4246
const scrollHandled = callback(isHorizontal, isHorizontal ? offsetX : offsetY, false, e);
43-
if (scrollHandled) {
47+
// Once the browser has started a native scroll (e.g. passed through at the list edge),
48+
// the rest of the sequence is not cancelable and `preventDefault` only logs a warning.
49+
if (scrollHandled && e.cancelable) {
4450
e.preventDefault();
4551
}
4652

@@ -93,12 +99,21 @@ export default function useMobileTouchMove(
9399
};
94100

95101
useLayoutEffect(() => {
102+
const listEle = listRef.current;
103+
96104
if (inVirtual) {
97-
listRef.current.addEventListener('touchstart', onTouchStart, { passive: true });
105+
listEle.addEventListener('touchstart', onTouchStart, { passive: true });
106+
// Browsers decide whether a touch sequence is cancelable by hit-testing for blocking
107+
// touch listeners at `touchstart`. The real `touchmove` listener is only attached to the
108+
// target inside `onTouchStart`, which is too late, so every `touchmove` would arrive with
109+
// `cancelable=false` and the page would scroll along with the list. A persistent
110+
// non-passive listener on the list marks its area as blocking up front.
111+
listEle.addEventListener('touchmove', onListTouchMove, { passive: false });
98112
}
99113

100114
return () => {
101-
listRef.current?.removeEventListener('touchstart', onTouchStart);
115+
listEle?.removeEventListener('touchstart', onTouchStart);
116+
listEle?.removeEventListener('touchmove', onListTouchMove);
102117
cleanUpEvents();
103118
clearInterval(intervalRef.current);
104119
};

‎tests/touch.test.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('List.Touch', () => {
181181

182182
// move
183183
const preventDefault = jest.fn();
184-
const moveEvent = new Event('touchmove');
184+
const moveEvent = new Event('touchmove', { cancelable: true });
185185
moveEvent.touches = [{ pageY: 0 }];
186186
moveEvent.preventDefault = preventDefault;
187187
getElement().dispatchEvent(moveEvent);
@@ -201,13 +201,55 @@ describe('List.Touch', () => {
201201
getElement().dispatchEvent(touchEvent2);
202202

203203
// move
204-
const moveEvent2 = new Event('touchmove');
204+
const moveEvent2 = new Event('touchmove', { cancelable: true });
205205
moveEvent2.touches = [{ pageY: 0 }];
206206
moveEvent2.preventDefault = preventDefault;
207207
getElement().dispatchEvent(moveEvent2);
208208

209209
expect(preventDefault).not.toHaveBeenCalled();
210210
});
211+
212+
it('registers a non-passive touchmove listener on the list before any touch starts', () => {
213+
// Browsers decide at touchstart whether the touch sequence is cancelable, so a
214+
// blocking touchmove listener must already be present on the list at that point.
215+
const addEventListener = jest.spyOn(HTMLElement.prototype, 'addEventListener');
216+
const { container, unmount } = genList({ itemHeight: 20, height: 100, data: genData(100) });
217+
const element = container.querySelector('.rc-virtual-list-holder');
218+
219+
const listTouchMove = addEventListener.mock.instances
220+
.map((instance, index) => [instance, addEventListener.mock.calls[index]])
221+
.find(([instance, [type]]) => instance === element && type === 'touchmove');
222+
addEventListener.mockRestore();
223+
224+
expect(listTouchMove).toBeTruthy();
225+
expect(listTouchMove[1][2]).toEqual(expect.objectContaining({ passive: false }));
226+
227+
unmount();
228+
});
229+
230+
it('does not preventDefault a non-cancelable touchmove', () => {
231+
const { container, unmount } = genList({ itemHeight: 20, height: 100, data: genData(100) });
232+
const element = container.querySelector('.rc-virtual-list-holder');
233+
234+
const touchStart = new Event('touchstart');
235+
touchStart.touches = [{ pageY: 100 }];
236+
element.dispatchEvent(touchStart);
237+
238+
// The browser already started a native scroll, so the move cannot be cancelled
239+
const preventDefault = jest.fn();
240+
const moveEvent = new Event('touchmove', { cancelable: false });
241+
moveEvent.touches = [{ pageY: 90 }];
242+
moveEvent.preventDefault = preventDefault;
243+
act(() => {
244+
element.dispatchEvent(moveEvent);
245+
element.dispatchEvent(new Event('touchend'));
246+
jest.runAllTimers();
247+
});
248+
249+
expect(preventDefault).not.toHaveBeenCalled();
250+
251+
unmount();
252+
});
211253
});
212254

213255
it('should container preventDefault', () => {

0 commit comments

Comments
 (0)