aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/trails/SlideEffect.tsx
blob: 7e2985a6adf4b63efea4d59dc806e38480b8ea80 (plain)
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
206
import { Button } from '@mui/material';
import { useSpring, animated, easings, to } from '@react-spring/web';
import React, { useEffect, useState } from 'react';
import { PresEffectDirection } from './PresEnums';

export enum EffectType {
    ZOOM = 'zoom',
    FADE = 'fade',
    BOUNCE = 'bounce',
    ROTATE = 'rotate',
}

interface Props {
    dir: PresEffectDirection;
    effectType: EffectType;
    friction: number;
    tension: number;
    mass: number;
    children: React.ReactNode;
}

// TODO: add visibility detector when the slide comes into view
export default function SpringAnimation({ dir, friction, tension, mass, effectType, children }: Props) {
    const [springs, api] = useSpring(
        () => ({
            from: {
                x: 0,
                y: 0,
                opacity: 0,
                scale: 1,
            },
            config: {
                tension: tension,
                friction: friction,
                mass: mass,
            },
            onStart: () => {
                console.log('started');
            },
            onRest: () => {
                console.log('resting');
            },
        }),
        [tension, friction, mass]
    );

    //   Whether the animation is currently playing
    const [animating, setAnimating] = useState(false);

    const zoomConfig = {
        from: {
            scale: 0,
            x: 0,
            y: 0,
            opacity: 1,
            // opacity: 0,
        },
        to: {
            scale: 1,
            x: 0,
            y: 0,
            opacity: 1,
            // opacity: 1,
            config: {
                tension: tension,
                friction: friction,
                mass: mass,
            },
        },
    };

    const fadeConfig = {
        from: {
            opacity: 0,
            scale: 1,
            x: 0,
            y: 0,
        },
        to: {
            opacity: 1,
            scale: 1,
            x: 0,
            y: 0,
            config: {
                tension: tension,
                friction: friction,
                mass: mass,
            },
        },
    };

    const rotateConfig = {
        from: {
            x: 0,
        },
        to: {
            x: 360,
            config: {
                tension: tension,
                friction: friction,
                mass: mass,
            },
        },
    };

    const getBounceConfigFrom = () => {
        switch (dir) {
            case PresEffectDirection.Left:
                return {
                    from: {
                        opacity: 0,
                        x: -200,
                        y: 0,
                    },
                };
            case PresEffectDirection.Right:
                return {
                    from: {
                        opacity: 0,
                        x: 200,
                        y: 0,
                    },
                };
            case PresEffectDirection.Top:
                return {
                    from: {
                        opacity: 0,
                        x: 0,
                        y: -200,
                    },
                };
            case PresEffectDirection.Bottom:
                return {
                    from: {
                        opacity: 0,
                        x: 0,
                        y: 200,
                    },
                };
            default:
                return {
                    from: {
                        opacity: 0,
                        x: 0,
                        y: 0,
                    },
                };
        }
    };

    const bounceConfig = {
        ...getBounceConfigFrom(),
        to: [
            {
                opacity: 1,
                x: 0,
                y: 0,
                config: {
                    tension: tension,
                    friction: friction,
                    mass: mass,
                },
            },
        ],
    };

    const handleClick = () => {
        if (effectType === EffectType.BOUNCE) {
            api.start(bounceConfig);
        } else if (effectType === EffectType.ZOOM) {
            api.start(zoomConfig);
        } else if (effectType === EffectType.ROTATE) {
            api.start(rotateConfig);
        } else if (effectType === EffectType.FADE) {
            api.start(fadeConfig);
        }
    };

    useEffect(() => {
        setTimeout(() => {
            handleClick();
        }, 200);
    }, []);

    return (
        <div
        // className="demo"
        // onPointerEnter={() => {
        //     handleClick();
        // }}
        >
            {effectType !== EffectType.ROTATE ? (
                <animated.div
                    style={{
                        // display: 'inline-block',
                        // transform: 'translate(50px, 50px)',
                        ...springs,
                    }}>
                    {children}
                </animated.div>
            ) : (
                <animated.div style={{ transform: to(springs.x, val => `rotate(${val}deg)`) }}>{children}</animated.div>
            )}
        </div>
    );
}