forked from oblador/react-native-lightbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lightbox.js
140 lines (129 loc) · 3.71 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
import React, { Component, Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import { Animated, TouchableHighlight, View } from 'react-native';
import LightboxOverlay from './LightboxOverlay';
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,
};
static defaultProps = {
swipeToDismiss: true,
onOpen: () => {},
didOpen: () => {},
willClose: () => {},
onClose: () => {},
};
state = {
isOpen: false,
origin: {
x: 0,
y: 0,
width: 0,
height: 0,
},
layoutOpacity: new Animated.Value(1),
};
getContent = () => {
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 = () => ({
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(),
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);
}
}
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}
>
{this.props.children}
</TouchableHighlight>
</Animated.View>
{this.props.navigator ? false : <LightboxOverlay {...this.getOverlayProps()} />}
</View>
);
}
}