-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
127 lines (114 loc) · 3.11 KB
/
App.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
/* eslint-disable react-native/no-inline-styles */
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React from 'react';
import {Pressable, SafeAreaView, ScrollView, Text, View} from 'react-native';
import Animated, {
Easing,
interpolate,
interpolateColor,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
const BUTTON_TRANSITION_TIME = 500;
const App = () => {
const pressed = useSharedValue<number>(0);
const animatedBoxShadow = useAnimatedStyle(() => {
const blurRadius = interpolate(pressed.value, [0, 1], [10, 0]);
const color = interpolateColor(
pressed.value,
[0, 1],
// from black to yellow - will not work <---!!!!
['rgba(0, 0, 0, 0.5)', 'rgba(255, 255, 0, 0.5)'], // will not work!! <---!!!!
);
const boxShadow = `0px 4px ${blurRadius}px 0px ${color}`;
return {
boxShadow,
};
});
const backgroundColorStyle = useAnimatedStyle(() => {
const backgroundColor = interpolateColor(
pressed.value,
[0, 1],
['lightblue', 'red'],
);
return {
backgroundColor,
};
});
const handlePressIn = () => {
pressed.value = withTiming(1, {
duration: BUTTON_TRANSITION_TIME,
easing: Easing.out(Easing.ease),
});
};
const handlePressOut = () => {
pressed.value = withTiming(0, {
duration: BUTTON_TRANSITION_TIME,
easing: Easing.out(Easing.ease),
});
};
return (
<SafeAreaView style={backgroundStyle}>
<ScrollView style={backgroundStyle}>
<View
style={{
padding: 24,
}}>
<Text
style={{
fontSize: 18,
color: 'black',
fontWeight: 'bold',
marginBottom: 16,
}}>
The background color animation will work, but not the shadow
animation.
</Text>
<Text
style={{
fontSize: 14,
color: 'black',
marginBottom: 8,
}}>
The shadow animation will also not work if using the BoxShadowValue
type instead of a string, it does not make a difference.
</Text>
<Text
style={{
fontSize: 14,
color: 'black',
}}>
Please note that the shadow renders correctly on first render, but
not after the button has been pressed once
</Text>
<AnimatedPressable
style={[
backgroundColorStyle,
animatedBoxShadow,
{
padding: 16,
borderRadius: 100,
marginVertical: 16,
},
]}
onPressIn={handlePressIn}
onPressOut={handlePressOut}>
<Text>Hello I am button with shadow</Text>
</AnimatedPressable>
</View>
</ScrollView>
</SafeAreaView>
);
};
const backgroundStyle = {
backgroundColor: 'lightgrey',
flex: 1,
};
export default App;