-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAnimatedSharedStyleExample.tsx
106 lines (98 loc) · 2.4 KB
/
AnimatedSharedStyleExample.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
import Animated, {
useSharedValue,
withTiming,
useAnimatedStyle,
Easing,
} from 'react-native-reanimated';
import { View, Button, StyleSheet } from 'react-native';
import React, { useState } from 'react';
export default function AnimatedSharedStyleExample() {
const randomWidth = useSharedValue(100);
const [blueCounter, setBlueCounter] = useState<number>(0);
const [greenCounter, setGreenCounter] = useState<number>(0);
const [itemList, setItemList] = useState<any>([]);
const [toggleState, setToggleState] = useState<boolean>(false);
const config = {
duration: 500,
easing: Easing.bezier(0.5, 0.01, 0, 1),
};
const style = useAnimatedStyle(() => {
return {
width: withTiming(randomWidth.value, config),
};
});
const scopeObject = (
<Animated.View
style={[{ backgroundColor: 'black' }, styles.block, style]}
/>
);
const renderItems = () => {
const output = [];
for (let i = 0; i < blueCounter; i++) {
output.push(
<Animated.View
key={i + 'a'}
style={[{ backgroundColor: 'blue' }, styles.block, style]}
/>
);
}
return output;
};
return (
<View
style={{
flex: 1,
flexDirection: 'column',
}}>
<Button
title="animate"
onPress={() => {
randomWidth.value = Math.random() * 350;
}}
/>
<Button
title="increment counter"
onPress={() => {
setBlueCounter(blueCounter + 1);
}}
/>
<Button
title="add item to static lists"
onPress={() => {
setGreenCounter(greenCounter + 1);
setItemList([
...itemList,
<Animated.View
key={greenCounter + 'b'}
style={[{ backgroundColor: 'green' }, styles.block, style]}
/>,
]);
}}
/>
<Button
title="toggle state"
onPress={() => {
setToggleState(!toggleState);
}}
/>
<Animated.View
style={[{ backgroundColor: 'orange' }, styles.block, style]}
/>
{toggleState && (
<Animated.View
style={[{ backgroundColor: 'black' }, styles.block, style]}
/>
)}
{toggleState && scopeObject}
{renderItems()}
{itemList}
</View>
);
}
const styles = StyleSheet.create({
block: {
width: 100,
height: 3,
margin: 1,
},
});