diff --git a/src/App/App.jsx b/src/App/App.jsx index 7dbc2af..2c2b546 100644 --- a/src/App/App.jsx +++ b/src/App/App.jsx @@ -14,6 +14,7 @@ import { Example12, Example13, Example14, + Example15, } from './examples'; import s from './style.scss'; @@ -255,6 +256,13 @@ class DevelopmentApp extends React.Component { )} + {(value === '0' || value === '15') && ( +
+
+ +
+ )} + diff --git a/src/App/examples/Example15/Example15.jsx b/src/App/examples/Example15/Example15.jsx new file mode 100644 index 0000000..be2df0c --- /dev/null +++ b/src/App/examples/Example15/Example15.jsx @@ -0,0 +1,41 @@ +import React from 'react'; +import +{ + ButtonBack, + ButtonFirst, + ButtonLast, + ButtonNext, + CarouselProvider, + Slide, + Slider, + ImageWithZoom, + DotGroup, +} from '../../..'; + +import s from '../../style.scss'; + +export default () => ( + +

Zoom only on click

+

Two images: first will zoom on click, second will not.

+ + + + + + + + + First + Back + Next + Last + +
+); diff --git a/src/App/examples/Example15/index.js b/src/App/examples/Example15/index.js new file mode 100644 index 0000000..72cb48f --- /dev/null +++ b/src/App/examples/Example15/index.js @@ -0,0 +1,3 @@ +import Example15 from './Example15'; + +export default Example15; diff --git a/src/App/examples/index.js b/src/App/examples/index.js index f890ea7..a4ff528 100644 --- a/src/App/examples/index.js +++ b/src/App/examples/index.js @@ -12,3 +12,4 @@ export { default as Example11 } from './Example11'; export { default as Example12 } from './Example12'; export { default as Example13 } from './Example13'; export { default as Example14 } from './Example14'; +export { default as Example15 } from './Example15'; diff --git a/src/ImageWithZoom/ImageWithZoom.jsx b/src/ImageWithZoom/ImageWithZoom.jsx index 9784082..1c1bb72 100644 --- a/src/ImageWithZoom/ImageWithZoom.jsx +++ b/src/ImageWithZoom/ImageWithZoom.jsx @@ -25,7 +25,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component { srcZoomed: PropTypes.string, tag: PropTypes.string, isPinchZoomEnabled: PropTypes.bool, - } + onlyZoomOnClick: PropTypes.bool, + }; static defaultProps = { alt: undefined, @@ -40,7 +41,8 @@ const ImageWithZoom = class ImageWithZoom extends React.Component { onError: null, srcZoomed: null, tag: 'div', - } + onlyZoomOnClick: false, + }; /** * Find the midpoint between two touches. @@ -86,6 +88,9 @@ const ImageWithZoom = class ImageWithZoom extends React.Component { // tracks the status via image element's onerror events. isImageLoadingError: true, + // tracks if the user has clicked on the image or not. + clicked: false, + // the mouse is currently hovering over the image. isHovering: false, @@ -156,6 +161,7 @@ const ImageWithZoom = class ImageWithZoom extends React.Component { if (this.state.isZooming) return; this.setState({ isHovering: false, + clicked: false, scale: 1, }); } @@ -303,6 +309,7 @@ const ImageWithZoom = class ImageWithZoom extends React.Component { src, srcZoomed, tag: Tag, + onlyZoomOnClick, ...filteredProps } = this.props; @@ -333,8 +340,38 @@ const ImageWithZoom = class ImageWithZoom extends React.Component { overlayStyle.transform = `scale(${this.state.scale})`; } + const overlayImage = ( + + ); + + let cursorStyle = { cursor: 'zoom-in' }; + if (onlyZoomOnClick) { + cursorStyle = this.state.clicked ? { cursor: 'zoom-out' } : { cursor: 'zoom-in' }; + } + return ( - + { + this.setState(state => ({ clicked: !state.clicked })); + }} + style={cursorStyle} + > {alt} - + {((onlyZoomOnClick && this.state.clicked) || !onlyZoomOnClick) + ? overlayImage : <>} {this.renderLoading()} ); diff --git a/src/ImageWithZoom/ImageWithZoom.scss b/src/ImageWithZoom/ImageWithZoom.scss index d1744b7..0c83d56 100644 --- a/src/ImageWithZoom/ImageWithZoom.scss +++ b/src/ImageWithZoom/ImageWithZoom.scss @@ -12,7 +12,6 @@ bottom: 0; right: 0; opacity: 0; - cursor: zoom-in; transition: opacity 300ms, transform 300ms; } diff --git a/src/ImageWithZoom/__tests__/ImageWithZoom.test.jsx b/src/ImageWithZoom/__tests__/ImageWithZoom.test.jsx index 87b5c59..28219fc 100644 --- a/src/ImageWithZoom/__tests__/ImageWithZoom.test.jsx +++ b/src/ImageWithZoom/__tests__/ImageWithZoom.test.jsx @@ -276,6 +276,15 @@ describe('', () => { expect(wrapper.state('x')).toBe('50%'); expect(wrapper.state('y')).toBe('50%'); }); + it('if onlyZoomOnClick then zoom should not happen if not clicked', () => { + const wrapper = shallow(); + expect(wrapper.state('clicked')).toBe(false); + expect(wrapper.find('.overlay').exists()).toBe(false); + wrapper.simulate('click'); + wrapper.update(); + expect(wrapper.find('.overlay').exists()).toBe(true); + expect(wrapper.state('clicked')).toBe(true); + }); it('should properly set state for x y when touches are moving', () => { const wrapper = shallow(); expect(wrapper.state('x')).toBe(null);