Skip to content

Commit 3989412

Browse files
authored
Merge pull request #507 from Tofandel/patch-6
fix: allow users to pinch zoom on mobile and prevent click properly only if user drags more than 10px
2 parents b04ab27 + aef80c8 commit 3989412

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

playground/App.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const handleReset = () => {
171171
items.splice(0, items.length, ...defaultSlides)
172172
}
173173
174-
const handelButtonClick = () => {
174+
const handleButtonClick = () => {
175175
alert('Button clicked')
176176
}
177177
@@ -264,7 +264,7 @@ onMounted(() => {
264264
>
265265
<h3>{{ item.title }}</h3>
266266
<p>{{ item.description }}</p>
267-
<button @click="handelButtonClick">This is a button</button>
267+
<button @click="handleButtonClick">This is a button</button>
268268
</div>
269269
</CarouselSlide>
270270
<template #addons>

src/components/Carousel/Carousel.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
height: var(--vc-carousel-height);
88
overscroll-behavior: none;
99
position: relative;
10-
touch-action: pan-y;
10+
touch-action: pan-x pan-y pinch-zoom;
1111
z-index: 1;
1212
}
1313

src/components/Carousel/Carousel.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ export const Carousel = defineComponent({
423423
threshold,
424424
})
425425

426+
// Prevent unnecessary reactivity
427+
if (draggedSlides === 0) {
428+
return
429+
}
430+
426431
activeSlideIndex.value = config.wrapAround
427432
? currentSlideIndex.value + draggedSlides
428433
: getNumberInRange({

src/composables/useDrag.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export function useDrag(options: UseDragOptions) {
3535

3636
isTouch = event.type === 'touchstart'
3737

38-
if (!isTouch) {
38+
if (isTouch && (event as TouchEvent).touches.length > 1) {
39+
// If there is more than 1 finger on the screen, avoid drag start (this allows user to pinch zoom)
40+
return
41+
} else if (!isTouch) {
3942
event.preventDefault()
4043
if ((event as MouseEvent).button !== 0) {
4144
return
@@ -58,6 +61,10 @@ export function useDrag(options: UseDragOptions) {
5861
}
5962

6063
const handleDrag = throttle((event: TouchEvent | MouseEvent): void => {
64+
if (isTouch && (event as TouchEvent).touches.length > 1) {
65+
return
66+
}
67+
6168
isDragging.value = true
6269

6370
const currentX = isTouch
@@ -76,12 +83,13 @@ export function useDrag(options: UseDragOptions) {
7683
const handleDragEnd = (): void => {
7784
handleDrag.cancel()
7885

79-
if (!isTouch) {
80-
const preventClick = (e: MouseEvent) => {
86+
const draggedDistance = Math.abs(dragged.x) + Math.abs(dragged.y);
87+
88+
if (!isTouch && draggedDistance > 10) {
89+
window.addEventListener('click', (e: MouseEvent) => {
8190
e.preventDefault()
82-
window.removeEventListener('click', preventClick)
83-
}
84-
window.addEventListener('click', preventClick)
91+
e.stopPropagation()
92+
}, { once: true, capture: true })
8593
}
8694

8795
options.onDragEnd?.()

0 commit comments

Comments
 (0)