forked from oblador/react-native-lightbox
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Lightbox.js
139 lines (121 loc) · 3.23 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
import React, {
Component,
Children,
cloneElement,
useState,
useRef,
} from "react";
import { Animated, TouchableOpacity, View } from "react-native";
import PropTypes from "prop-types";
import LightboxOverlay from "./LightboxOverlay";
const Lightbox = (props) => {
const layoutOpacity = useRef(new Animated.Value(1));
const _root = useRef();
const [isOpen, setIsOpen] = useState(false);
const [origin, setOrigin] = useState({
x: 0,
y: 0,
width: 0,
height: 0,
});
getContent = () => {
if (props.renderContent) {
return props.renderContent();
} else if (props.activeProps) {
return cloneElement(Children.only(props.children), props.activeProps);
}
return props.children;
};
getOverlayProps = () => ({
isOpen: isOpen,
origin: origin,
renderHeader: props.renderHeader,
swipeToDismiss: props.swipeToDismiss,
springConfig: props.springConfig,
backgroundColor: props.backgroundColor,
children: getContent(),
didOpen: props.didOpen,
willClose: props.willClose,
onClose: onClose,
});
open = () => {
_root.current.measure((ox, oy, width, height, px, py) => {
props.onOpen();
setIsOpen(props.navigator ? true : false);
setOrigin({
width,
height,
x: px,
y: py,
});
props.didOpen();
if (props.navigator) {
const route = {
component: LightboxOverlay,
passProps: getOverlayProps(),
};
const routes = props.navigator.getCurrentRoutes();
routes.push(route);
props.navigator.immediatelyResetRouteStack(routes);
} else {
setIsOpen(true);
}
setTimeout(() => {
_root && _root.current && layoutOpacity.current.setValue(0);
});
});
};
close = () => {
throw new Error(
"Lightbox.close method is deprecated. Use renderHeader(close) prop instead."
);
};
onClose = () => {
layoutOpacity.current.setValue(1);
setIsOpen(false);
if (props.navigator) {
const routes = props.navigator.getCurrentRoutes();
routes.pop();
props.navigator.immediatelyResetRouteStack(routes);
}
};
return (
<View ref={_root} style={props.style} onLayout={() => {}}>
<Animated.View style={{ opacity: layoutOpacity.current }}>
<TouchableOpacity
underlayColor={props.underlayColor}
onPress={open}
onLongPress={props.onLongPress}
>
{props.children}
</TouchableOpacity>
</Animated.View>
{props.navigator ? false : <LightboxOverlay {...getOverlayProps()} />}
</View>
);
};
Lightbox.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,
};
Lightbox.defaultProps = {
swipeToDismiss: true,
onOpen: () => {},
didOpen: () => {},
willClose: () => {},
onClose: () => {},
onLongPress: () => {},
};
export default Lightbox;