forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossFadeIcon.js
143 lines (126 loc) · 3.01 KB
/
CrossFadeIcon.js
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
/* @flow */
import * as React from 'react';
import { Animated, StyleSheet, View } from 'react-native';
import { polyfill } from 'react-lifecycles-compat';
import Icon, { isValidIcon, isEqualIcon } from './Icon';
import type { IconSource } from './Icon';
type Props = {|
/**
* Icon to display for the `CrossFadeIcon`.
*/
source: IconSource,
/**
* Color of the icon.
*/
color: string,
/**
* Size of the icon.
*/
size: number,
|};
type State = {
currentIcon: IconSource,
previousIcon: ?IconSource,
fade: Animated.Value,
};
class CrossFadeIcon extends React.Component<Props, State> {
static getDerivedStateFromProps(nextProps: Props, nextState: State) {
if (nextState.currentIcon === nextProps.source) {
return null;
}
return {
currentIcon: nextProps.source,
previousIcon: nextState.currentIcon,
};
}
state = {
currentIcon: this.props.source,
previousIcon: null,
fade: new Animated.Value(1),
};
componentDidUpdate(prevProps: Props, prevState: State) {
const { previousIcon } = this.state;
if (
!isValidIcon(previousIcon) ||
isEqualIcon(previousIcon, prevState.previousIcon)
) {
return;
}
this.state.fade.setValue(1);
Animated.timing(this.state.fade, {
duration: 200,
toValue: 0,
}).start();
}
render() {
const { color, size } = this.props;
const opacityPrev = this.state.fade;
const opacityNext = this.state.previousIcon
? this.state.fade.interpolate({
inputRange: [0, 1],
outputRange: [1, 0],
})
: 1;
const rotatePrev = this.state.fade.interpolate({
inputRange: [0, 1],
outputRange: ['-90deg', '0deg'],
});
const rotateNext = this.state.previousIcon
? this.state.fade.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '-180deg'],
})
: '0deg';
return (
<View
style={[
styles.content,
{
height: size,
width: size,
},
]}
>
{this.state.previousIcon ? (
<Animated.View
style={[
styles.icon,
{
opacity: opacityPrev,
transform: [{ rotate: rotatePrev }],
},
]}
>
<Icon source={this.state.previousIcon} size={size} color={color} />
</Animated.View>
) : null}
<Animated.View
style={[
styles.icon,
{
opacity: opacityNext,
transform: [{ rotate: rotateNext }],
},
]}
>
<Icon source={this.state.currentIcon} size={size} color={color} />
</Animated.View>
</View>
);
}
}
polyfill(CrossFadeIcon);
export default CrossFadeIcon;
const styles = StyleSheet.create({
content: {
alignItems: 'center',
justifyContent: 'center',
},
icon: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
});