@@ -25,7 +25,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
25
25
srcZoomed : PropTypes . string ,
26
26
tag : PropTypes . string ,
27
27
isPinchZoomEnabled : PropTypes . bool ,
28
- }
28
+ onlyZoomOnClick : PropTypes . bool ,
29
+ } ;
29
30
30
31
static defaultProps = {
31
32
alt : undefined ,
@@ -40,7 +41,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
40
41
onError : null ,
41
42
srcZoomed : null ,
42
43
tag : 'div' ,
43
- }
44
+ onlyZoomOnClick : false ,
45
+ } ;
44
46
45
47
/**
46
48
* Find the midpoint between two touches.
@@ -86,6 +88,9 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
86
88
// tracks the status via image element's onerror events.
87
89
isImageLoadingError : true ,
88
90
91
+ // tracks if the user has clicked on the image or not.
92
+ clicked : false ,
93
+
89
94
// the mouse is currently hovering over the image.
90
95
isHovering : false ,
91
96
@@ -156,6 +161,7 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
156
161
if ( this . state . isZooming ) return ;
157
162
this . setState ( {
158
163
isHovering : false ,
164
+ clicked : false ,
159
165
scale : 1 ,
160
166
} ) ;
161
167
}
@@ -303,6 +309,7 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
303
309
src,
304
310
srcZoomed,
305
311
tag : Tag ,
312
+ onlyZoomOnClick,
306
313
...filteredProps
307
314
} = this . props ;
308
315
@@ -333,8 +340,38 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
333
340
overlayStyle . transform = `scale(${ this . state . scale } )` ;
334
341
}
335
342
343
+ const overlayImage = (
344
+ < Image
345
+ className = { newOverlayClassName }
346
+ tag = "div"
347
+ src = { srcZoomed || src }
348
+ style = { overlayStyle }
349
+ isBgImage
350
+ onFocus = { this . handleOnMouseOver }
351
+ onMouseOver = { this . handleOnMouseOver }
352
+ onBlur = { this . handleOnMouseOut }
353
+ onMouseOut = { this . handleOnMouseOut }
354
+ onMouseMove = { this . handleOnMouseMove }
355
+ onTouchStart = { this . handleOnTouchStart }
356
+ onTouchEnd = { this . handleOnTouchEnd }
357
+ onTouchMove = { this . handleOnTouchMove }
358
+ />
359
+ ) ;
360
+
361
+ let cursorStyle = { cursor : 'zoom-in' } ;
362
+ if ( onlyZoomOnClick ) {
363
+ cursorStyle = this . state . clicked ? { cursor : 'zoom-out' } : { cursor : 'zoom-in' } ;
364
+ }
365
+
336
366
return (
337
- < Tag className = { newClassName } { ...filteredProps } >
367
+ < Tag
368
+ className = { newClassName }
369
+ { ...filteredProps }
370
+ onClick = { ( ) => {
371
+ this . setState ( state => ( { clicked : ! state . clicked } ) ) ;
372
+ } }
373
+ style = { cursorStyle }
374
+ >
338
375
< Image
339
376
alt = { alt }
340
377
className = { newImageClassName }
@@ -344,21 +381,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
344
381
onError = { this . handleImageLoadError }
345
382
{ ...bgImageProps }
346
383
/>
347
- < Image
348
- className = { newOverlayClassName }
349
- tag = "div"
350
- src = { srcZoomed || src }
351
- style = { overlayStyle }
352
- isBgImage
353
- onFocus = { this . handleOnMouseOver }
354
- onMouseOver = { this . handleOnMouseOver }
355
- onBlur = { this . handleOnMouseOut }
356
- onMouseOut = { this . handleOnMouseOut }
357
- onMouseMove = { this . handleOnMouseMove }
358
- onTouchStart = { this . handleOnTouchStart }
359
- onTouchEnd = { this . handleOnTouchEnd }
360
- onTouchMove = { this . handleOnTouchMove }
361
- />
384
+ { ( ( onlyZoomOnClick && this . state . clicked ) || ! onlyZoomOnClick )
385
+ ? overlayImage : < > </ > }
362
386
{ this . renderLoading ( ) }
363
387
</ Tag >
364
388
) ;
0 commit comments