Skip to content

Commit 4b89840

Browse files
committed
Refactor src/app/pages/Dashboard/Content/DragLayer/SelectedSandboxItems/AnimatedSandboxItem to Typescript and functional component with hook
1 parent a3ed79c commit 4b89840

File tree

3 files changed

+105
-95
lines changed

3 files changed

+105
-95
lines changed

packages/app/src/app/pages/Dashboard/Content/DragLayer/SelectedSandboxItems/AnimatedSandboxItem.js

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React, { useState, useEffect } from 'react';
2+
import { Spring, animated, interpolate } from 'react-spring/renderprops';
3+
4+
import {
5+
Container,
6+
SandboxImageContainer,
7+
SandboxImage,
8+
SandboxInfo,
9+
} from './elements';
10+
11+
type Props = {
12+
id: string;
13+
i: number;
14+
x: number;
15+
y: number;
16+
scale: number;
17+
isLast: boolean;
18+
selectedSandboxes: Array<string>;
19+
};
20+
21+
export const AnimatedSandboxItem: React.FC<Props> = ({
22+
id,
23+
i,
24+
x,
25+
y,
26+
scale,
27+
isLast,
28+
selectedSandboxes,
29+
}) => {
30+
const [render, setRender] = useState(true);
31+
const [position, setPosition] = useState<DOMRect>(null);
32+
33+
useEffect(() => {
34+
let timeout: NodeJS.Timeout;
35+
36+
const sandboxBrotherItem = document.getElementById(id);
37+
38+
if (sandboxBrotherItem) {
39+
setPosition(sandboxBrotherItem.getBoundingClientRect() as DOMRect);
40+
}
41+
42+
if (i !== 0 && !isLast) {
43+
timeout = global.setTimeout(() => {
44+
setRender(false);
45+
}, 200);
46+
}
47+
48+
return () => {
49+
if (timeout) {
50+
global.clearTimeout(timeout);
51+
}
52+
};
53+
}, [id, i, isLast]);
54+
55+
if (!render || !position) {
56+
return null;
57+
}
58+
59+
return (
60+
<Spring
61+
native
62+
immediate={i === 0 ? el => el !== 'scale' : false}
63+
from={{ x: position.x, y: position.y, shadow: 2, scale: 1 }}
64+
to={{ scale, x, y, shadow: isLast ? 16 : 2 }}
65+
key={id}
66+
>
67+
{({ x: newX, y: newY, scale: newScale, shadow: newShadow }) => (
68+
<animated.div
69+
style={{
70+
position: 'absolute',
71+
willChange: 'transform',
72+
boxShadow:
73+
i === 0 || isLast
74+
? interpolate(
75+
[newShadow],
76+
s => `0 ${s}px ${s * 2}px rgba(0, 0, 0, 0.3)`
77+
)
78+
: 'inherit',
79+
transform: interpolate(
80+
[newX, newY, newScale],
81+
(xx, yy, zz) =>
82+
`translate3d(${xx}px, ${yy}px, 0px) scale3d(${zz}, ${zz}, ${zz})`
83+
),
84+
zIndex: i === 0 ? 20 : 10,
85+
}}
86+
>
87+
<Container>
88+
<SandboxImageContainer>
89+
<SandboxImage
90+
style={{
91+
backgroundImage: `url(${`https://codesandbox.io/api/v1/sandboxes/${id}/screenshot.png`})`,
92+
}}
93+
/>
94+
</SandboxImageContainer>
95+
<SandboxInfo>
96+
{selectedSandboxes.length}{' '}
97+
{selectedSandboxes.length === 1 ? 'Sandbox' : 'Sandboxes'}
98+
</SandboxInfo>
99+
</Container>
100+
</animated.div>
101+
)}
102+
</Spring>
103+
);
104+
};

packages/app/src/app/pages/Dashboard/Content/DragLayer/SelectedSandboxItems/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useMemo } from 'react';
22
import { useOvermind } from 'app/overmind';
33

4-
import AnimatedSandboxItem from './AnimatedSandboxItem';
4+
import { AnimatedSandboxItem } from './AnimatedSandboxItem';
55

66
interface ISelectedSandboxItemsProps {
77
x: number;

0 commit comments

Comments
 (0)