Skip to content

Commit 06b451f

Browse files
authored
Merge pull request #474 from rpearce/fix/pinch-zoom
2 parents b53dd4e + 19b5fc3 commit 06b451f

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

source/Controlled.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
133133
private changeObserver: MutationObserver | undefined
134134
private imgEl: SupportedImage | null = null
135135
private imgElObserver: ResizeObserver | undefined
136+
private isScaling = false
136137
private prevBodyAttrs: BodyAttrs = defaultBodyAttrs
137138
private styleModalImg: CSSProperties = {}
138139
private touchYStart?: number
@@ -337,6 +338,7 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
337338
window.removeEventListener('wheel', this.handleWheel)
338339
window.removeEventListener('touchstart', this.handleTouchStart)
339340
window.removeEventListener('touchmove', this.handleTouchMove)
341+
window.removeEventListener('touchend', this.handleTouchEnd)
340342
window.removeEventListener('touchcancel', this.handleTouchCancel)
341343
window.removeEventListener('resize', this.handleResize)
342344
document.removeEventListener('keydown', this.handleKeyDown, true)
@@ -532,20 +534,29 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
532534
}
533535

534536
/**
535-
* Start tracking the Y-axis
537+
* Start tracking the Y-axis but abort if non-scroll
538+
* gesture is detected (like pinch-to-zoom)
536539
*/
537540
handleTouchStart = (e: TouchEvent) => {
541+
if (e.touches.length > 1) {
542+
this.isScaling = true
543+
return
544+
}
545+
538546
if (e.changedTouches.length === 1 && e.changedTouches[0]) {
539547
this.touchYStart = e.changedTouches[0].screenY
540548
}
541549
}
542550

543551
/**
544-
* Track how far we've moved on the Y-axis
545-
* and unzoom if we detect a "swipe"
552+
* If the window isn't browser zoomed,
553+
* track how far we've moved on the Y-axis
554+
* and unzoom if we detect a swipe
546555
*/
547556
handleTouchMove = (e: TouchEvent) => {
548-
if (this.touchYStart != null && e.changedTouches[0]) {
557+
const browserScale = window.visualViewport?.scale ?? 1
558+
559+
if (!this.isScaling && browserScale <= 1 && this.touchYStart != null && e.changedTouches[0]) {
549560
this.touchYEnd = e.changedTouches[0].screenY
550561

551562
const max = Math.max(this.touchYStart, this.touchYEnd)
@@ -562,9 +573,19 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
562573
}
563574

564575
/**
565-
* Reset the Y-axis start and end tracking points
576+
* Reset the scaling check and the Y-axis start and end tracking points
577+
*/
578+
handleTouchEnd = () => {
579+
this.isScaling = false
580+
this.touchYStart = undefined
581+
this.touchYEnd = undefined
582+
}
583+
584+
/**
585+
* Reset the scaling check and the Y-axis start and end tracking points
566586
*/
567587
handleTouchCancel = () => {
588+
this.isScaling = false
568589
this.touchYStart = undefined
569590
this.touchYEnd = undefined
570591
}
@@ -592,6 +613,7 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
592613
window.addEventListener('wheel', this.handleWheel, { passive: true })
593614
window.addEventListener('touchstart', this.handleTouchStart, { passive: true })
594615
window.addEventListener('touchmove', this.handleTouchMove, { passive: true })
616+
window.addEventListener('touchend', this.handleTouchEnd, { passive: true })
595617
window.addEventListener('touchcancel', this.handleTouchCancel, { passive: true })
596618
document.addEventListener('keydown', this.handleKeyDown, true)
597619

@@ -620,6 +642,7 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
620642
window.removeEventListener('wheel', this.handleWheel)
621643
window.removeEventListener('touchstart', this.handleTouchStart)
622644
window.removeEventListener('touchmove', this.handleTouchMove)
645+
window.removeEventListener('touchend', this.handleTouchEnd)
623646
window.removeEventListener('touchcancel', this.handleTouchCancel)
624647
document.removeEventListener('keydown', this.handleKeyDown, true)
625648

0 commit comments

Comments
 (0)