-
Notifications
You must be signed in to change notification settings - Fork 53
/
App.jsx
311 lines (275 loc) · 9.73 KB
/
App.jsx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* eslint-disable jsx-a11y/label-has-associated-control */
import Lottie from 'react-lottie-player';
import LottieLight from 'react-lottie-player/dist/LottiePlayerLight.modern';
import React, {
useState, memo, useRef, useEffect,
} from 'react';
import Test from './Test';
import lottieJson from './26514-check-success-animation.json';
const boxStyle = {
boxShadow: '0 0 10px 10px rgba(0,0,0,0.03)', width: 200, maxWidth: '100%', margin: 30, padding: 30, borderRadius: 7, display: 'flex', flexDirection: 'column',
};
const ScrollTest = memo(({ Component, useSubframes }) => {
const scrollRef = useRef();
const [animationPosition, setAnimationPosition] = useState(0);
useEffect(() => {
function handleScroll(e) {
setAnimationPosition(Math.max((0, e.target.scrollTop - 50) * 0.3));
}
const scroller = scrollRef.current;
scroller.addEventListener('scroll', handleScroll, { passive: true });
return () => {
scroller.removeEventListener('scroll', handleScroll);
};
}, []);
return (
<div ref={scrollRef} style={{ ...boxStyle, height: 400, overflowY: 'scroll' }}>
<div style={{ textAlign: 'center' }}>
<p>Scroll down</p>
<span style={{ fontSize: 40 }} role="img" aria-label="Scroll down">⬇️</span>
</div>
<Component
animationData={lottieJson}
goTo={animationPosition}
useSubframes={useSubframes}
style={{
width: 150, height: 150, alignSelf: 'center', marginTop: 200, marginBottom: 300,
}}
/>
</div>
);
});
const MainTest = memo(({ Component, useSubframes }) => {
const [segmentFrom, setSegmentFrom] = useState(0);
const [segmentTo, setSegmentTo] = useState(70);
const [segmentsEnabled, setSegmentsEnabled] = useState(false);
const [play, setPlay] = useState(true);
const [loop, setLoop] = useState(true);
const [loopTimes, setLoopTimes] = useState(0);
const [speed, setSpeed] = useState(1);
const [direction, setDirection] = useState(1);
const segments = [segmentFrom, segmentTo];
const lottieRef = useRef();
const logCounter = useRef(0);
const [log, setLog] = useState([]);
const addLog = (line) => {
logCounter.current += 1;
setLog((existing) => [{ text: line, n: logCounter.current }, ...existing]);
};
function handleLoopTimesChange(e) {
const { value } = e.target;
setLoopTimes(value);
const n = parseInt(value, 10);
if (!Number.isInteger(n)) return;
setLoop(n);
}
function getLoopVal() {
if (loop === true) return '';
if (loop === false) return 0;
return loopTimes;
}
useEffect(() => {
// eslint-disable-next-line no-console
console.log('Your lottie object', lottieRef.current);
}, []);
return (
<div style={boxStyle}>
<Component
ref={lottieRef}
loop={loop}
speed={speed}
play={play}
animationData={lottieJson}
direction={direction}
segments={segmentsEnabled && segments}
useSubframes={useSubframes}
style={{
width: 150, height: 150, marginBottom: 10, alignSelf: 'center',
}}
onComplete={() => addLog('complete')}
onLoopComplete={() => addLog('loopComplete')}
onEnterFrame={() => { /* addLog('enterFrame') */ }}
onSegmentStart={() => addLog('segmentStart')}
onLoad={() => addLog('load')}
/>
<div style={{ margin: '7px 0' }}>
<input type="checkbox" checked={loop} onChange={(e) => setLoop(e.target.checked)} id="loop" />
{' '}
<label htmlFor="loop">Loop</label>
</div>
<div style={{ margin: '7px 0' }}>
Loop times
<br />
<input type="number" value={getLoopVal()} onChange={handleLoopTimesChange} />
</div>
<div style={{ margin: '7px 0' }}>
<input type="checkbox" checked={play} onChange={(e) => setPlay(e.target.checked)} id="playing1" />
{' '}
<label htmlFor="playing1">Playing</label>
</div>
<div style={{ margin: '7px 0' }}>
<div style={{ marginBottom: 5 }}>
<input type="checkbox" checked={segmentsEnabled} onChange={(e) => setSegmentsEnabled(e.target.checked)} id="segmentsEnabled" />
<label htmlFor="segmentsEnabled">Segments enabled</label>
</div>
<div style={{ marginLeft: 10 }}>
Segment from
<br />
<input disabled={!segmentsEnabled} type="number" value={segmentFrom} onChange={(e) => setSegmentFrom(parseInt(e.target.value, 10))} />
</div>
<div style={{ marginLeft: 10 }}>
Segment to
<br />
<input disabled={!segmentsEnabled} type="number" value={segmentTo} onChange={(e) => setSegmentTo(parseInt(e.target.value, 10))} />
</div>
</div>
<div style={{ margin: '10px 0' }}>
Speed
<input
style={{ width: '100%' }}
type="range"
min="0"
max="100"
value={speed * 20}
onChange={(e) => setSpeed(parseInt(e.target.value, 10) / 20)}
step="1"
/>
</div>
<div style={{ margin: '10px 0' }}>
Direction
<br />
<input style={{ padding: 5, margin: 5, border: direction === -1 ? '2px solid black' : undefined }} type="button" value="-1" onClick={() => setDirection(-1)} />
<input style={{ padding: 5, margin: 5, border: direction === 1 ? '2px solid black' : undefined }} type="button" value="1" onClick={() => setDirection(1)} />
</div>
<div>Event log</div>
<div style={{
height: 100, overflowY: 'scroll', background: 'rgba(0,0,0,0.03)', borderRadius: 5, padding: 10,
}}
>
{log.map(({ text, n }) => <div key={n}>{text}</div>)}
</div>
</div>
);
});
const RangeTest = memo(({ Component, useSubframes }) => {
const [goTo, setGoTo] = useState(55);
const [play, setPlay] = useState(false);
const [mounted, setMounted] = useState(true);
return (
<div style={boxStyle}>
<div style={{ margin: '7px 0' }}>
<input type="checkbox" checked={mounted} onChange={(e) => setMounted(e.target.checked)} id="mounted1" />
{' '}
<label htmlFor="mounted1">Mounted</label>
</div>
{mounted && (
<>
<Component
play={play}
goTo={goTo}
useSubframes={useSubframes}
animationData={lottieJson}
style={{ width: 150, height: 150, marginBottom: 10 }}
/>
<div style={{ margin: '10px 0' }}>
<input type="checkbox" checked={play} onChange={(e) => setPlay(e.target.checked)} id="playing2" />
<label htmlFor="playing2">Playing</label>
</div>
<div style={{ margin: '10px 0' }}>
Controlled position
<br />
<input
style={{ width: '100%' }}
type="range"
min="0"
max="108"
value={goTo}
onChange={(e) => setGoTo(parseInt(e.target.value, 10))}
step="1"
/>
</div>
</>
)}
</div>
);
});
function PathLoadTest({ Component, useSubframes }) {
return (
<div style={boxStyle}>
<Component
play
loop
useSubframes={useSubframes}
path={`${window.location.href}26514-check-success-animation.json`}
style={{ width: 150, height: 150, marginBottom: 10 }}
/>
<p>
Loaded with
{' '}
<b>path</b>
{' '}
URL
</p>
</div>
);
}
const LazyLoadTest = memo(({ Component, useSubframes }) => {
const [animationData, setAnimationData] = useState();
useEffect(() => {
setTimeout(() => {
import('./26514-check-success-animation.json').then(setAnimationData);
}, 1000);
}, []);
return (
<div style={boxStyle}>
{animationData ? (
<Component
play
loop
useSubframes={useSubframes}
animationData={animationData}
style={{ width: 150, height: 150, marginBottom: 10 }}
/>
) : (
<div>Loading...</div>
)}
<p>
Lazy loaded with <b>import()</b>
</p>
</div>
);
});
function App() {
const [useLottieLight, setUseLottieLight] = useState(false);
const [useSubframes, setUseSubframes] = useState();
const Component = useLottieLight ? LottieLight : Lottie;
return (
<>
<h1 style={{ textAlign: 'center' }}>react-lottie-player Live Demo</h1>
<div style={{
display: 'flex', justifyContent: 'center', alignItems: 'center', flexWrap: 'wrap',
}}
>
<a style={{ margin: '0 .5em' }} href="https://github.com/mifi/react-lottie-player/blob/master/example/src/index.js">View source code</a>
<div style={{ margin: '0 .5em' }}>
<input type="checkbox" checked={useLottieLight} id="useLottieLight" onChange={(e) => setUseLottieLight(e.target.checked)} />
{' '}
<label htmlFor="useLottieLight">Use lottie light?</label>
</div>
<div style={{ margin: '0 .5em' }}>
<input type="checkbox" checked={useSubframes != null ? useSubframes : true} id="useSubframes" onChange={(e) => setUseSubframes(e.target.checked)} />
{' '}
<label htmlFor="useSubframes">Use subframes?</label>
</div>
</div>
<div style={{ display: 'flex', flexWrap: 'wrap', justifyContent: 'center' }}>
<MainTest Component={Component} useSubframes={useSubframes} />
<RangeTest Component={Component} useSubframes={useSubframes} />
<ScrollTest Component={Component} useSubframes={useSubframes} />
<LazyLoadTest Component={Component} useSubframes={useSubframes} />
<PathLoadTest Component={Component} useSubframes={useSubframes} />
</div>
</>
);
}
export default window.location.pathname.startsWith('/test') ? Test : App;