Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable window scrolling while swiping horizontally #1048

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 30 additions & 0 deletions src/blocks/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@
return () => window.removeEventListener('resize', onResize);
}, [onResize]);

useEffect(() => {
window.addEventListener('touchstart', touchStart);

Check warning on line 188 in src/blocks/Slider/Slider.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'touchStart' was used before it was defined
window.addEventListener('touchmove', preventTouch, {passive: false});

Check warning on line 189 in src/blocks/Slider/Slider.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'preventTouch' was used before it was defined

return () => {
window.removeEventListener('touchstart', touchStart);

Check warning on line 192 in src/blocks/Slider/Slider.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'touchStart' was used before it was defined
window.removeEventListener('touchmove', preventTouch);

Check warning on line 193 in src/blocks/Slider/Slider.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'preventTouch' was used before it was defined
};
});

const handleArrowClick = (direction: ArrowType) => {
let nextIndex;

Expand Down Expand Up @@ -436,6 +446,26 @@
);
};

const [firstClient, setFirstClient] = useState<number | undefined>();
const touchStart = (e: TouchEvent) => {
setFirstClient(e.touches[0].clientX);
};

const preventTouch = (e: TouchEvent) => {
if (firstClient) {
const minValue = 5; // threshold
const clientX = e.touches[0].clientX - firstClient;

// Vertical scrolling does not work when you start swiping horizontally.
if (Math.abs(clientX) > minValue) {
e.preventDefault();
e.returnValue = false;

Check warning on line 462 in src/blocks/Slider/Slider.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Assignment to property of function parameter 'e'

Check warning on line 462 in src/blocks/Slider/Slider.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Assignment to property of function parameter 'e'
return false;
}
}
return true;
};

return (
<StylesContext.Provider value={{...childStyles, setStyles: setChildStyles}}>
<div
Expand Down
Loading