Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Unzoom on drag controls. #472

Merged
merged 9 commits into from
Apr 6, 2024
Merged
Changes from 7 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
34 changes: 23 additions & 11 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
canSwipeToUnzoom?: boolean
swipeToUnzoomThreshold?: number
wrapElement?: 'div' | 'span'
ZoomContent?: (data: {
img: ReactElement | null
Expand All @@ -92,6 +94,8 @@ interface ControlledDefaultProps {
a11yNameButtonZoom: string
IconUnzoom: ElementType
IconZoom: ElementType
canSwipeToUnzoom: boolean
swipeToUnzoomThreshold: 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,
canSwipeToUnzoom: true,
swipeToUnzoomThreshold: 10,
wrapElement: 'div',
zoomMargin: 0,
}
Expand Down Expand Up @@ -554,20 +560,26 @@ class ControlledBase extends Component<ControlledPropsWithDefaults, ControlledSt
* 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]) {
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

if (delta > threshold) {
this.touchYStart = undefined
this.touchYEnd = undefined
this.handleUnzoom()
if (this.touchYStart != null && e.changedTouches[0]) {
tshmieldev marked this conversation as resolved.
Show resolved Hide resolved
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 { swipeToUnzoomThreshold } = this.props

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