Skip to content

Commit a7b3784

Browse files
🔨 Refactor, 🧠 Overmind, Hactoberfest | packages/app/src/app/p… (codesandbox#2863)
🔨 Refactor, 🧠 Overmind, Hactoberfest | packages/app/src/app/pages/Dashboard/Content/DragLayer/SelectedSandboxItems
2 parents 4e483af + 560923f commit a7b3784

File tree

6 files changed

+154
-137
lines changed

6 files changed

+154
-137
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/elements.js renamed to packages/app/src/app/pages/Dashboard/Content/DragLayer/SelectedSandboxItems/elements.ts

File renamed without changes.

‎packages/app/src/app/pages/Dashboard/Content/DragLayer/SelectedSandboxItems/index.js‎

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { useMemo } from 'react';
2+
import { useOvermind } from 'app/overmind';
3+
4+
import { AnimatedSandboxItem } from './AnimatedSandboxItem';
5+
6+
interface ISelectedSandboxItemsProps {
7+
x: number;
8+
y: number;
9+
isOverPossibleTargets: boolean;
10+
id: string;
11+
}
12+
13+
export const SelectedSandboxItems: React.FC<ISelectedSandboxItemsProps> = ({
14+
x,
15+
y,
16+
isOverPossibleTargets,
17+
id,
18+
}) => {
19+
const {
20+
state: {
21+
dashboard: { selectedSandboxes },
22+
},
23+
} = useOvermind();
24+
25+
const selectedIds = useMemo(
26+
() => [id, ...selectedSandboxes.filter(b => b !== id)],
27+
[id, selectedSandboxes]
28+
);
29+
30+
const scale = isOverPossibleTargets ? 0.4 : 0.8;
31+
32+
return (
33+
<>
34+
{selectedIds.map((sid, i) => (
35+
<AnimatedSandboxItem
36+
key={sid}
37+
id={sid}
38+
i={i}
39+
isLast={i === selectedIds.length - 1}
40+
x={x}
41+
y={y}
42+
scale={scale}
43+
selectedSandboxes={selectedIds}
44+
/>
45+
))}
46+
</>
47+
);
48+
};

‎packages/app/src/app/pages/Dashboard/Content/DragLayer/index.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22

33
import { DragLayer as DNDDragLayer } from 'react-dnd';
44

5-
import SelectedSandboxItems from './SelectedSandboxItems';
5+
import { SelectedSandboxItems } from './SelectedSandboxItems';
66

77
const layerStyles = {
88
position: 'fixed',
@@ -22,13 +22,11 @@ function getItemCoords(props) {
2222
return {
2323
x,
2424
y,
25-
left: props.item.left,
26-
top: props.item.top,
2725
};
2826
}
2927

3028
class CustomDragLayer extends React.Component {
31-
renderItem(type, item, isOverPossibleTargets, { x, y, left, top }) {
29+
renderItem(type, item, isOverPossibleTargets, { x, y }) {
3230
if (type !== 'SANDBOX') {
3331
return null;
3432
}
@@ -37,8 +35,6 @@ class CustomDragLayer extends React.Component {
3735
isOverPossibleTargets={isOverPossibleTargets}
3836
x={x}
3937
y={y}
40-
left={left}
41-
top={top}
4238
id={item.id}
4339
/>
4440
);

0 commit comments

Comments
 (0)