Skip to content

Commit 8e87541

Browse files
committed
Improve code style
1 parent 6d1258a commit 8e87541

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

src/Magnifier.tsx

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import "./style.scss";
2+
13
import debounce from "lodash.debounce";
24
import throttle from "lodash.throttle";
35
import React, { PureComponent } from "react";
4-
import "./style.scss";
56

67
type mgShape = "circle" | "square";
78

@@ -41,20 +42,6 @@ interface State {
4142
}
4243

4344
export default class Magnifier extends PureComponent<Props, State> {
44-
state: Readonly<State> = {
45-
showZoom: false,
46-
mgOffsetX: 0,
47-
mgOffsetY: 0,
48-
relX: 0,
49-
relY: 0,
50-
};
51-
52-
calcImgBoundsDebounced: () => void;
53-
54-
img: HTMLElement;
55-
56-
imgBounds: DOMRect | ClientRect;
57-
5845
static defaultProps = {
5946
// Image
6047
width: "100%",
@@ -76,21 +63,29 @@ export default class Magnifier extends PureComponent<Props, State> {
7663
mgTouchOffsetY: -50,
7764
};
7865

66+
state: Readonly<State> = {
67+
showZoom: false,
68+
mgOffsetX: 0,
69+
mgOffsetY: 0,
70+
relX: 0,
71+
relY: 0,
72+
};
73+
74+
calcImgBoundsDebounced: () => void;
75+
76+
img: HTMLElement;
77+
78+
imgBounds: DOMRect | ClientRect;
79+
7980
constructor(props: Props) {
8081
super(props);
8182

82-
// Function bindings
83-
this.onMouseEnter = this.onMouseEnter.bind(this);
8483
this.onMouseMove = throttle(this.onMouseMove.bind(this), 20, { trailing: false });
85-
this.onMouseOut = this.onMouseOut.bind(this);
86-
this.onTouchStart = this.onTouchStart.bind(this);
8784
this.onTouchMove = throttle(this.onTouchMove.bind(this), 20, { trailing: false });
88-
this.onTouchEnd = this.onTouchEnd.bind(this);
89-
this.calcImgBounds = this.calcImgBounds.bind(this);
9085
this.calcImgBoundsDebounced = debounce(this.calcImgBounds, 200);
9186
}
9287

93-
componentDidMount(): void {
88+
componentDidMount = (): void => {
9489
// Add mouse/touch event listeners to image element (assigned in render function)
9590
// `passive: false` prevents scrolling on touch move
9691
this.img.addEventListener("mouseenter", this.onMouseEnter, { passive: false });
@@ -104,9 +99,9 @@ export default class Magnifier extends PureComponent<Props, State> {
10499
window.addEventListener("resize", this.calcImgBoundsDebounced);
105100
// Re-calculate image bounds on scroll (useCapture: catch scroll events in entire DOM)
106101
window.addEventListener("scroll", this.calcImgBoundsDebounced, true);
107-
}
102+
};
108103

109-
componentWillUnmount(): void {
104+
componentWillUnmount = (): void => {
110105
// Remove all event listeners
111106
this.img.removeEventListener("mouseenter", this.onMouseEnter);
112107
this.img.removeEventListener("mousemove", this.onMouseMove);
@@ -116,13 +111,13 @@ export default class Magnifier extends PureComponent<Props, State> {
116111
this.img.removeEventListener("touchend", this.onTouchEnd);
117112
window.removeEventListener("resize", this.calcImgBoundsDebounced);
118113
window.removeEventListener("scroll", this.calcImgBoundsDebounced, true);
119-
}
114+
};
120115

121-
onMouseEnter(): void {
116+
onMouseEnter = (): void => {
122117
this.calcImgBounds();
123-
}
118+
};
124119

125-
onMouseMove(e: MouseEvent): void {
120+
onMouseMove = (e: MouseEvent): void => {
126121
const { mgMouseOffsetX, mgMouseOffsetY } = this.props;
127122

128123
if (this.imgBounds) {
@@ -138,21 +133,21 @@ export default class Magnifier extends PureComponent<Props, State> {
138133
showZoom: true,
139134
});
140135
}
141-
}
136+
};
142137

143-
onMouseOut(): void {
138+
onMouseOut = (): void => {
144139
this.setState({
145140
showZoom: false,
146141
});
147-
}
142+
};
148143

149-
onTouchStart(e: TouchEvent): void {
144+
onTouchStart = (e: TouchEvent): void => {
150145
e.preventDefault(); // Prevent mouse event from being fired
151146

152147
this.calcImgBounds();
153-
}
148+
};
154149

155-
onTouchMove(e: TouchEvent): void {
150+
onTouchMove = (e: TouchEvent): void => {
156151
e.preventDefault(); // Disable scroll on touch
157152

158153
if (this.imgBounds) {
@@ -176,21 +171,21 @@ export default class Magnifier extends PureComponent<Props, State> {
176171
});
177172
}
178173
}
179-
}
174+
};
180175

181-
onTouchEnd(): void {
176+
onTouchEnd = (): void => {
182177
this.setState({
183178
showZoom: false,
184179
});
185-
}
180+
};
186181

187-
calcImgBounds(): void {
182+
calcImgBounds = (): void => {
188183
if (this.img) {
189184
this.imgBounds = this.img.getBoundingClientRect();
190185
}
191-
}
186+
};
192187

193-
render(): React.ReactElement {
188+
render = (): React.ReactElement => {
194189
/* eslint-disable @typescript-eslint/no-unused-vars */
195190
const {
196191
src,
@@ -231,7 +226,8 @@ export default class Magnifier extends PureComponent<Props, State> {
231226
overflow: mgShowOverflow ? "visible" : "hidden",
232227
}}
233228
>
234-
<img // eslint-disable-line jsx-a11y/alt-text
229+
{/* eslint-disable-next-line jsx-a11y/alt-text */}
230+
<img
235231
className="magnifier-image"
236232
src={src}
237233
width="100%"
@@ -263,5 +259,5 @@ export default class Magnifier extends PureComponent<Props, State> {
263259
)}
264260
</div>
265261
);
266-
}
262+
};
267263
}

0 commit comments

Comments
 (0)