forked from oblador/react-native-lightbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lightbox.js
157 lines (143 loc) · 3.76 KB
/
Lightbox.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
/**
* @providesModule Lightbox
*/
'use strict';
var React = require('react');
var {
Children,
cloneElement,
PropTypes,
} = React;
var {
Animated,
TouchableHighlight,
View,
} = require('react-native');
var TimerMixin = require('react-timer-mixin');
var LightboxOverlay = require('./LightboxOverlay');
var Lightbox = React.createClass({
mixins: [TimerMixin],
propTypes: {
activeProps: PropTypes.object,
renderHeader: PropTypes.func,
renderContent: PropTypes.func,
underlayColor: PropTypes.string,
backgroundColor: PropTypes.string,
onOpen: PropTypes.func,
onClose: PropTypes.func,
springConfig: PropTypes.shape({
tension: PropTypes.number,
friction: PropTypes.number,
}),
swipeToDismiss: PropTypes.bool,
},
getDefaultProps: function() {
return {
swipeToDismiss: true,
onOpen: () => {},
onClose: () => {},
};
},
getInitialState: function() {
return {
isOpen: false,
origin: {
x: 0,
y: 0,
width: 0,
height: 0,
},
layoutOpacity: new Animated.Value(1),
};
},
getContent: function() {
if(this.props.renderContent) {
return this.props.renderContent();
} else if(this.props.activeProps) {
return cloneElement(
Children.only(this.props.children),
this.props.activeProps
);
}
return this.props.children;
},
getOverlayProps: function() {
return {
isOpen: this.state.isOpen,
origin: this.state.origin,
renderHeader: this.props.renderHeader,
swipeToDismiss: this.props.swipeToDismiss,
springConfig: this.props.springConfig,
backgroundColor: this.props.backgroundColor,
children: this.getContent(),
onClose: this.onClose,
};
},
open: function() {
this._root.measure((ox, oy, width, height, px, py) => {
this.props.onOpen();
this.setState({
isOpen: (this.props.navigator ? true : false),
isAnimating: true,
origin: {
width,
height,
x: px,
y: py,
},
}, () => {
if(this.props.navigator) {
var route = {
component: LightboxOverlay,
passProps: this.getOverlayProps(),
};
var routes = this.props.navigator.getCurrentRoutes();
routes.push(route);
this.props.navigator.immediatelyResetRouteStack(routes);
} else {
this.setState({
isOpen: true,
});
}
this.setTimeout(() => {
this.state.layoutOpacity.setValue(0);
});
});
});
},
close: function() {
throw new Error('Lightbox.close method is deprecated. Use renderHeader(close) prop instead.')
},
onClose: function() {
this.state.layoutOpacity.setValue(1);
this.setState({
isOpen: false,
}, this.props.onClose);
if(this.props.navigator) {
var routes = this.props.navigator.getCurrentRoutes();
routes.pop();
this.props.navigator.immediatelyResetRouteStack(routes);
}
},
render: function() {
// measure will not return anything useful if we dont attach a onLayout handler on android
return (
<View
ref={component => this._root = component}
style={this.props.style}
onLayout={() => {}}
>
<Animated.View style={{opacity: this.state.layoutOpacity}}>
<TouchableHighlight
underlayColor={this.props.underlayColor}
onPress={this.open}
>
{this.props.children}
</TouchableHighlight>
</Animated.View>
{this.props.navigator ? false : <LightboxOverlay {...this.getOverlayProps()} />}
</View>
);
}
});
module.exports = Lightbox;