-
Notifications
You must be signed in to change notification settings - Fork 10
/
DraggableDrawer.jsx.js
164 lines (106 loc) · 3.78 KB
/
DraggableDrawer.jsx.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict'
var React = require('react-native');
var TENSION = 800;
var FRICTION = 90;
var {
AppRegistry,
StyleSheet,
Text,
Image,
View,
Animated,
PanResponder,
Dimensions
} = React;
var SCREEN_HEIGHT = Dimensions.get('window').height;
var DraggableDrawerHelper = require('./helpers/DraggableDrawerHelper')(SCREEN_HEIGHT);
var component = React.createClass({
getInitialState: function() {
// naming it initialX clearly indicates that the only purpose
// of the passed down prop is to initialize something internally
var initialDrawerSize = DraggableDrawerHelper.calculateInitialPosition(this.props.initialDrawerSize);
return {position: new Animated.Value(initialDrawerSize) };
},
onUpdatePosition: function (position){
this.state.position.setValue(position);
this._previousTop = position;
console.log('Position ',position);
var initialPosition = DraggableDrawerHelper.getInitialPosition();
if( initialPosition === position){
this.props.onInitialPositionReached && this.props.onInitialPositionReached();
}
},
componentWillMount: function() {
// Initialize the DraggableDrawerHelper that will drive animations
DraggableDrawerHelper.setupAnimation(TENSION,FRICTION,
(position) => {
if (!this.center) return;
this.onUpdatePosition(position.value);
}
);
this._panGesture = PanResponder.create({
onMoveShouldSetPanResponder: (evt, gestureState) => {
return DraggableDrawerHelper.isAValidMovement(gestureState.dx,gestureState.dy);
},
onPanResponderMove: (evt, gestureState) => this.moveDrawerView(gestureState),
onPanResponderRelease: (evt, gestureState) => this.moveFinished(gestureState),
})
},
moveDrawerView: function(gestureState) {
if (!this.center) return;
var currentValue = Math.abs(gestureState.moveY / SCREEN_HEIGHT);
var isGoingToUp = ( gestureState.vy < 0 );
//Here, I'm subtracting %5 of screen size from edge drawer position to be closer as possible to finger location when dragging the drawer view
var position = gestureState.moveY - SCREEN_HEIGHT * 0.05;
//Send to callback function the current drawer position when drag down the drawer view component
if(!isGoingToUp) this.props.onDragDown(1-currentValue);
this.onUpdatePosition(position);
},
moveFinished: function(gestureState) {
console.log('moveFinished ');
if (!this.center) return;
DraggableDrawerHelper.startAnimation(gestureState.vy,gestureState.moveY);
var isGoingToUp = ( gestureState.vy < 0 );
this.props.onRelease && this.props.onRelease(isGoingToUp);
},
render: function() {
var containerView = this.props.renderContainerView ? this.props.renderContainerView() : null;
var drawerView = this.props.renderDrawerView ? this.props.renderDrawerView() : null;
var drawerPosition = {
top: this.state.position
};
return (
<View style={styles.viewport}>
<View style={styles.container}>
{containerView}
</View>
<Animated.View
style={[drawerPosition, styles.drawer]}
ref={(center) => this.center = center}
{...this._panGesture.panHandlers}>
{drawerView}
</Animated.View>
</View>
)
},
});
var styles = StyleSheet.create({
viewport: {
flex: 1,
margin: 20,
backgroundColor: '#22aaff',
},
drawer: {
flex:1,
backgroundColor: '#9AABC1',
},
container: {
position: 'absolute',
top:0,
left:0,
bottom: 0,
right: 0,
backgroundColor: '#EAF2F2',
},
});
module.exports = component;