-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
132 lines (115 loc) · 4.25 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
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
var TENSION = 800;
var FRICTION = 90;
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
TouchableWithoutFeedback,
Text,
Image,
View,
Animated,
AlertIOS,
PanResponder,
Dimensions
} from 'react-native';
var SCREEN_HEIGHT = Dimensions.get('window').height;
var DraggableDrawerHelper = require('./helper')(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);
console.log(initialDrawerSize,'Initila size');
return {
touched:'FALSE',
position: new Animated.Value(initialDrawerSize),
initialPositon: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) && this.state.touched=='TRUE';
},
onPanResponderMove: (evt, gestureState) => {
this.moveDrawerView(gestureState)
},
onPanResponderRelease: (evt, gestureState) => {
this.moveFinished(gestureState)
},
})
},
moveDrawerView: function(gestureState) {
console.log(gestureState.vy,'GESTURE');
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) {
var isGoingToUp = ( gestureState.vy < 0 );
if (!this.center) return;
DraggableDrawerHelper.startAnimation(gestureState.vy,gestureState.moveY,this.state.initialPositon,gestureState.stateId);
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}>
<Animated.View
style={[drawerPosition, styles.drawer]}
ref={(center) => this.center = center}
{...this._panGesture.panHandlers}>
<TouchableWithoutFeedback
onPressIn={()=>{this.setState({touched:'TRUE'})}}
onPressOut={()=>{this.setState({touched:'FALSE'})}}>
<View style={{height:40,paddingTop:40,marginTop:20,backgroundColor:'rgba(0, 0, 0, 0.51)',borderTopLeftRadius:15,borderTopRightRadius:15}}>
</View>
</TouchableWithoutFeedback>
{drawerView}
</Animated.View>
</View>
)
},
});
var styles = StyleSheet.create({
viewport: {
flex: 1,
},
drawer: {
flex:1,
},
container: {
position: 'absolute',
top:0,
left:0,
bottom: 0,
right: 0,
},
});
module.exports = component;