Skip to content

Commit 186b16b

Browse files
authored
Merge pull request #472 from tshmieldev/dragUnZoomControl
Better Unzoom on drag controls.
2 parents 1818903 + af2af39 commit 186b16b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

source/Controlled.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export interface ControlledProps {
6161
IconZoom?: React.ElementType
6262
isZoomed: boolean
6363
onZoomChange?: (value: boolean) => void
64+
canSwipeToUnzoom?: boolean
65+
swipeToUnzoomThreshold?: number
6466
wrapElement?: 'div' | 'span'
6567
ZoomContent?: (data: {
6668
img: React.ReactElement | null
@@ -79,6 +81,8 @@ export function Controlled (props: ControlledProps) {
7981
interface ControlledDefaultProps {
8082
a11yNameButtonUnzoom: string
8183
a11yNameButtonZoom: string
84+
canSwipeToUnzoom: boolean
85+
swipeToUnzoomThreshold: number
8286
IconUnzoom: React.ElementType
8387
IconZoom: React.ElementType
8488
wrapElement: 'div' | 'span'
@@ -101,6 +105,8 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
101105
a11yNameButtonZoom: 'Expand image',
102106
IconUnzoom: ICompress,
103107
IconZoom: IEnlarge,
108+
canSwipeToUnzoom: true,
109+
swipeToUnzoomThreshold: 10,
104110
wrapElement: 'div',
105111
zoomMargin: 0,
106112
}
@@ -543,6 +549,10 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
543549
* and unzoom if we detect a swipe
544550
*/
545551
handleTouchMove = (e: TouchEvent) => {
552+
if (!this.props.canSwipeToUnzoom) {
553+
return
554+
}
555+
546556
const browserScale = window.visualViewport?.scale ?? 1
547557

548558
if (!this.isScaling && browserScale <= 1 && this.touchYStart != null && e.changedTouches[0]) {
@@ -551,9 +561,9 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
551561
const max = Math.max(this.touchYStart, this.touchYEnd)
552562
const min = Math.min(this.touchYStart, this.touchYEnd)
553563
const delta = Math.abs(max - min)
554-
const threshold = 10
564+
const { swipeToUnzoomThreshold } = this.props
555565

556-
if (delta > threshold) {
566+
if (delta > swipeToUnzoomThreshold) {
557567
this.touchYStart = undefined
558568
this.touchYEnd = undefined
559569
this.handleUnzoom()

0 commit comments

Comments
 (0)