forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.jsx
More file actions
205 lines (187 loc) · 5.45 KB
/
Cube.jsx
File metadata and controls
205 lines (187 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Import External Dependencies
import PropTypes from "prop-types";
import { useEffect, useRef, useState } from "react";
export default function Cube({
hover = false,
theme = "dark",
depth = 30,
repeatDelay = 1000,
className = "",
continuous,
}) {
const [state, setState] = useState({ x: 0, y: 0, z: 0, iteration: 0 });
const containerRef = useRef(null);
const timeoutRef = useRef(null);
const { x, y, z, iteration } = state;
useEffect(() => {
const container = containerRef.current;
if (hover) {
const spin = () => {
const axes = ["x", "y", "z", "iteration"];
const axis = axes[Math.floor(Math.random() * axes.length)];
const sign = Math.random() < 0.5 ? -1 : 1;
setState((prev) => ({ ...prev, [axis]: sign * 90 }));
};
const reset = () => {
setState((prev) => ({ ...prev, x: 0, y: 0, z: 0 }));
};
container.addEventListener("mouseenter", spin);
container.addEventListener("mouseleave", reset);
return () => {
container.removeEventListener("mouseenter", spin);
container.removeEventListener("mouseleave", reset);
};
} else if (continuous) {
let degrees = 0;
const axis = "y";
const animation = () => {
const obj = {};
obj[axis] = degrees += 90;
setState((prev) => ({
...prev,
...obj,
iteration: (prev.iteration + 1) % 4,
}));
// eslint-disable-next-line no-use-before-define
tick();
};
const tick = () =>
setTimeout(() => requestAnimationFrame(animation), repeatDelay);
timeoutRef.current = tick();
return () => clearTimeout(timeoutRef.current);
}
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const getFaces = (type) => {
// Keep the thicker border on
// the outside on each iteration
const borderWidthMap = {
0: {
left: [1, 1, 1, 6],
right: [6, 1, 1, 1],
top: [1, 1, 1, 1],
bottom: [6, 1, 1, 6],
},
1: {
left: [1, 1, 1, 1],
right: [1, 1, 1, 1],
top: [1, 1, 1, 1],
bottom: [1, 1, 1, 1],
},
2: {
left: [1, 1, 6, 6],
right: [6, 6, 1, 1],
top: [6, 1, 1, 6],
bottom: [1, 6, 6, 1],
},
3: {
left: [6, 1, 1, 1],
right: [1, 6, 1, 1],
top: [1, 1, 1, 1],
bottom: [6, 6, 1, 1],
},
4: {
left: [1, 1, 6, 1],
right: [1, 1, 1, 6],
top: [1, 1, 1, 1],
bottom: [1, 1, 6, 6],
},
5: {
left: [1, 6, 1, 1],
right: [1, 1, 6, 1],
top: [1, 1, 1, 1],
bottom: [1, 6, 6, 1],
},
};
// Base classes applied to all faces
const baseFaceClasses = "absolute w-full h-full border-white";
// Distinguish styles for inner vs outer faces natively via Tailwind
const isOuter = type === "outer";
const variantClasses = isOuter
? "border bg-blue-200/50 transition-[border-width] duration-200 delay-200"
: "border-2 bg-blue-400";
return [
"rotateX(0deg)",
"rotateX(-90deg)",
"rotateX(90deg)",
"rotateY(-90deg)",
"rotateY(90deg)",
"rotateY(180deg)",
].map((rotation, i) => {
const borderStyles = isOuter
? {
borderTopWidth: borderWidthMap[i].top[iteration],
borderRightWidth: borderWidthMap[i].right[iteration],
borderBottomWidth: borderWidthMap[i].bottom[iteration],
borderLeftWidth: borderWidthMap[i].left[iteration],
}
: {};
return (
<section
key={i}
className={`cube__face ${baseFaceClasses} ${variantClasses}`}
style={{
transform: `${rotation} translateZ(${depth / 2}px)`,
...borderStyles,
}}
/>
);
});
};
return (
<div
className={`cube__container ${className}`}
style={{
width: `${depth * 1.5}px`,
height: `${depth * 1.5}px`,
paddingLeft: `${depth / 1.7}px`,
}}
>
<span
ref={containerRef}
className={`cube cube--${theme} relative block [transform-style:preserve-3d]`}
style={{
width: `${depth}px`,
paddingBottom: `${depth * 0.5}px`,
transform: "rotateX(-35.5deg) rotateY(45deg)",
}}
>
<figure
className="cube__outer inline-block [transform-style:preserve-3d] transition-transform duration-1000"
style={{
width: `${depth}px`,
height: `${depth}px`,
transform: `translateX(-50%)
scale3d(1,1,1)
rotateX(${x}deg)
rotateY(${y}deg)
rotateZ(${z}deg)`,
}}
>
{getFaces("outer")}
</figure>
<figure
className="cube__inner absolute -top-[2px] left-0 inline-block [transform-style:preserve-3d] transition-transform duration-1000"
style={{
width: `${depth}px`,
height: `${depth}px`,
transform: `translateX(-50%) translateY(2px)
scale3d(0.5,0.5,0.5)
rotateX(${-x}deg)
rotateY(${-y}deg)
rotateZ(${-z}deg)`,
}}
>
{getFaces("inner")}
</figure>
</span>
</div>
);
}
Cube.propTypes = {
hover: PropTypes.bool,
theme: PropTypes.string,
depth: PropTypes.number,
repeatDelay: PropTypes.number,
className: PropTypes.string,
continuous: PropTypes.bool,
};