-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathCrossFadeIcon.tsx
140 lines (127 loc) · 2.95 KB
/
CrossFadeIcon.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
import * as React from 'react';
import { Animated, StyleSheet, View } from 'react-native';
import Icon, { IconSource, isEqualIcon, isValidIcon } from './Icon';
import { useInternalTheme } from '../core/theming';
import type { ThemeProp } from '../types';
type Props = {
/**
* Icon to display for the `CrossFadeIcon`.
*/
source: IconSource;
/**
* Color of the icon.
*/
color: string;
/**
* Size of the icon.
*/
size: number;
/**
* TestID used for testing purposes
*/
testID?: string;
/**
* @optional
*/
theme?: ThemeProp;
};
const CrossFadeIcon = ({
color,
size,
source,
theme: themeOverrides,
testID = 'cross-fade-icon',
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const [currentIcon, setCurrentIcon] = React.useState<IconSource>(
() => source
);
const [previousIcon, setPreviousIcon] = React.useState<IconSource | null>(
null
);
const { current: fade } = React.useRef<Animated.Value>(new Animated.Value(1));
const { scale } = theme.animation;
if (currentIcon !== source) {
setPreviousIcon(() => currentIcon);
setCurrentIcon(() => source);
}
React.useEffect(() => {
if (isValidIcon(previousIcon) && !isEqualIcon(previousIcon, currentIcon)) {
fade.setValue(1);
Animated.timing(fade, {
duration: scale * 200,
toValue: 0,
useNativeDriver: true,
}).start();
}
}, [currentIcon, previousIcon, fade, scale]);
const opacityPrev = fade;
const opacityNext = previousIcon
? fade.interpolate({
inputRange: [0, 1],
outputRange: [1, 0],
})
: 1;
const rotatePrev = fade.interpolate({
inputRange: [0, 1],
outputRange: ['-90deg', '0deg'],
});
const rotateNext = previousIcon
? fade.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '-180deg'],
})
: '0deg';
return (
<View
style={[
styles.content,
{
height: size,
width: size,
},
]}
>
{previousIcon ? (
<Animated.View
style={[
styles.icon,
{
opacity: opacityPrev,
transform: [{ rotate: rotatePrev }],
},
]}
testID={`${testID}-previous`}
>
<Icon source={previousIcon} size={size} color={color} theme={theme} />
</Animated.View>
) : null}
<Animated.View
style={[
styles.icon,
{
opacity: opacityNext,
transform: [{ rotate: rotateNext }],
},
]}
testID={`${testID}-current`}
>
<Icon source={currentIcon} size={size} color={color} theme={theme} />
</Animated.View>
</View>
);
};
export default CrossFadeIcon;
const styles = StyleSheet.create({
content: {
alignItems: 'center',
justifyContent: 'center',
},
icon: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
});