-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (82 loc) · 1.96 KB
/
index.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
import React, {
Component,
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity,
Animated,
Dimensions
} from 'react-native';
const { height: deviceHeight, width: deviceWidth } = Dimensions.get('window');
export default class Modal extends Component {
constructor(props) {
super(props);
this.state = {
isOpen: false,
offset: new Animated.Value(deviceHeight)
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.isOpen && !this.state.isOpen) {
this.setState({isOpen: true});
Animated.timing(this.state.offset, {
duration: 200,
toValue: 0
}).start();
} else if (!nextProps.isOpen && this.state.isOpen) {
Animated.timing(this.state.offset, {
duration: 200,
toValue: deviceHeight
}).start(() => {this.setState({isOpen: false});});
}
}
render() {
if (!this.state.isOpen) return null;
const styles = StyleSheet.create({
overlay: {
backgroundColor: 'rgba(0,0,0,0.8)',
position: 'absolute',
top: 0,
left: 0,
width: deviceWidth,
height: deviceHeight
},
modal: {
position: 'absolute',
top: (deviceHeight - this.props.styles.modal.height) / 2,
left: (deviceWidth - this.props.styles.modal.width) / 2
}
});
return (
<Animated.View
style={[
styles.overlay,
styles.flexCenter,
this.props.styles.overlay,
{transform: [{translateY: this.state.offset}]}
]}>
<View style={[this.props.styles.modal, styles.modal]}>
{this.props.children}
</View>
</Animated.View>
);
}
}
Modal.propTypes = {
styles: React.PropTypes.object,
isOpen: React.PropTypes.bool.isRequired
};
Modal.defaultProps = {
styles: {
modal: {
width: 100,
height: 100,
backgroundColor: '#FFF'
},
overlay: {
backgroundColor: 'rgba(0,0,0,0.6)'
}
},
isOpen: false
};