Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/ScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((props, ref) =>

onStartMove();
e.stopPropagation();
e.preventDefault();
if (e.cancelable) {
e.preventDefault();
}
});

// ======================== Effect ========================
Expand All @@ -172,7 +174,9 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((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;
Expand Down Expand Up @@ -272,6 +276,9 @@ const ScrollBar = React.forwardRef<ScrollBarRef, ScrollBarProps>((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 = {
Expand Down
43 changes: 42 additions & 1 deletion tests/touch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<List component="ul" itemHeight={20} height={100} data={genData(100)}>
Expand Down
Loading