1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22import PropTypes from 'prop-types' ;
33import Image from '../Image' ;
44import Spinner from '../Spinner' ;
@@ -25,7 +25,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
2525 srcZoomed : PropTypes . string ,
2626 tag : PropTypes . string ,
2727 isPinchZoomEnabled : PropTypes . bool ,
28- }
28+ onlyZoomOnClick : PropTypes . bool ,
29+ } ;
2930
3031 static defaultProps = {
3132 alt : undefined ,
@@ -40,7 +41,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
4041 onError : null ,
4142 srcZoomed : null ,
4243 tag : 'div' ,
43- }
44+ onlyZoomOnClick : false ,
45+ } ;
4446
4547 /**
4648 * Find the midpoint between two touches.
@@ -86,6 +88,9 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
8688 // tracks the status via image element's onerror events.
8789 isImageLoadingError : true ,
8890
91+ // tracks if the user has clicked on the image or not.
92+ clicked : false ,
93+
8994 // the mouse is currently hovering over the image.
9095 isHovering : false ,
9196
@@ -156,6 +161,7 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
156161 if ( this . state . isZooming ) return ;
157162 this . setState ( {
158163 isHovering : false ,
164+ clicked : false ,
159165 scale : 1 ,
160166 } ) ;
161167 }
@@ -303,6 +309,7 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
303309 src,
304310 srcZoomed,
305311 tag : Tag ,
312+ onlyZoomOnClick,
306313 ...filteredProps
307314 } = this . props ;
308315
@@ -333,8 +340,34 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
333340 overlayStyle . transform = `scale(${ this . state . scale } )` ;
334341 }
335342
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" } ;
336360 return (
337- < Tag className = { newClassName } { ...filteredProps } >
361+ < Tag
362+ className = { newClassName }
363+ { ...filteredProps }
364+ onClick = { ( ) => {
365+ console . log ( "clicked!!!" , this . state . clicked ) ;
366+ this . setState ( { clicked : ! this . state . clicked } ) ;
367+ console . log ( "state is now: " , this . state . clicked ) ;
368+ } }
369+ style = { cursorStyle }
370+ >
338371 < Image
339372 alt = { alt }
340373 className = { newImageClassName }
@@ -344,21 +377,10 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
344377 onError = { this . handleImageLoadError }
345378 { ...bgImageProps }
346379 />
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- />
380+ { onlyZoomOnClick && this . state . clicked ?
381+ overlayImage : < > </ > }
382+ { ! onlyZoomOnClick && overlayImage }
383+
362384 { this . renderLoading ( ) }
363385 </ Tag >
364386 ) ;
0 commit comments