-
Notifications
You must be signed in to change notification settings - Fork 38
/
react-spring-example.js
73 lines (68 loc) · 2.1 KB
/
react-spring-example.js
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
import React, { useRef } from 'react'
import { useTransition, useChain, config, animated } from 'react-spring'
const TransitionGrid = ({ visible, items, removeItem }) => {
const containerRef = useRef()
// https://react-spring.surge.sh/#/useTransition
const containerTransition = useTransition(visible, item => item, {
ref: containerRef,
config: config.stiff,
from: { opacity: 0, x: -500 },
enter: { opacity: 1, x: 0 },
leave: { opacity: 0, x: 500 },
unique: true,
reset: true
})
const cardsRef = useRef()
const cardsTransition = useTransition(visible ? items : [], item => item, {
ref: cardsRef,
config: config.stiff,
from: { opacity: 0, translateY: -30 },
enter: { opacity: 1, translateY: 0 },
leave: { opacity: 0, translateY: -30 },
trail: 400 / items.length,
unique: true,
reset: true
})
// https://react-spring.surge.sh/#/useChain
useChain(visible ? [containerRef, cardsRef] : [cardsRef, containerRef], [
0,
visible ? 0.1 : 0.6
])
return (
<div>
{containerTransition.map(
({ item, key, props: { x, opacity } }) =>
item && (
<animated.div
key={key}
style={{
opacity,
transform: x.interpolate(x => `translateX(${x}px)`)
}}
className="grid animated-grid"
>
{cardsTransition.map(
({ item, key, props: { translateY, opacity } }) => (
<animated.div
className="card"
key={key}
style={{
opacity,
transform: translateY.interpolate(
s => `translateY(${s}px)`
)
}}
onClick={() => removeItem(item)}
>
<div className="close-card">✕</div>
<div>{item}</div>
</animated.div>
)
)}
</animated.div>
)
)}
</div>
)
}
export default TransitionGrid