forked from maxs15/react-native-modalbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
407 lines (349 loc) · 10.6 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
'use strict';
var React = require('react-native');
var {
View,
StyleSheet,
PanResponder,
Animated,
TouchableWithoutFeedback,
Dimensions,
Easing
} = React;
var screen = Dimensions.get('window');
var styles = StyleSheet.create({
wrapper: {
backgroundColor: "white"
},
transparent: {
backgroundColor: 'rgba(0,0,0,0)'
},
absolute: {
position: "absolute",
top: 0,
bottom: 0,
left: 0,
right: 0
}
});
var ModalBox = React.createClass({
propTypes: {
isOpen: React.PropTypes.bool,
isDisabled: React.PropTypes.bool,
backdropPressToClose: React.PropTypes.bool,
swipeToClose: React.PropTypes.bool,
swipeThreshold: React.PropTypes.number,
swipeArea: React.PropTypes.number,
position: React.PropTypes.string,
backdrop: React.PropTypes.bool,
backdropOpacity: React.PropTypes.number,
backdropColor: React.PropTypes.string,
backdropContent: React.PropTypes.element,
animationDuration: React.PropTypes.number,
onClosed: React.PropTypes.func,
onOpened: React.PropTypes.func,
onClosingState: React.PropTypes.func,
},
getDefaultProps: function () {
return {
backdropPressToClose: true,
swipeToClose: true,
swipeThreshold: 50,
position: "center",
backdrop: true,
backdropOpacity: 0.5,
backdropColor: "black",
backdropContent: null,
animationDuration: 400
};
},
getInitialState: function () {
return {
position: new Animated.Value(screen.height),
backdropOpacity: new Animated.Value(0),
isOpen: false,
isAnimateClose: false,
isAnimateOpen: false,
swipeToClose: false,
height: screen.height,
width: screen.width,
containerHeight: screen.height,
containerWidth: screen.width,
isInitialized: false
};
},
componentWillMount: function() {
this.createPanResponder();
this.handleOpenning(this.props);
},
componentWillReceiveProps: function(props) {
this.handleOpenning(props);
},
handleOpenning: function(props) {
if (typeof props.isOpen == "undefined") return;
if (props.isOpen)
this.open();
else
this.close();
},
/****************** ANIMATIONS **********************/
/*
* Open animation for the backdrop, will fade in
*/
animateBackdropOpen: function() {
if (this.state.isAnimateBackdrop) {
this.state.animBackdrop.stop();
this.state.isAnimateBackdrop = false;
}
this.state.isAnimateBackdrop = true;
this.state.animBackdrop = Animated.timing(
this.state.backdropOpacity,
{
toValue: 1,
duration: this.props.animationDuration
}
);
this.state.animBackdrop.start(() => {
this.state.isAnimateBackdrop = false;
});
},
/*
* Close animation for the backdrop, will fade out
*/
animateBackdropClose: function() {
if (this.state.isAnimateBackdrop) {
this.state.animBackdrop.stop();
this.state.isAnimateBackdrop = false;
}
this.state.isAnimateBackdrop = true;
this.state.animBackdrop = Animated.timing(
this.state.backdropOpacity,
{
toValue: 0,
duration: this.props.animationDuration
}
);
this.state.animBackdrop.start(() => {
this.state.isAnimateBackdrop = false;
});
},
/*
* Stop opening animation
*/
stopAnimateOpen: function() {
if (this.state.isAnimateOpen) {
if (this.state.animOpen) this.state.animOpen.stop();
this.state.isAnimateOpen = false;
}
},
/*
* Open animation for the modal, will move up
*/
animateOpen: function() {
this.stopAnimateClose();
// Backdrop fadeIn
if (this.props.backdrop)
this.animateBackdropOpen();
// Detecting modal position
this.state.positionDest = this.calculateModalPosition(this.state.containerHeight, this.state.containerWidth);
this.state.isAnimateOpen = true;
this.state.animOpen = Animated.timing(
this.state.position,
{
toValue: this.state.positionDest,
duration: this.props.animationDuration,
easing: Easing.elastic(0.8)
}
);
this.state.animOpen.start(() => {
this.state.isAnimateOpen = false;
this.state.isOpen = true;
if (this.props.onOpened) this.props.onOpened();
});
},
/*
* Stop closing animation
*/
stopAnimateClose: function() {
if (this.state.isAnimateClose) {
if (this.state.animClose) this.state.animClose.stop();
this.state.isAnimateClose = false;
}
},
/*
* Close animation for the modal, will move down
*/
animateClose: function() {
this.stopAnimateOpen();
// Backdrop fadeout
if (this.props.backdrop)
this.animateBackdropClose();
this.state.isAnimateClose = true;
this.state.animClose = Animated.timing(
this.state.position,
{
toValue: this.state.containerHeight,
duration: this.props.animationDuration
}
);
this.state.animClose.start(() => {
this.state.isAnimateClose = false;
this.state.isOpen = false;
this.setState({});
if (this.props.onClosed) this.props.onClosed();
});
},
/*
* Calculate when should be placed the modal
*/
calculateModalPosition: function(containerHeight, containerWidth) {
var position = 0;
if (this.props.position == "bottom") {
position = containerHeight - this.state.height;
}
else if (this.props.position == "center") {
position = containerHeight / 2 - this.state.height / 2;
}
// Checking if the position >= 0
if (position < 0) position = 0;
return position;
},
/*
* Create the pan responder to detect gesture
* Only used if swipeToClose is enabled
*/
createPanResponder: function() {
var closingState = false;
var inSwipeArea = false;
var onPanRelease = (evt, state) => {
if (!inSwipeArea) return;
inSwipeArea = false;
if (state.dy > this.props.swipeThreshold)
this.animateClose();
else
this.animateOpen();
};
var animEvt = Animated.event([null, {customY: this.state.position}]);
var onPanMove = (evt, state) => {
var newClosingState = (state.dy > this.props.swipeThreshold) ? true : false;
if (state.dy < 0) return;
if (newClosingState != closingState && this.props.onClosingState)
this.props.onClosingState(newClosingState);
closingState = newClosingState;
state.customY = state.dy + this.state.positionDest;
animEvt(evt, state);
};
var onPanStart = (evt, state) => {
if (!this.props.swipeToClose || this.props.isDisabled || (this.props.swipeArea && (evt.nativeEvent.pageY - this.state.positionDest) > this.props.swipeArea)) {
inSwipeArea = false;
return false;
}
inSwipeArea = true;
return true;
};
this.state.pan = PanResponder.create({
onStartShouldSetPanResponder: onPanStart,
onPanResponderMove: onPanMove,
onPanResponderRelease: onPanRelease,
onPanResponderTerminate: onPanRelease,
});
},
/*
* Event called when the modal view layout is calculated
*/
onViewLayout: function(evt) {
this.state.height = evt.nativeEvent.layout.height;
this.state.width = evt.nativeEvent.layout.width;
if (this.onViewLayoutCalculated) this.onViewLayoutCalculated();
},
/*
* Event called when the container view layout is calculated
*/
onContainerLayout: function(evt) {
var height = evt.nativeEvent.layout.height;
var width = evt.nativeEvent.layout.width;
// If the container size is still the same we're done
if (height == this.state.containerHeight && width == this.state.containerWidth) {
this.state.isInitialized = true;
return;
}
var modalPosition = this.calculateModalPosition(height, width);
var coords = {};
// Fixing the position if the modal was already open or an animation was in progress
if (this.state.isInitialized && (this.state.isOpen || this.state.isAnimateOpen || this.state.isAnimateClose)) {
var position = this.state.isOpen ? modalPosition : this.state.containerHeight;
// Checking if a animation was in progress
if (this.state.isAnimateOpen) {
position = modalPosition;
this.stopAnimateOpen();
} else if (this.state.isAnimateClose) {
position = this.state.containerHeight;
this.stopAnimateClose();
}
this.state.position.setValue(position);
coords = {positionDest: position};
}
this.setState({
isInitialized: true,
containerHeight: height,
containerWidth: width,
...coords
});
},
/*
* Render the backdrop element
*/
renderBackdrop: function(size) {
var backdrop = [];
if (this.props.backdrop) {
backdrop = (
<TouchableWithoutFeedback onPress={this.props.backdropPressToClose ? this.close : null}>
<Animated.View style={[styles.absolute, size, {opacity: this.state.backdropOpacity}]}>
<View style={[styles.absolute, {backgroundColor:this.props.backdropColor, opacity: this.props.backdropOpacity}]}/>
{this.props.backdropContent || []}
</Animated.View>
</TouchableWithoutFeedback>
);
}
return backdrop;
},
/*
* Render the component
*/
render: function() {
var visible = this.state.isOpen || this.state.isAnimateOpen || this.state.isAnimateClose;
var size = {height: this.state.containerHeight, width: this.state.containerWidth};
var offsetX = (this.state.containerWidth - this.state.width) / 2;
var backdrop = this.renderBackdrop(size);
if (!visible) return <View/>
return (
<View style={[styles.transparent, styles.absolute]} pointerEvents={'box-none'} onLayout={this.onContainerLayout}>
{backdrop}
<Animated.View
onLayout={this.onViewLayout}
style={[styles.wrapper, size, this.props.style, {transform: [{translateY: this.state.position}, {translateX: offsetX}]} ]}
{...this.state.pan.panHandlers}>
{this.props.children}
</Animated.View>
</View>
);
},
/****************** PUBLIC METHODS **********************/
open: function() {
if (this.props.isDisabled) return;
if (!this.state.isAnimateOpen && (!this.state.isOpen || this.state.isAnimateClose)) {
this.onViewLayoutCalculated = () => {
this.setState({});
this.animateOpen();
};
this.setState({isAnimateOpen : true});
}
},
close: function() {
if (this.props.isDisabled) return;
if (!this.state.isAnimateClose && (this.state.isOpen || this.state.isAnimateOpen)) {
delete this.onViewLayoutCalculated;
this.animateClose();
}
}
});
module.exports = ModalBox;