Skip to content

Commit cf340b0

Browse files
authored
Merge pull request #510 from rpearce/chore/swipe-to-unzoom-backfill
chore: backfill docs and style for 2 new swipe to unzoom features
2 parents 186b16b + 5b12819 commit cf340b0

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export interface UncontrolledProps {
7474
// Default: 'Expand image'
7575
a11yNameButtonZoom?: string
7676

77+
// Allow swipe gesture to unzoom.
78+
// Default: true
79+
canSwipeToUnzoom?: boolean
80+
7781
// Your image (required).
7882
children: ReactNode
7983

@@ -88,6 +92,10 @@ export interface UncontrolledProps {
8892
// Default: IEnlarge
8993
IconZoom?: ElementType
9094

95+
// Swipe gesture threshold after which to unzoom.
96+
// Default: 10
97+
swipeToUnzoomThreshold?: number
98+
9199
// Specify what type of element should be used for
92100
// internal component usage. This is useful if the
93101
// image is inside a <p> or <button>, for example.

source/Controlled.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ const defaultBodyAttrs: BodyAttrs = {
5555
export interface ControlledProps {
5656
a11yNameButtonUnzoom?: string
5757
a11yNameButtonZoom?: string
58+
canSwipeToUnzoom?: boolean
5859
children: React.ReactNode
5960
classDialog?: string
6061
IconUnzoom?: React.ElementType
6162
IconZoom?: React.ElementType
6263
isZoomed: boolean
6364
onZoomChange?: (value: boolean) => void
64-
canSwipeToUnzoom?: boolean
6565
swipeToUnzoomThreshold?: number
6666
wrapElement?: 'div' | 'span'
6767
ZoomContent?: (data: {
@@ -82,9 +82,9 @@ interface ControlledDefaultProps {
8282
a11yNameButtonUnzoom: string
8383
a11yNameButtonZoom: string
8484
canSwipeToUnzoom: boolean
85-
swipeToUnzoomThreshold: number
8685
IconUnzoom: React.ElementType
8786
IconZoom: React.ElementType
87+
swipeToUnzoomThreshold: number
8888
wrapElement: 'div' | 'span'
8989
zoomMargin: number
9090
}
@@ -103,9 +103,9 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
103103
static defaultProps: ControlledDefaultProps = {
104104
a11yNameButtonUnzoom: 'Minimize image',
105105
a11yNameButtonZoom: 'Expand image',
106+
canSwipeToUnzoom: true,
106107
IconUnzoom: ICompress,
107108
IconZoom: IEnlarge,
108-
canSwipeToUnzoom: true,
109109
swipeToUnzoomThreshold: 10,
110110
wrapElement: 'div',
111111
zoomMargin: 0,
@@ -549,21 +549,21 @@ class ControlledBase extends React.Component<ControlledPropsWithDefaults, Contro
549549
* and unzoom if we detect a swipe
550550
*/
551551
handleTouchMove = (e: TouchEvent) => {
552-
if (!this.props.canSwipeToUnzoom) {
553-
return
554-
}
555-
556552
const browserScale = window.visualViewport?.scale ?? 1
557553

558-
if (!this.isScaling && browserScale <= 1 && this.touchYStart != null && e.changedTouches[0]) {
554+
if (
555+
this.props.canSwipeToUnzoom &&
556+
!this.isScaling &&
557+
browserScale <= 1 && this.touchYStart != null &&
558+
e.changedTouches[0]
559+
) {
559560
this.touchYEnd = e.changedTouches[0].screenY
560561

561562
const max = Math.max(this.touchYStart, this.touchYEnd)
562563
const min = Math.min(this.touchYStart, this.touchYEnd)
563564
const delta = Math.abs(max - min)
564-
const { swipeToUnzoomThreshold } = this.props
565565

566-
if (delta > swipeToUnzoomThreshold) {
566+
if (delta > this.props.swipeToUnzoomThreshold) {
567567
this.touchYStart = undefined
568568
this.touchYEnd = undefined
569569
this.handleUnzoom()

stories/Img.stories.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,58 @@ export const InlineImage = (props) => (
451451
</main>
452452
)
453453

454+
// =============================================================================
455+
456+
export const SwipeToUnzoomDisabled = (props) => (
457+
<main aria-label="Story">
458+
<h1>Swipe to Unzoom Disabled</h1>
459+
<p>
460+
This example demonstrates preventing swipe gestures from
461+
unzooming when an image is zoomed. This is best tested on
462+
a touchscreen device!
463+
</p>
464+
<div>
465+
<Zoom {...props} canSwipeToUnzoom={false}>
466+
<img
467+
alt={imgThatWanakaTree.alt}
468+
src={imgThatWanakaTree.src}
469+
decoding="async"
470+
height="320"
471+
loading="lazy"
472+
/>
473+
</Zoom>
474+
</div>
475+
</main>
476+
)
477+
478+
export const SwipeToUnzoomThreshold = (props) => (
479+
<main aria-label="Story">
480+
<h1>Swipe to Unzoom Threshold</h1>
481+
<p>
482+
This example demonstrates increasing the threshold
483+
required for a swipe gesture on a touchscreen device to
484+
unzoom when an image is zoomed. This is best tested on
485+
a touchscreen device!
486+
</p>
487+
<p>
488+
The default is <code>10</code> (px), but this example
489+
is set to <code>200</code> (px); that&apos;s how far
490+
you&apos;ll have to move your finger across the screen.
491+
</p>
492+
<div>
493+
<Zoom {...props} swipeToUnzoomThreshold={200}>
494+
<img
495+
alt={imgThatWanakaTree.alt}
496+
src={imgThatWanakaTree.src}
497+
decoding="async"
498+
height="320"
499+
loading="lazy"
500+
/>
501+
</Zoom>
502+
</div>
503+
</main>
504+
)
505+
454506
// =============================================================================
455507
// INTERACTIONS
456508

0 commit comments

Comments
 (0)