Skip to content
Merged
Changes from 3 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 @@ -72,6 +72,8 @@ export interface ControlledProps {
IconZoom?: ElementType
isZoomed: boolean
onZoomChange?: (value: boolean) => void
unZoomOnContentDragged?: boolean
unZoomOnContentDraggedThreshold?: number
wrapElement?: 'div' | 'span'
ZoomContent?: (data: {
img: ReactElement | null
Expand All @@ -92,6 +94,8 @@ interface ControlledDefaultProps {
a11yNameButtonZoom: string
IconUnzoom: ElementType
IconZoom: ElementType
unZoomOnContentDragged: boolean
unZoomOnContentDraggedThreshold: number
wrapElement: 'div' | 'span'
zoomMargin: number
}
Expand All @@ -112,6 +116,8 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
a11yNameButtonZoom: 'Expand image',
IconUnzoom: ICompress,
IconZoom: IEnlarge,
unZoomOnContentDragged: true,
unZoomOnContentDraggedThreshold: 10,
wrapElement: 'div',
zoomMargin: 0,
}
Expand Down Expand Up @@ -545,15 +551,19 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
* and unzoom if we detect a "swipe"
*/
handleTouchMove = (e: TouchEvent) => {
if (!this.props.unZoomOnContentDragged) {
return
}

if (this.touchYStart != null && e.changedTouches[0]) {
this.touchYEnd = e.changedTouches[0].screenY

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 { unZoomOnContentDraggedThreshold } = this.props

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