diff --git a/src/ScrollBar.tsx b/src/ScrollBar.tsx index ec4e5a4..7116ef4 100644 --- a/src/ScrollBar.tsx +++ b/src/ScrollBar.tsx @@ -162,7 +162,9 @@ const ScrollBar = React.forwardRef((props, ref) => onStartMove(); e.stopPropagation(); - e.preventDefault(); + if (e.cancelable) { + e.preventDefault(); + } }); // ======================== Effect ======================== @@ -172,7 +174,9 @@ const ScrollBar = React.forwardRef((props, ref) => // ref: https://github.com/facebook/react/issues/9809 React.useEffect(() => { const onScrollbarTouchStart = (e: TouchEvent) => { - e.preventDefault(); + if (e.cancelable) { + e.preventDefault(); + } }; const scrollbarEle = scrollbarRef.current; @@ -272,6 +276,9 @@ const ScrollBar = React.forwardRef((props, ref) => const containerStyle: React.CSSProperties = { position: 'absolute', visibility: visible ? null : 'hidden', + // Touches on the scrollbar must never pan the page. Unlike `preventDefault` on touchstart, + // this also holds when the touchstart is not cancelable (e.g. during a momentum scroll). + touchAction: 'none', }; const thumbStyle: React.CSSProperties = { diff --git a/tests/touch.test.js b/tests/touch.test.js index 79ad44f..b8f6ab4 100644 --- a/tests/touch.test.js +++ b/tests/touch.test.js @@ -256,13 +256,54 @@ describe('List.Touch', () => { const preventDefault = jest.fn(); const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) }); - const touchEvent = new Event('touchstart'); + const touchEvent = new Event('touchstart', { cancelable: true }); touchEvent.preventDefault = preventDefault; container.querySelector('.rc-virtual-list-scrollbar').dispatchEvent(touchEvent); expect(preventDefault).toHaveBeenCalled(); }); + describe('scrollbar touch during a momentum scroll', () => { + // While a fling is running the browser dispatches touchstart with cancelable=false + function dispatchUncancelableTouchStart(element) { + const preventDefault = jest.fn(); + const touchEvent = new Event('touchstart', { bubbles: true, cancelable: false }); + touchEvent.touches = [{ pageX: 0, pageY: 0 }]; + touchEvent.preventDefault = preventDefault; + act(() => { + element.dispatchEvent(touchEvent); + }); + return preventDefault; + } + + it('does not preventDefault on the track', () => { + const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) }); + + const preventDefault = dispatchUncancelableTouchStart( + container.querySelector('.rc-virtual-list-scrollbar'), + ); + + expect(preventDefault).not.toHaveBeenCalled(); + }); + + it('does not preventDefault on the thumb but still starts dragging', () => { + const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) }); + + const preventDefault = dispatchUncancelableTouchStart( + container.querySelector('.rc-virtual-list-scrollbar-thumb'), + ); + + expect(preventDefault).not.toHaveBeenCalled(); + expect(container.querySelector('.rc-virtual-list-scrollbar-thumb-moving')).toBeTruthy(); + }); + + it('disables native panning on the scrollbar', () => { + const { container } = genList({ itemHeight: 20, height: 100, data: genData(100) }); + + expect(container.querySelector('.rc-virtual-list-scrollbar').style.touchAction).toBe('none'); + }); + }); + it('nest touch', async () => { const { container } = render(