-
Notifications
You must be signed in to change notification settings - Fork 3
/
Modal.js
109 lines (99 loc) · 3.33 KB
/
Modal.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
import React, { useState, useEffect } from 'react'
import { View, Text, StyleSheet, TouchableOpacity, Animated, Dimensions } from 'react-native'
const { height } = Dimensions.get('window')
const Modal = ({ show, close }) => {
const [state, setState] = useState({
opacity: new Animated.Value(0),
container: new Animated.Value(height),
modal: new Animated.Value(height)
})
const openModal = () => {
Animated.sequence([
Animated.timing(state.container, { toValue: 0, duration: 100 }),
Animated.timing(state.opacity, { toValue: 1, duration: 300 }),
Animated.spring(state.modal, { toValue: 0, bounciness: 5, useNativeDriver: true })
]).start()
}
const closeModal = () => {
Animated.sequence([
Animated.timing(state.modal, { toValue: height, duration: 250, useNativeDriver: true }),
Animated.timing(state.opacity, { toValue: 0, duration: 300 }),
Animated.timing(state.container, { toValue: height, duration: 100 })
]).start()
}
useEffect(() => {
console.log(show)
if(show){
openModal()
}else{
closeModal()
}
}, [show])
return(
<Animated.View
style={[styles.container, {
opacity: state.opacity,
transform: [
{ translateY: state.container }
]
}]}
>
<Animated.View
style={[styles.modal, {
transform: [
{ translateY: state.modal }
]
}]}
>
<View style={styles.indicator} />
<Text style={styles.text}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vitae massa odio. Quisque ante sem, tempor eget massa vel, mollis tincidunt metus. Ut sed felis lectus. Nam semper molestie urna, quis ultricies quam semper ut. Maecenas aliquet id urna a convallis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas leo lectus, dictum vitae erat eget, luctus dapibus sapien. Integer at hendrerit quam. Vivamus tempor, arcu non fringilla laoreet, enim nibh porttitor enim, eget pellentesque eros nulla congue neque. Suspendisse et lobortis enim, nec fermentum est. Aliquam accumsan viverra vehicula. Proin tempus sagittis auctor. Vivamus quam ligula, laoreet eget eros et, hendrerit iaculis risus. Nam a nulla in purus fermentum rhoncus eu et erat. Aliquam tempus felis lorem, id hendrerit tortor vestibulum ac.
</Text>
<TouchableOpacity style={styles.btn} onPress={close}>
<Text style={{ color: '#fff' }}>Close</Text>
</TouchableOpacity>
</Animated.View>
</Animated.View>
)
}
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
position: 'absolute'
},
modal: {
bottom: 0,
position: 'absolute',
height: '50%',
backgroundColor: '#fff',
width: '100%',
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
paddingLeft: 25,
paddingRight: 25
},
indicator: {
width: 50,
height: 5,
backgroundColor: '#ccc',
borderRadius: 50,
alignSelf: 'center',
marginTop: 5
},
text: {
marginTop: 50,
textAlign: 'center'
},
btn: {
width: '100%',
height: 50,
borderRadius: 10,
backgroundColor: '#9b59b6',
justifyContent: 'center',
alignItems: 'center',
marginTop: 30
}
})
export default Modal