forked from ZackLeonardo/react-native-lightbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightbox.js
More file actions
169 lines (150 loc) · 4.37 KB
/
Lightbox.js
File metadata and controls
169 lines (150 loc) · 4.37 KB
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
import React, { Component, Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import { Animated, TouchableHighlight, View } from 'react-native';
import LightboxOverlay from './LightboxOverlay';
import './Gallery';
export default class Lightbox extends Component {
static propTypes = {
activeProps: PropTypes.object,
renderHeader: PropTypes.func,
renderContent: PropTypes.func,
underlayColor: PropTypes.string,
backgroundColor: PropTypes.string,
didOpen: PropTypes.func,
onOpen: PropTypes.func,
willClose: PropTypes.func,
onClose: PropTypes.func,
springConfig: PropTypes.shape({
tension: PropTypes.number,
friction: PropTypes.number,
}),
swipeToDismiss: PropTypes.bool,
renderMask: PropTypes.func,
};
static defaultProps = {
swipeToDismiss: true,
onOpen: () => {},
didOpen: () => {},
willClose: () => {},
onClose: () => {},
renderMask: () => {},
};
state = {
isOpen: false,
origin: {
x: 0,
y: 0,
width: 0,
height: 0,
},
layoutOpacity: new Animated.Value(1),
};
componentWillMount(){
if (this.props.galleryMode){
var GKeyArray = global.gallery.get(this.props.GKey);
if (!!GKeyArray){
GKeyArray.push(this.props.children)
} else {
GKeyArray = [this.props.children]
}
global.gallery.set(this.props.GKey, GKeyArray);
}
}
componentWillUnmount(){
global.gallery.delete(this.props.GKey);
}
getOverlayProps = () => ({
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.props.children,//this.getContent(),
activeProps: this.props.activeProps,
renderContent: this.props.renderContent,
didOpen: this.props.didOpen,
willClose: this.props.willClose,
onClose: this.onClose,
})
open = () => {
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,
},
}, () => {
this.props.didOpen();
if(this.props.navigator) {
const route = {
component: LightboxOverlay,
passProps: this.getOverlayProps(),
};
const routes = this.props.navigator.getCurrentRoutes();
routes.push(route);
this.props.navigator.immediatelyResetRouteStack(routes);
} else {
this.setState({
isOpen: true,
});
}
setTimeout(() => {
this._root && this.state.layoutOpacity.setValue(0);
});
});
});
}
close = () => {
throw new Error('Lightbox.close method is deprecated. Use renderHeader(close) prop instead.')
}
onClose = () => {
this.state.layoutOpacity.setValue(1);
this.setState({
isOpen: false,
}, this.props.onClose);
if(this.props.navigator) {
const routes = this.props.navigator.getCurrentRoutes();
routes.pop();
this.props.navigator.immediatelyResetRouteStack(routes);
}
}
_renderMask = () => {
if(this.props.renderMask) {
return (
<View style={{position: 'absolute'}}>
{this.props.renderMask()}
</View>
);
}
return null;
}
render() {
// 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}
>
<View style={{alignItems: 'center', justifyContent: 'center'}}>
{this.props.children}
{this._renderMask()}
</View>
</TouchableHighlight>
</Animated.View>
{this.props.navigator ? false : <LightboxOverlay galleryMode={this.props.galleryMode} GKey={this.props.GKey} {...this.getOverlayProps()} />}
</View>
);
}
}