forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-native-reanimated-tests.tsx
765 lines (695 loc) · 17.6 KB
/
react-native-reanimated-tests.tsx
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useState, useCallback, forwardRef } from 'react';
import {
StyleSheet,
Button,
View,
Image,
FlatListProps,
ViewProps,
ImageProps,
} from 'react-native';
import {
PanGestureHandler,
PinchGestureHandlerGestureEvent,
PinchGestureHandler,
PanGestureHandlerGestureEvent,
FlatList,
} from 'react-native-gesture-handler';
import Animated, {
useSharedValue,
useDerivedValue,
useAnimatedStyle,
useAnimatedScrollHandler,
useAnimatedGestureHandler,
Easing,
withTiming,
withSpring,
cancelAnimation,
withDelay,
withRepeat,
withSequence,
withDecay,
useWorkletCallback,
createWorklet,
runOnUI,
useAnimatedReaction,
interpolateColor,
makeMutable,
interpolateNode,
useValue,
color,
interpolateColors,
createAnimatedPropAdapter,
useAnimatedProps,
useAnimatedRef,
// eslint-disable-next-line import/no-unresolved
} from 'react-native-reanimated';
class Path extends React.Component<{ fill?: string }> {
render() {
return null;
}
}
type Item = {
id: number;
};
const SomeFC = (props: ViewProps) => {
return <View {...props} />;
};
const SomeFCWithRef = forwardRef((props: ViewProps) => {
return <View {...props} />;
});
// Class Component -> Animated Class Component
const AnimatedPath = Animated.createAnimatedComponent(Path);
const AnimatedImage = Animated.createAnimatedComponent(Image);
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
const AnimatedTypedFlatList = Animated.createAnimatedComponent<
FlatListProps<Item[]>
>(FlatList);
// Function Component -> Animated Function Component
const AnimatedFC = Animated.createAnimatedComponent(SomeFC);
const AnimatedFCWithRef = Animated.createAnimatedComponent(SomeFCWithRef);
function CreateAnimatedComponentTest1() {
const animatedProps = useAnimatedProps(() => ({ fill: 'blue' }));
return <AnimatedPath animatedProps={animatedProps} />;
}
function CreateAnimatedComponentTest2() {
const animatedProps = useAnimatedProps(() => ({ fill2: 'blue' }));
return (
// @ts-expect-error
<AnimatedPath animatedProps={animatedProps} />
);
}
function CreateAnimatedComponentTest3() {
const animatedProps = useAnimatedProps(
() => ({ pointerEvents: 'none' } as const)
);
return (
<Animated.View animatedProps={animatedProps}>
<AnimatedPath />
</Animated.View>
);
}
function CreateAnimatedFlatList() {
const renderItem = useCallback(
({ item, index }: { item: Item[]; index: number }) => {
if (Math.random()) {
return null;
}
return <View style={{ width: 100 }}></View>;
},
[]
);
return (
<>
<AnimatedTypedFlatList
style={{ flex: 1 }}
data={[]}
renderItem={renderItem}
/>
<AnimatedFlatList
// @ts-expect-error
style={{ flex: 1, red: false }}
data={[]}
renderItem={() => null}
/>
<AnimatedImage style={{ flex: 1 }} source={{ uri: '' }} />
</>
);
}
function TestClassComponentRef() {
const animatedRef = useAnimatedRef<React.Component<ImageProps>>();
return <AnimatedImage ref={animatedRef} source={{}} />;
}
function TestFunctionComponentRef() {
const animatedRef = useAnimatedRef<React.Component<ViewProps>>();
return (
<AnimatedFC
// @ts-expect-error ref is not available on plain function-components
ref={animatedRef}
/>
);
}
function TestFunctionComponentForwardRef() {
const animatedRef = useAnimatedRef<React.Component<ViewProps>>();
return <AnimatedFCWithRef ref={animatedRef} />;
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
box: {
height: 50,
backgroundColor: 'blue',
},
});
/**
* Reanimated 1
*/
// @TODO: add reanimated 1 tests here
/**
* Reanimated 2 Functions
*/
// makeMutable
function MakeMutableTest() {
const mut = makeMutable(0);
const mut2 = makeMutable(true);
return <Animated.View style={styles.container} />;
}
/**
* Reanimated 2 Hooks
*/
// useSharedValue
function SharedValueTest() {
const translate = useSharedValue(0);
const translate2 = useSharedValue(0);
const translate3 = useSharedValue(0);
const sharedBool = useSharedValue<boolean>(false);
if (sharedBool.value) {
sharedBool.value = false;
}
return <Animated.View style={styles.container} />;
}
// useAnimatedStyle
function AnimatedStyleTest() {
const width = useSharedValue(50);
const animatedStyle = useAnimatedStyle(() => {
return {
width: width.value,
};
});
return <Animated.View style={[styles.box, animatedStyle]} />;
}
// useAnimatedStyle with arrays (invalid return)
function AnimatedStyleArrayTest() {
const width = useSharedValue(50);
// @ts-expect-error since the animated style cannot be an array.
const animatedStyle = useAnimatedStyle(() => {
return [styles.box, { width: width.value }];
});
return <Animated.View style={[styles.box, animatedStyle]} />;
}
// useAnimatedStyle with null (invalid return)
function AnimatedStyleNullTest() {
const width = useSharedValue(50);
// @ts-expect-error since the animated style cannot be "false".
const animatedStyle = useAnimatedStyle(() => false);
return <Animated.View style={[styles.box, animatedStyle]} />;
}
// useAnimatedStyle with number (invalid return)
function AnimatedStyleNumberTest() {
const width = useSharedValue(50);
// @ts-expect-error since the animated style cannot be a number.
const animatedStyle = useAnimatedStyle(() => 5);
return <Animated.View style={[styles.box, animatedStyle]} />;
}
// useDerivedValue
function DerivedValueTest() {
const progress = useSharedValue(0);
const width = useDerivedValue(() => {
return progress.value * 250;
});
// @ts-expect-error width is readonly
width.value = 100;
return (
<Button title="Random" onPress={() => (progress.value = Math.random())} />
);
}
// useAnimatedScrollHandler
function AnimatedScrollHandlerTest() {
const translationY = useSharedValue(0);
const scrollHandler = useAnimatedScrollHandler((event) => {
translationY.value = event.contentOffset.y;
});
const stylez = useAnimatedStyle(() => {
return {
color: 'red',
backgroundColor: 0x00ff00,
transform: [
{
translateY: translationY.value,
},
{
rotate: `${Math.PI}rad`,
},
],
};
});
// @ts-expect-error
const style2 = useAnimatedStyle(() => {
return {
transform: [
{
rotate: Math.PI,
},
],
};
});
// @ts-expect-error
const style3 = useAnimatedStyle(() => {
return {
color: {},
};
});
return (
<View style={styles.container}>
<Animated.View style={[styles.box, stylez]} />
<Animated.ScrollView onScroll={scrollHandler} scrollEventThrottle={16} />
</View>
);
}
// useAnimatedGestureHandler with context
function AnimatedGestureHandlerTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
{ startX: number }
>({
onStart: (_, ctx) => {
ctx.startX = x.value;
},
onActive: (event, ctx) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
x.value = 0;
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
function AnimatedPinchGestureHandlerTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler<PinchGestureHandlerGestureEvent>(
{
onActive: (event) => {
x.value = event.scale;
},
onEnd: () => {
x.value = withTiming(1);
},
}
);
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
scale: x.value,
},
],
};
});
return (
<PinchGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PinchGestureHandler>
);
}
/**
* Reanimated 2 Animations
*/
// withTiming
function WithTimingTest() {
const width = useSharedValue(50);
const style = useAnimatedStyle(() => {
return {
width: withTiming(
width.value,
{
duration: 500,
easing: Easing.bezier(0.25, 0.1, 0.25, 1),
},
(finished) => {}
),
};
});
return (
<View>
<Animated.View style={[styles.box, style]} />
<Button onPress={() => (width.value = Math.random() * 300)} title="Hey" />
</View>
);
}
function WithTimingToValueAsColorTest() {
const style = useAnimatedStyle(() => {
return {
width: withTiming(
'rgba(255,105,180,0)',
{
duration: 500,
easing: Easing.bezier(0.25, 0.1, 0.25, 1),
},
(_finished) => {}
),
};
});
return (
<View>
<Animated.View style={[styles.box, style]} />
</View>
);
}
// withSpring
function WithSpringTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx: { startX: number }) => {
ctx.startX = x.value;
},
onActive: (event, ctx: { startX: number }) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
x.value = withSpring(0, {}, (finished) => {});
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
function WithSpringToValueAsColorTest() {
const style = useAnimatedStyle(() => {
return {
width: withSpring('rgba(255,105,180,0)', {}, (_finished) => {}),
};
});
return (
<View>
<Animated.View style={[styles.box, style]} />
</View>
);
}
// cancelAnimation
function CancelAnimationTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx: { startX: number }) => {
cancelAnimation(x);
},
onActive: (event, ctx: { startX: number }) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
x.value = 0;
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
// withDelay
function WithDelayTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
{ startX: number }
>({
onStart: (_, _ctx) => {
cancelAnimation(x);
},
onActive: (event, ctx) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
x.value = withDelay(1000, withTiming(70));
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
// withRepeat
function WithRepeatTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
{ startX: number }
>({
onStart: (_, _ctx) => {
cancelAnimation(x);
},
onActive: (event, ctx) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
x.value = withRepeat(withTiming(70), 1, true, (finished) => {});
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
// withSequence
function WithSequenceTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
{ startX: number }
>({
onStart: (_, _ctx) => {
cancelAnimation(x);
},
onActive: (event, ctx) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
x.value = withSequence(withTiming(70), withTiming(70));
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
// withDecay
function WithDecayTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx: { startX: number }) => {
ctx.startX = x.value;
},
onActive: (event, ctx: { startX: number }) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (evt) => {
x.value = withDecay({
velocity: evt.velocityX,
clamp: [0, 200], // optionally define boundaries for the animation
});
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: x.value,
},
],
};
});
return (
<PanGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PanGestureHandler>
);
}
// useWorkletCallback
function UseWorkletCallbackTest() {
const workletCallback = useWorkletCallback((a: number, b: number) => {
return a + b;
}, []);
runOnUI(() => {
const res = workletCallback(1, 1);
console.log(res);
})();
return <Animated.View style={styles.container} />;
}
// createWorklet
function CreateWorkletTest() {
const workletCallback = createWorklet((a: number, b: number) => {
return a + b;
});
runOnUI(() => {
const res = workletCallback(1, 1);
console.log(res);
})();
return <Animated.View style={styles.container} />;
}
// useWorkletCallback
function UseAnimatedReactionTest() {
const [state, setState] = useState();
const sv = useSharedValue(0);
useAnimatedReaction(
() => {
return sv.value;
},
(value) => {
console.log(value);
}
);
useAnimatedReaction(
() => {
return sv.value;
},
(value) => {
console.log(value);
},
[]
);
useAnimatedReaction(
() => {
return sv.value;
},
(value) => {
console.log(value);
},
[state]
);
useAnimatedReaction(
() => {
return sv.value;
},
(value, previousResult) => {
console.log(value, previousResult);
}
);
return null;
}
// interpolateColor
function interpolateColorTest() {
const sv = useSharedValue(0);
interpolateColor(sv.value, [0, 1], [0x00ff00, 0x0000ff]);
interpolateColor(sv.value, [0, 1], ['red', 'blue']);
interpolateColor(sv.value, [0, 1], ['#00FF00', '#0000FF'], 'RGB');
interpolateColor(sv.value, [0, 1], ['#FF0000', '#00FF99'], 'HSV');
return null;
}
function interpolateNodeTest() {
const value = useValue(0);
interpolateNode(value, {
inputRange: [0, 1],
outputRange: ['0deg', '100deg'],
});
interpolateNode(value, {
inputRange: [0, 1],
outputRange: ['0rad', `${Math.PI}rad`],
});
}
function colorTest() {
const r = useValue(255);
const g = useValue(255);
const b = useValue(255);
const a = useValue(255);
return color(r, g, b, a);
}
function interpolateColorsTest() {
const animationValue = useValue(0);
const color = interpolateColors(animationValue, {
inputRange: [0, 1],
outputColorRange: ['red', 'blue'],
});
return color;
}
// update props
function updatePropsTest() {
const adapter1 = createAnimatedPropAdapter((props) => {}, []);
const adapter2 = createAnimatedPropAdapter((props) => {}, ['prop1', 'prop2']);
const adapter3 = createAnimatedPropAdapter(() => {});
// @ts-expect-error works only for useAnimatedProps
useAnimatedStyle(() => ({}), undefined, [adapter1, adapter2, adapter3]);
useAnimatedProps(() => ({}), null, adapter1);
useAnimatedProps(() => ({}), null, [adapter2, adapter3]);
}
// test partial animated props
function testPartialAnimatedProps() {
const ap = useAnimatedProps<ImageProps>(() => ({
height: 100,
}));
const aps = useAnimatedProps<ImageProps>(() => ({
source: { uri: 'whatever' },
}));
// @ts-expect-error it should fail because `source` is a required prop
const test1 = <AnimatedImage />;
// TODO: Figure out a way to let this error pass, if `source` is set in `animatedProps` that should be okay even if it is not set in normal props!!
// @ts-expect-error it should fail because `source` is a required prop, even though animatedProps sets it
const test2 = <AnimatedImage animatedProps={aps} />;
// should pass because source is set
const test3 = <AnimatedImage source={{ uri: 'whatever' }} />;
// should pass because source is set and `animatedProps` doesn't change that
const test4 = (
<AnimatedImage source={{ uri: 'whatever' }} animatedProps={ap} />
);
// TODO: Should this test fail? Setting it twice might not be intentional...
// should pass because source is set normally and in `animatedProps`
const test5 = (
<AnimatedImage source={{ uri: 'whatever' }} animatedProps={aps} />
);
}