Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions source/Controlled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface ControlledProps {
IconZoom?: React.ElementType
isZoomed: boolean
onZoomChange?: (value: boolean) => void
canSwipeToUnzoom?: boolean
swipeToUnzoomThreshold?: number
wrapElement?: 'div' | 'span'
ZoomContent?: (data: {
img: React.ReactElement | null
Expand All @@ -79,6 +81,8 @@ export function Controlled (props: ControlledProps) {
interface ControlledDefaultProps {
a11yNameButtonUnzoom: string
a11yNameButtonZoom: string
canSwipeToUnzoom: boolean
swipeToUnzoomThreshold: number
IconUnzoom: React.ElementType
IconZoom: React.ElementType
wrapElement: 'div' | 'span'
Expand All @@ -101,6 +105,8 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
a11yNameButtonZoom: 'Expand image',
IconUnzoom: ICompress,
IconZoom: IEnlarge,
canSwipeToUnzoom: true,
swipeToUnzoomThreshold: 10,
wrapElement: 'div',
zoomMargin: 0,
}
Expand Down Expand Up @@ -543,6 +549,10 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
* and unzoom if we detect a swipe
*/
handleTouchMove = (e: TouchEvent) => {
if (!this.props.canSwipeToUnzoom) {
return
}

const browserScale = window.visualViewport?.scale ?? 1

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

if (delta > threshold) {
if (delta > swipeToUnzoomThreshold) {
this.touchYStart = undefined
this.touchYEnd = undefined
this.handleUnzoom()
Expand Down