forked from Kureev/react-native-side-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (234 loc) · 7.03 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//@noflow
const styles = require('./styles');
const ReactNative = require('react-native');
const React = require('react');
const { Dimensions, Animated, } = ReactNative;
const deviceScreen = Dimensions.get('window');
const {
PanResponder,
View,
TouchableWithoutFeedback,
} = ReactNative;
/**
* Size of the amount you can move content view in the opened menu state and
* release without menu closing
* @type {Number}
*/
const barrierForward = deviceScreen.width / 4;
/**
* Check if the current gesture offset bigger than allowed one
* before opening menu
* @param {Number} dx Gesture offset from the left side of the window
* @return {Boolean}
*/
function shouldOpenMenu(dx: Number) {
return dx > barrierForward;
}
class SideMenu extends React.Component {
constructor(props) {
super(props);
/**
* Default left offset for content view
* @todo Check if it's possible to avoid using `prevLeft`
* @type {Number}
*/
this.prevLeft = 0;
this.isOpen = props.isOpen;
const initialMenuPositionMultiplier = props.menuPosition === 'right' ? -1 : 1
this.state = {
width: deviceScreen.width,
height: deviceScreen.height,
left: new Animated.Value(
props.isOpen ? props.openMenuOffset * initialMenuPositionMultiplier : props.hiddenMenuOffset
),
};
}
/**
* Set the initial responders
* @return {Void}
*/
componentWillMount() {
this.responder = PanResponder.create({
onStartShouldSetResponderCapture: this.props.onStartShouldSetResponderCapture.bind(this),
onMoveShouldSetPanResponder: this.handleMoveShouldSetPanResponder.bind(this),
onPanResponderMove: this.handlePanResponderMove.bind(this),
onPanResponderRelease: this.handlePanResponderEnd.bind(this),
});
}
componentWillReceiveProps(props) {
if (this.isOpen !== props.isOpen) {
this.openMenu(props.isOpen);
}
}
/**
* Determines if gestures are enabled, based off of disableGestures prop
* @return {Boolean}
*/
gesturesAreEnabled() {
let { disableGestures, } = this.props;
if (typeof disableGestures === 'function') {
return !disableGestures();
}
return !disableGestures;
}
/**
* Permission to use responder
* @return {Boolean}
*/
handleMoveShouldSetPanResponder(e: Object, gestureState: Object) {
if (this.gesturesAreEnabled()) {
const x = Math.round(Math.abs(gestureState.dx));
const y = Math.round(Math.abs(gestureState.dy));
const touchMoved = x > this.props.toleranceX && y < this.props.toleranceY;
if (this.isOpen) {
return touchMoved;
}
const withinEdgeHitWidth = this.props.menuPosition === 'right' ?
gestureState.moveX > (deviceScreen.width - this.props.edgeHitWidth) :
gestureState.moveX < this.props.edgeHitWidth;
const swipingToOpen = this.menuPositionMultiplier() * gestureState.dx > 0;
return withinEdgeHitWidth && touchMoved && swipingToOpen;
}
return false;
}
/**
* Handler on responder move
* @param {Synthetic Event} e
* @param {Object} gestureState
* @return {Void}
*/
handlePanResponderMove(e: Object, gestureState: Object) {
if (this.state.left.__getValue() * this.menuPositionMultiplier() >= 0) {
let newLeft = this.prevLeft + gestureState.dx;
if (!this.props.bounceBackOnOverdraw && Math.abs(newLeft) > this.props.openMenuOffset) {
newLeft = this.menuPositionMultiplier() * this.props.openMenuOffset;
}
this.state.left.setValue(newLeft);
}
}
/**
* Handler on responder move ending
* @param {Synthetic Event} e
* @param {Object} gestureState
* @return {Void}
*/
handlePanResponderEnd(e: Object, gestureState: Object) {
const offsetLeft = this.menuPositionMultiplier() *
(this.state.left.__getValue() + gestureState.dx);
this.openMenu(shouldOpenMenu(offsetLeft));
}
/**
* Returns 1 or -1 depending on the menuPosition
* @return {Number}
*/
menuPositionMultiplier() {
return this.props.menuPosition === 'right' ? -1 : 1;
}
moveLeft(offset) {
const newOffset = this.menuPositionMultiplier() * offset;
this.props
.animationFunction(this.state.left, newOffset)
.start();
this.prevLeft = newOffset;
}
/**
* Toggle menu
* @return {Void}
*/
openMenu(isOpen) {
const { hiddenMenuOffset, openMenuOffset, } = this.props;
this.moveLeft(isOpen ? openMenuOffset : hiddenMenuOffset);
this.isOpen = isOpen;
this.forceUpdate();
this.props.onChange(isOpen);
}
/**
* Get content view. This view will be rendered over menu
* @return {React.Component}
*/
getContentView() {
let overlay = null;
if (this.isOpen) {
overlay = (
<TouchableWithoutFeedback onPress={() => this.openMenu(false)}>
<View style={styles.overlay} />
</TouchableWithoutFeedback>
);
}
const { width, height, } = this.state;
const ref = (sideMenu) => this.sideMenu = sideMenu;
const style = [
styles.frontView,
{ width, height, },
this.props.animationStyle(this.state.left),
];
return (
<Animated.View style={style} ref={ref} {...this.responder.panHandlers}>
{this.props.children}
{overlay}
</Animated.View>
);
}
onLayoutChange(e) {
const { width, height, } = e.nativeEvent.layout;
this.setState({ width, height, });
}
/**
* Compose and render menu and content view
* @return {React.Component}
*/
render() {
const boundryStyle = this.props.menuPosition == 'right' ?
{left: deviceScreen.width - this.props.openMenuOffset} :
{right: deviceScreen.width - this.props.openMenuOffset} ;
const menu = <View style={[styles.menu, boundryStyle]}>{this.props.menu}</View>;
return (
<View style={styles.container} onLayout={this.onLayoutChange.bind(this)}>
{menu}
{this.getContentView()}
</View>
);
}
}
SideMenu.propTypes = {
edgeHitWidth: React.PropTypes.number,
toleranceX: React.PropTypes.number,
toleranceY: React.PropTypes.number,
menuPosition: React.PropTypes.oneOf(['left', 'right', ]),
onChange: React.PropTypes.func,
openMenuOffset: React.PropTypes.number,
hiddenMenuOffset: React.PropTypes.number,
disableGestures: React.PropTypes.oneOfType([React.PropTypes.func, React.PropTypes.bool, ]),
animationFunction: React.PropTypes.func,
onStartShouldSetResponderCapture: React.PropTypes.func,
isOpen: React.PropTypes.bool,
bounceBackOnOverdraw: React.PropTypes.bool,
};
SideMenu.defaultProps = {
toleranceY: 10,
toleranceX: 10,
edgeHitWidth: 60,
openMenuOffset: deviceScreen.width * 2 / 3,
hiddenMenuOffset: 0,
onStartShouldSetResponderCapture: () => true,
onChange: () => {},
animationStyle: (value) => {
return {
transform: [{
translateX: value,
}, ],
};
},
animationFunction: (prop, value) => {
return Animated.spring(
prop,
{
toValue: value,
friction: 8,
}
);
},
isOpen: false,
bounceBackOnOverdraw: true,
};
module.exports = SideMenu;