forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpacityView.js
71 lines (61 loc) · 1.84 KB
/
OpacityView.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
import React from 'react';
import {Animated} from 'react-native';
import PropTypes from 'prop-types';
const propTypes = {
// Should we dim the view
shouldDim: PropTypes.bool.isRequired,
// Content to render
children: PropTypes.node.isRequired,
// Array of style objects
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.arrayOf(PropTypes.object),
};
const defaultProps = {
style: [],
};
class OpacityView extends React.Component {
constructor(props) {
super(props);
this.opacity = new Animated.Value(1);
this.undim = this.undim.bind(this);
}
componentDidUpdate(prevProps) {
if (!prevProps.shouldDim && this.props.shouldDim) {
Animated.timing(this.opacity, {
toValue: 0.5,
duration: 50,
useNativeDriver: true,
}).start();
}
if (prevProps.shouldDim && !this.props.shouldDim) {
this.undim();
}
}
undim() {
Animated.timing(this.opacity, {
toValue: 1,
duration: 50,
useNativeDriver: true,
}).start(({finished}) => {
// If animation doesn't finish because Animation.stop was called
// (e.g. because it was interrupted by a gesture or another animation),
// restart animation so we always make sure the component gets completely shown.
if (finished) {
return;
}
this.undim();
});
}
render() {
return (
<Animated.View
style={[{opacity: this.opacity}, ...this.props.style]}
>
{this.props.children}
</Animated.View>
);
}
}
OpacityView.propTypes = propTypes;
OpacityView.defaultProps = defaultProps;
export default OpacityView;