This repository was archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
128 lines (110 loc) · 3.39 KB
/
App.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
import React, { Component } from 'react';
import { Animated, Easing, Button, StyleSheet, Text, View, Dimensions, PanResponder } from 'react-native';
import AnimatedModal from './AnimatedModal';
import ModalView from './ModalView';
import styles from './styles'
const deviceHeight = Dimensions.get('window').height
export default class App extends Component {
state = {
modal: new Animated.ValueXY({ x: 0, y: 400 })
}
animateModal = (open) => {
Animated.timing(
this.state.modal,
{
toValue: open ? 0 : 400,
easing: Easing.inOut(Easing.sin)
}
).start()
}
showModal = () => {
this.animateModal(true)
}
closeModal = () => {
this.animateModal(false)
}
allowedToMove = (evt, gestureState) => {
if (gestureState.dy < 0) {
return false
}
return true
}
componentWillMount() {
this._animatedValueY = 0;
this.state.modal.y.addListener((value) => this._animatedValueY = value.value);
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: (evt, gestureState) => this.allowedToMove(evt, gestureState),
onMoveShouldSetPanResponderCapture: (evt, gestureState) => this.allowedToMove(evt, gestureState),
onPanResponderGrant: (e, gestureState) => {
this.state.modal.setOffset({ y: this._animatedValueY });
this.state.modal.setValue({ x: 0, y: 0 }); //Initial value
},
onPanResponderMove: Animated.event([
null,
{ dx: 0, dy: this.state.modal.y }
]),
onPanResponderRelease: () => {
const y = this.state.modal.y._value
if (y > 200) {
this.closeModal()
} else {
Animated.spring(this.state.modal, {
toValue: 0
}).start();
}
}
});
}
componentWillUnmount() {
this.state.pan.y.removeAllListeners();
}
render() {
const transformScale = this.state.modal.y.interpolate({
inputRange: [0, 400],
outputRange: [0.9, 1],
extrapolate: 'clamp'
})
const tapSuprresorPosition = this.state.modal.y.interpolate({
inputRange: [0, 400],
outputRange: [-deviceHeight, 0],
extrapolate: 'clamp'
})
const menuPosition = this.state.modal.y.interpolate({
inputRange: [0, 400],
outputRange: [400, 0],
extrapolate: 'clamp'
})
const opacity = this.state.modal.y.interpolate({
inputRange: [0, 400],
outputRange: [1, 0],
extrapolate: 'clamp'
})
return (
<Animated.View style={styles.container}>
<Animated.View style={{
height: '100%',
width: '100%',
paddingTop: 100,
backgroundColor: '#000',
transform: [{ scale: transformScale }]
}}>
<Button title="ÖPPNA" onPress={() => this.showModal()} />
</Animated.View>
<Animated.View
pointerEvents="none"
style={[
styles.backdrop,
{ opacity }
]}
/>
<Animated.View
onStartShouldSetResponder={() => this.closeModal()}
style={{ backgroundColor: 'transparent', height: deviceHeight, width: '100%', transform: [{ translateY: tapSuprresorPosition }] }}
/>
<AnimatedModal height={400} position={this.state.modal.y} {...this._panResponder.panHandlers}>
<ModalView onClose={this.closeModal} />
</AnimatedModal>
</Animated.View>
);
}
}