Skip to content

Commit cdab145

Browse files
author
Rose Syrett
committed
Zoom only on click
Add a prop which allows images to only be zoomed in if they are clicked.
1 parent 1a10f5a commit cdab145

File tree

6 files changed

+92
-20
lines changed

6 files changed

+92
-20
lines changed

src/App/App.jsx

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Example12,
1515
Example13,
1616
Example14,
17+
Example15,
1718
} from './examples';
1819
import s from './style.scss';
1920

@@ -255,6 +256,13 @@ class DevelopmentApp extends React.Component {
255256
</section>
256257
)}
257258

259+
{(value === '0' || value === '14') && (
260+
<section id="example--14">
261+
<hr />
262+
<Example15 />
263+
</section>
264+
)}
265+
258266
</div>
259267
</div>
260268
</div>
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
import
3+
{
4+
ButtonBack,
5+
ButtonFirst,
6+
ButtonLast,
7+
ButtonNext,
8+
CarouselProvider,
9+
Slide,
10+
Slider,
11+
ImageWithZoom,
12+
DotGroup,
13+
} from '../../..';
14+
15+
import s from '../../style.scss';
16+
17+
export default () => (
18+
<CarouselProvider
19+
visibleSlides={1}
20+
totalSlides={2}
21+
step={1}
22+
naturalSlideWidth={400}
23+
naturalSlideHeight={500}
24+
>
25+
<h2 className={s.headline}>Zoom only on click</h2>
26+
<p>Two images: first will zoom on click, second will not.</p>
27+
<Slider className={s.slider}>
28+
<Slide tag="a" index={0}>
29+
<ImageWithZoom src="./media/img01.jpeg" onlyZoomOnClick />
30+
</Slide>
31+
<Slide tag="a" index={1}>
32+
<ImageWithZoom src="./media/img02.jpeg" />
33+
</Slide>
34+
</Slider>
35+
<ButtonFirst>First</ButtonFirst>
36+
<ButtonBack>Back</ButtonBack>
37+
<ButtonNext>Next</ButtonNext>
38+
<ButtonLast>Last</ButtonLast>
39+
<DotGroup dotNumbers />
40+
</CarouselProvider>
41+
);

src/App/examples/Example15/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Example15 from './Example15';
2+
3+
export default Example15;

src/App/examples/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export { default as Example11 } from './Example11';
1212
export { default as Example12 } from './Example12';
1313
export { default as Example13 } from './Example13';
1414
export { default as Example14 } from './Example14';
15+
export { default as Example15 } from './Example15';

src/ImageWithZoom/ImageWithZoom.jsx

+39-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
33
import Image from '../Image';
44
import 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,32 @@ 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+
this.setState({clicked: !this.state.clicked});
366+
}}
367+
style={cursorStyle}
368+
>
338369
<Image
339370
alt={alt}
340371
className={newImageClassName}
@@ -344,21 +375,10 @@ const ImageWithZoom = class ImageWithZoom extends React.Component {
344375
onError={this.handleImageLoadError}
345376
{...bgImageProps}
346377
/>
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+
362382
{this.renderLoading()}
363383
</Tag>
364384
);

src/ImageWithZoom/ImageWithZoom.scss

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
bottom: 0;
1313
right: 0;
1414
opacity: 0;
15-
cursor: zoom-in;
1615
transition: opacity 300ms, transform 300ms;
1716
}
1817

0 commit comments

Comments
 (0)