Skip to content

Commit a5a90d9

Browse files
committed
Fix swiping stuck after zoom (issue #44) - second attempt
1 parent d189c5b commit a5a90d9

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/components/Carousel/Carousel.module.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
display: flex;
4343
justify-content: flex-start;
4444
align-items: flex-start;
45+
touch-action: auto;
4546
flex: 0 1 auto;
4647
}
4748

src/utils/useTouch.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ const getTouchDistinguisher = () => {
1010
}
1111

1212
function _isPinch(event) {
13-
return (
14-
(event.touches !== undefined && event.touches.length > 1) ||
15-
(event.scale !== undefined && event.scale !== 1)
16-
);
13+
return event.touches !== undefined && event.touches.length > 1;
1714
}
1815

1916
function _wasPinch(event) {
@@ -50,8 +47,19 @@ const useTouch = (elementRef, { onTouchMove, onTouchEnd, onTap }) => {
5047
event.preventDefault();
5148
};
5249

53-
const shouldOmitEvent = (event) => {
54-
return !!touchDistinguisher.isPinch(event);
50+
const shouldOmitEvent = (event, displacementX = 0) => {
51+
if (touchDistinguisher.isPinch(event)) return true;
52+
53+
// window.visualViewport is not yet supported on IE
54+
if (!('visualViewport' in window)) return false;
55+
56+
const { scale, offsetLeft, width } = window.visualViewport;
57+
if (scale <= 1) return false;
58+
// pan right at or beyond the left edge
59+
if (offsetLeft <= 0 && displacementX > 0) return false;
60+
// pan left at or beyond the right edge
61+
if (offsetLeft + width >= width * scale && displacementX < 0) return false;
62+
return true;
5563
};
5664

5765
const handleTouchStart = (event) => {
@@ -67,7 +75,7 @@ const useTouch = (elementRef, { onTouchMove, onTouchEnd, onTap }) => {
6775
event.stopPropagation();
6876
if (!isTouchStarted) return;
6977
const displacementX = event.changedTouches[0].clientX - touchStartX;
70-
if (shouldOmitEvent(event)) return;
78+
if (shouldOmitEvent(event, displacementX)) return;
7179
const displacementY = event.changedTouches[0].clientY - touchStartY;
7280
handleVerticalMovement(event, displacementX, displacementY);
7381
onTouchMove(displacementX, displacementY);
@@ -83,7 +91,7 @@ const useTouch = (elementRef, { onTouchMove, onTouchEnd, onTap }) => {
8391
event.stopPropagation();
8492
if (!isTouchStarted) return;
8593
const displacementX = event.changedTouches[0].clientX - touchStartX;
86-
if (shouldOmitEvent(event)) {
94+
if (shouldOmitEvent(event, displacementX)) {
8795
onTouchEnd(0, 0, 0);
8896
return;
8997
}

0 commit comments

Comments
 (0)