UNPKG

1.44 kBJavaScriptView Raw
1import React, { useState, useEffect } from "react";
2import posed from "react-pose";
3
4const Frame = posed.div({
5 init: {
6 applyAtEnd: { display: "none" },
7 opacity: 0,
8 },
9 zoom: {
10 applyAtStart: { display: "block" },
11 opacity: 1,
12 },
13});
14
15const transition = {
16 duration: 400,
17 ease: [0.08, 0.69, 0.2, 0.99],
18};
19
20const Image = posed.img({
21 init: {
22 position: "static",
23 width: "auto",
24 height: "auto",
25 transition,
26 flip: true,
27 },
28 zoom: {
29 position: "fixed",
30 top: 0,
31 left: 0,
32 right: 0,
33 bottom: 0,
34 transition,
35 flip: true,
36 },
37});
38
39export default function ZoomImg(props) {
40 const [zoomed, setZoomed] = useState(false);
41 const toggle = () => setZoomed(!zoomed);
42 const pose = zoomed ? "zoom" : "init";
43
44 useEffect(() => {
45 zoomed
46 ? window.addEventListener("scroll", toggle)
47 : window.removeEventListener("scroll", toggle);
48 }, [zoomed]);
49
50 const boxStyle = { width: props.imageWidth, height: props.imageHeight };
51
52 return (
53 <div style={boxStyle} onClick={toggle}>
54 <Frame pose={pose} style={frameStyle} />
55 <Image pose={pose} {...props} style={imgStyle} />
56 </div>
57 );
58}
59
60const frameStyle = {
61 position: "fixed",
62 top: 0,
63 left: 0,
64 right: 0,
65 bottom: 0,
66 display: "none",
67 background: "white",
68 transform: "translateZ(0)",
69};
70
71const imgStyle = {
72 cursor: "zoom-in",
73 display: "block",
74 maxWidth: "100%",
75 margin: "auto",
76};