1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import Image from '../Image' ;
4
4
import Spinner from '../Spinner' ;
@@ -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,32 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
333
340
overlayStyle . transform = `scale(${ this . state . scale } )` ;
334
341
}
335
342
343
+ const overlayImage = < Image
344
+ className = { newOverlayClassName }
345
+ tag = "div"
346
+ src = { srcZoomed || src }
347
+ style = { overlayStyle }
348
+ isBgImage
349
+ onFocus = { this . handleOnMouseOver }
350
+ onMouseOver = { this . handleOnMouseOver }
351
+ onBlur = { this . handleOnMouseOut }
352
+ onMouseOut = { this . handleOnMouseOut }
353
+ onMouseMove = { this . handleOnMouseMove }
354
+ onTouchStart = { this . handleOnTouchStart }
355
+ onTouchEnd = { this . handleOnTouchEnd }
356
+ onTouchMove = { this . handleOnTouchMove }
357
+ />
358
+
359
+ const cursorStyle = onlyZoomOnClick ? this . state . clicked ? { cursor : "zoom-out" } : { cursor : "zoom-in" } : { cursor : "zoom-in" } ;
336
360
return (
337
- < Tag className = { newClassName } { ...filteredProps } >
361
+ < Tag
362
+ className = { newClassName }
363
+ { ...filteredProps }
364
+ onClick = { ( ) => {
365
+ this . setState ( { clicked : ! this . state . clicked } ) ;
366
+ } }
367
+ style = { cursorStyle }
368
+ >
338
369
< Image
339
370
alt = { alt }
340
371
className = { newImageClassName }
@@ -344,21 +375,10 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
344
375
onError = { this . handleImageLoadError }
345
376
{ ...bgImageProps }
346
377
/>
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
- />
378
+ { onlyZoomOnClick && this . state . clicked ?
379
+ overlayImage : < > </ > }
380
+ { ! onlyZoomOnClick && overlayImage }
381
+
362
382
{ this . renderLoading ( ) }
363
383
</ Tag >
364
384
) ;
0 commit comments