Skip to content

Commit 1b5e14a

Browse files
committed
fix(core): disable zoom on mobile when zoomToPinch is false (#1449)
* fix(core): disable zoom on mobile when zoomToPinch is false * chore(changeset): add
1 parent 0c1bd1b commit 1b5e14a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

.changeset/khaki-peas-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": patch
3+
---
4+
5+
Disable pinch zoom on mobile if `zoomToPinch` is `false`

packages/core/src/container/Viewport/Viewport.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,14 @@ onMounted(() => {
151151
}
152152
})
153153
154-
d3Zoom.filter((event: MouseEvent) => {
154+
d3Zoom.filter((event: MouseEvent | TouchEvent) => {
155155
const zoomScroll = zoomKeyPressed.value || zoomOnScroll.value
156156
const pinchZoom = zoomOnPinch.value && event.ctrlKey
157+
const eventButton = (event as MouseEvent).button
157158
158159
if (
159160
(shouldPanOnDrag.value === true || (Array.isArray(shouldPanOnDrag.value) && shouldPanOnDrag.value.includes(1))) &&
160-
event.button === 1 &&
161+
eventButton === 1 &&
161162
event.type === 'mousedown' &&
162163
((event.target as HTMLElement)?.closest('.vue-flow__node') || (event.target as HTMLElement)?.closest('.vue-flow__edge'))
163164
) {
@@ -201,6 +202,12 @@ onMounted(() => {
201202
return false
202203
}
203204
205+
// prevent zooming on mobile when using pinch and zoomOnPinch is disabled
206+
if (!zoomOnPinch && event.type === 'touchstart' && (event as TouchEvent).touches?.length > 1) {
207+
event.preventDefault() // if you manage to start with 2 touches, we prevent native zoom
208+
return false
209+
}
210+
204211
// if the pane is not movable, we prevent dragging it with mousestart or touchstart
205212
if (!shouldPanOnDrag.value && (event.type === 'mousedown' || event.type === 'touchstart')) {
206213
return false
@@ -209,15 +216,15 @@ onMounted(() => {
209216
// if the pane is only movable using allowed clicks
210217
if (
211218
Array.isArray(shouldPanOnDrag.value) &&
212-
!shouldPanOnDrag.value.includes(event.button) &&
219+
!shouldPanOnDrag.value.includes(eventButton) &&
213220
(event.type === 'mousedown' || event.type === 'touchstart')
214221
) {
215222
return false
216223
}
217224
218225
// We only allow right clicks if pan on drag is set to right-click
219226
const buttonAllowed =
220-
(Array.isArray(shouldPanOnDrag.value) && shouldPanOnDrag.value.includes(event.button)) || !event.button || event.button <= 1
227+
(Array.isArray(shouldPanOnDrag.value) && shouldPanOnDrag.value.includes(eventButton)) || !eventButton || eventButton <= 1
221228
222229
// default filter for d3-zoom
223230
return (!event.ctrlKey || event.type === 'wheel') && buttonAllowed

0 commit comments

Comments
 (0)