diff --git a/src/hooks/useMobileTouchMove.ts b/src/hooks/useMobileTouchMove.ts index 4ca00a2..f6ec396 100644 --- a/src/hooks/useMobileTouchMove.ts +++ b/src/hooks/useMobileTouchMove.ts @@ -4,6 +4,10 @@ import { useRef } from 'react'; const SMOOTH_PTG = 14 / 15; +// Handling stays on the touch target (see `onTouchStart`): virtualized items can be unmounted +// mid-gesture, and events on a detached target never bubble up to the list. +const onListTouchMove = () => {}; + export default function useMobileTouchMove( inVirtual: boolean, listRef: React.RefObject, @@ -40,7 +44,9 @@ export default function useMobileTouchMove( } const scrollHandled = callback(isHorizontal, isHorizontal ? offsetX : offsetY, false, e); - if (scrollHandled) { + // Once the browser has started a native scroll (e.g. passed through at the list edge), + // the rest of the sequence is not cancelable and `preventDefault` only logs a warning. + if (scrollHandled && e.cancelable) { e.preventDefault(); } @@ -93,12 +99,21 @@ export default function useMobileTouchMove( }; useLayoutEffect(() => { + const listEle = listRef.current; + if (inVirtual) { - listRef.current.addEventListener('touchstart', onTouchStart, { passive: true }); + listEle.addEventListener('touchstart', onTouchStart, { passive: true }); + // Browsers decide whether a touch sequence is cancelable by hit-testing for blocking + // touch listeners at `touchstart`. The real `touchmove` listener is only attached to the + // target inside `onTouchStart`, which is too late, so every `touchmove` would arrive with + // `cancelable=false` and the page would scroll along with the list. A persistent + // non-passive listener on the list marks its area as blocking up front. + listEle.addEventListener('touchmove', onListTouchMove, { passive: false }); } return () => { - listRef.current?.removeEventListener('touchstart', onTouchStart); + listEle?.removeEventListener('touchstart', onTouchStart); + listEle?.removeEventListener('touchmove', onListTouchMove); cleanUpEvents(); clearInterval(intervalRef.current); }; diff --git a/tests/touch.test.js b/tests/touch.test.js index 74416ce..79ad44f 100644 --- a/tests/touch.test.js +++ b/tests/touch.test.js @@ -181,7 +181,7 @@ describe('List.Touch', () => { // move const preventDefault = jest.fn(); - const moveEvent = new Event('touchmove'); + const moveEvent = new Event('touchmove', { cancelable: true }); moveEvent.touches = [{ pageY: 0 }]; moveEvent.preventDefault = preventDefault; getElement().dispatchEvent(moveEvent); @@ -201,13 +201,55 @@ describe('List.Touch', () => { getElement().dispatchEvent(touchEvent2); // move - const moveEvent2 = new Event('touchmove'); + const moveEvent2 = new Event('touchmove', { cancelable: true }); moveEvent2.touches = [{ pageY: 0 }]; moveEvent2.preventDefault = preventDefault; getElement().dispatchEvent(moveEvent2); expect(preventDefault).not.toHaveBeenCalled(); }); + + it('registers a non-passive touchmove listener on the list before any touch starts', () => { + // Browsers decide at touchstart whether the touch sequence is cancelable, so a + // blocking touchmove listener must already be present on the list at that point. + const addEventListener = jest.spyOn(HTMLElement.prototype, 'addEventListener'); + const { container, unmount } = genList({ itemHeight: 20, height: 100, data: genData(100) }); + const element = container.querySelector('.rc-virtual-list-holder'); + + const listTouchMove = addEventListener.mock.instances + .map((instance, index) => [instance, addEventListener.mock.calls[index]]) + .find(([instance, [type]]) => instance === element && type === 'touchmove'); + addEventListener.mockRestore(); + + expect(listTouchMove).toBeTruthy(); + expect(listTouchMove[1][2]).toEqual(expect.objectContaining({ passive: false })); + + unmount(); + }); + + it('does not preventDefault a non-cancelable touchmove', () => { + const { container, unmount } = genList({ itemHeight: 20, height: 100, data: genData(100) }); + const element = container.querySelector('.rc-virtual-list-holder'); + + const touchStart = new Event('touchstart'); + touchStart.touches = [{ pageY: 100 }]; + element.dispatchEvent(touchStart); + + // The browser already started a native scroll, so the move cannot be cancelled + const preventDefault = jest.fn(); + const moveEvent = new Event('touchmove', { cancelable: false }); + moveEvent.touches = [{ pageY: 90 }]; + moveEvent.preventDefault = preventDefault; + act(() => { + element.dispatchEvent(moveEvent); + element.dispatchEvent(new Event('touchend')); + jest.runAllTimers(); + }); + + expect(preventDefault).not.toHaveBeenCalled(); + + unmount(); + }); }); it('should container preventDefault', () => {