Skip to content

Commit 5ba62cd

Browse files
committed
Attempted fix for issue #19
Read props at render time, instead of in the constructor. That way if props change we're always reading the latest values.
1 parent b867091 commit 5ba62cd

1 file changed

Lines changed: 47 additions & 65 deletions

File tree

src/index.js

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,83 @@
11
/* eslint jsx-a11y/mouse-events-have-key-events: 0 */
2-
import React, { Component, createRef } from 'react'
3-
import PropTypes from 'prop-types'
4-
import styles from './style.css'
2+
import React, { Component, createRef } from "react";
3+
import PropTypes from "prop-types";
4+
import styles from "./style.css";
55

66
class Zoom extends Component {
7-
constructor (props) {
8-
super(props)
7+
constructor(props) {
8+
super(props);
99

1010
this.state = {
1111
zoom: false,
1212
mouseX: null,
1313
mouseY: null,
14-
}
14+
};
1515

16-
const {
17-
height,
18-
img,
19-
transitionTime,
20-
width,
21-
} = props
22-
23-
this.outerDivStyle = {
24-
height: `${height}px`,
25-
width: `${width}px`,
26-
overflow: 'hidden',
27-
}
16+
this.imageRef = createRef();
2817

29-
this.innerDivStyle = {
30-
height: `${height}px`,
31-
backgroundRepeat: 'no-repeat',
32-
backgroundPosition: 'center',
33-
backgroundSize: 'auto 100%',
34-
transition: `transform ${transitionTime}s ease-out`,
35-
backgroundImage: `url('${img}')`,
36-
}
37-
38-
this.imageRef = createRef()
39-
40-
this.handleMouseOver = this.handleMouseOver.bind(this)
41-
this.handleMouseOut = this.handleMouseOut.bind(this)
42-
this.handleMouseMovement = this.handleMouseMovement.bind(this)
18+
this.handleMouseOver = this.handleMouseOver.bind(this);
19+
this.handleMouseOut = this.handleMouseOut.bind(this);
20+
this.handleMouseMovement = this.handleMouseMovement.bind(this);
4321
}
4422

45-
handleMouseOver () {
23+
handleMouseOver() {
4624
this.setState({
4725
zoom: true,
48-
})
26+
});
4927
}
5028

51-
handleMouseOut () {
29+
handleMouseOut() {
5230
this.setState({
5331
zoom: false,
54-
})
32+
});
5533
}
5634

57-
handleMouseMovement (e) {
58-
const {
59-
left: offsetLeft,
60-
top: offsetTop,
61-
} = this.imageRef.current.getBoundingClientRect()
35+
handleMouseMovement(e) {
36+
const { left: offsetLeft, top: offsetTop } =
37+
this.imageRef.current.getBoundingClientRect();
6238

6339
const {
6440
current: {
65-
style: {
66-
height,
67-
width,
68-
},
41+
style: { height, width },
6942
},
70-
} = this.imageRef
43+
} = this.imageRef;
7144

72-
const x = ((e.pageX - offsetLeft) / parseInt(width, 10)) * 100
73-
const y = ((e.pageY - offsetTop) / parseInt(height, 10)) * 100
45+
const x = ((e.pageX - offsetLeft) / parseInt(width, 10)) * 100;
46+
const y = ((e.pageY - offsetTop) / parseInt(height, 10)) * 100;
7447

7548
this.setState({
7649
mouseX: x,
7750
mouseY: y,
78-
})
51+
});
7952
}
8053

81-
render () {
82-
const {
83-
mouseX,
84-
mouseY,
85-
zoom,
86-
} = this.state
54+
render() {
55+
const { mouseX, mouseY, zoom } = this.state;
8756

88-
const {
89-
zoomScale,
90-
} = this.props
57+
const { zoomScale, height, width, img, transitionTime } = this.props;
9158

9259
const transform = {
9360
transformOrigin: `${mouseX}% ${mouseY}%`,
94-
}
61+
};
62+
63+
const outerDivStyle = {
64+
height: `${height}px`,
65+
width: `${width}px`,
66+
overflow: "hidden",
67+
};
68+
69+
const innerDivStyle = {
70+
height: `${height}px`,
71+
backgroundRepeat: "no-repeat",
72+
backgroundPosition: "center",
73+
backgroundSize: "auto 100%",
74+
transition: `transform ${transitionTime}s ease-out`,
75+
backgroundImage: `url('${img}')`,
76+
};
9577

9678
return (
9779
<div
98-
style={this.outerDivStyle}
80+
style={outerDivStyle}
9981
onMouseOver={this.handleMouseOver}
10082
onMouseOut={this.handleMouseOut}
10183
onMouseMove={this.handleMouseMovement}
@@ -105,12 +87,12 @@ class Zoom extends Component {
10587
style={{
10688
...transform,
10789
...this.innerDivStyle,
108-
transform: zoom ? `scale(${zoomScale})` : 'scale(1.0)',
90+
transform: zoom ? `scale(${zoomScale})` : "scale(1.0)",
10991
}}
11092
className={styles.zoomImg}
11193
/>
11294
</div>
113-
)
95+
);
11496
}
11597
}
11698

@@ -125,10 +107,10 @@ Zoom.propTypes = {
125107
width: PropTypes.number.isRequired,
126108
/** The time (in seconds) that will take to scale your image. */
127109
transitionTime: PropTypes.number,
128-
}
110+
};
129111

130112
Zoom.defaultProps = {
131113
transitionTime: 0.1,
132-
}
114+
};
133115

134-
export default Zoom
116+
export default Zoom;

0 commit comments

Comments
 (0)