-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
FlashMessageWrapper.js
232 lines (191 loc) · 6.82 KB
/
FlashMessageWrapper.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
import React, { Component } from "react";
import { Dimensions, Platform, StatusBar, StyleSheet } from "react-native";
import { isIphoneX, getStatusBarHeight } from "react-native-iphone-x-helper";
import PropTypes from "prop-types";
/**
* DETECTION AND DIMENSIONS CODE FROM:
* https://github.com/react-community/react-native-safe-area-view
*/
const PAD_WIDTH = 768; // iPad
const PAD_HEIGHT = 1024; // iPad
const { height: D_HEIGHT, width: D_WIDTH } = Dimensions.get("window");
const isAndroid = Platform.OS === "android";
const isIPhoneX = isIphoneX();
const isIPad = (() => {
if (Platform.OS !== "ios" || isIPhoneX) return false;
// if portrait and width are smaller than the iPad's width...
if (D_HEIGHT > D_WIDTH && D_WIDTH < PAD_WIDTH) {
return false; // It is probably not an iPad
}
// if landscape and height are smaller that the iPad's height...
if (D_WIDTH > D_HEIGHT && D_HEIGHT < PAD_WIDTH) {
return false; // It is probably not an iPad
}
// If all verifications go alright
// then it is probably an iPad
return true;
})();
const isOrientationLandscape = ({ width, height }) => width > height;
/**
* Helper function to get the current status bar height to plus in paddingTop message
*/
export function getFlashMessageStatusBarHeight(isLandscape = false, _customStatusBarHeight = null) {
if (_customStatusBarHeight !== null && _customStatusBarHeight !== false) {
return typeof _customStatusBarHeight === "function" ? _customStatusBarHeight(isLandscape) : +_customStatusBarHeight;
}
/**
* This is a temporary workaround because we don't have a way to detect
* if the status bar is translucent or opaque. If opaque, we don't need to
* factor in the height here; if translucent (content renders under it) then
* we do.
*/
if (isAndroid) {
if (!!global && !!global.Expo) {
return +StatusBar.currentHeight + 6;
}
return 6;
}
if (isIPhoneX) {
return isLandscape ? 0 : getStatusBarHeight(true);
}
if (isIPad) {
return 20;
}
return isLandscape ? 0 : 20;
}
const doubleFromPercentString = percent => {
if (!percent || !percent.includes("%")) {
return 0;
}
const dbl = parseFloat(percent) / 100;
if (isNaN(dbl)) return 0;
return dbl;
};
/**
* Helper function to "append" extra padding in MessageComponent style
*/
export function styleWithInset(style, wrapperInset, hideStatusBar = false, prop = "padding") {
if (prop === "margin") {
return styleWithInsetMargin(style, wrapperInset, hideStatusBar);
}
const { width: viewWidth } = Dimensions.get("window");
let {
padding = 0,
paddingVertical = padding,
paddingHorizontal = padding,
paddingTop = paddingVertical,
paddingBottom = paddingVertical,
paddingLeft = paddingHorizontal,
paddingRight = paddingHorizontal,
...viewStyle
} = StyleSheet.flatten(style || {});
if (typeof paddingTop !== "number") {
paddingTop = doubleFromPercentString(paddingTop) * viewWidth;
}
if (typeof paddingBottom !== "number") {
paddingBottom = doubleFromPercentString(paddingBottom) * viewWidth;
}
if (typeof paddingLeft !== "number") {
paddingLeft = doubleFromPercentString(paddingLeft) * viewWidth;
}
if (typeof paddingRight !== "number") {
paddingRight = doubleFromPercentString(paddingRight) * viewWidth;
}
return {
...viewStyle,
paddingTop: !!wrapperInset.isIPhoneX || !hideStatusBar ? paddingTop + wrapperInset.insetTop : paddingTop,
paddingBottom: paddingBottom + wrapperInset.insetBottom,
paddingLeft: paddingLeft + wrapperInset.insetLeft,
paddingRight: paddingRight + wrapperInset.insetRight,
};
}
/**
* Helper function to "append" extra margin in MessageComponent style
*/
export function styleWithInsetMargin(style, wrapperInset, hideStatusBar = false) {
const { width: viewWidth } = Dimensions.get("window");
let {
margin = 0,
marginVertical = margin,
marginHorizontal = margin,
marginTop = marginVertical,
marginBottom = marginVertical,
marginLeft = marginHorizontal,
marginRight = marginHorizontal,
...viewStyle
} = StyleSheet.flatten(style || {});
if (typeof marginTop !== "number") {
marginTop = doubleFromPercentString(marginTop) * viewWidth;
}
if (typeof marginBottom !== "number") {
marginBottom = doubleFromPercentString(marginBottom) * viewWidth;
}
if (typeof marginLeft !== "number") {
marginLeft = doubleFromPercentString(marginLeft) * viewWidth;
}
if (typeof marginRight !== "number") {
marginRight = doubleFromPercentString(marginRight) * viewWidth;
}
return {
...viewStyle,
marginTop: !!wrapperInset.isIPhoneX || !hideStatusBar ? marginTop + wrapperInset.insetTop : marginTop,
marginBottom: marginBottom + wrapperInset.insetBottom,
marginLeft: marginLeft + wrapperInset.insetLeft,
marginRight: marginRight + wrapperInset.insetRight,
};
}
/**
* Utility component wrapper to handle orientation changes and extra padding control for iOS (specially iPads and iPhone X)
*/
export default class FlashMessageWrapper extends Component {
static defaultProps = {
/**
* Default FlashMessage position is "top"
* Other options like "bottom" and "center" use other extra padding configurations
*/
position: "top",
};
static propTypes = {
position: PropTypes.string,
children: PropTypes.func.isRequired,
};
constructor() {
super();
this.handleOrientationChange = this.handleOrientationChange.bind(this);
this.dimensionsSubscription = null;
this.state = {
isLandscape: isOrientationLandscape(Dimensions.get("window")),
};
}
componentDidMount() {
this.dimensionsSubscription = Dimensions.addEventListener("change", this.handleOrientationChange);
}
componentWillUnmount() {
if (!!this.dimensionsSubscription) {
this.dimensionsSubscription.remove();
}
}
handleOrientationChange({ window }) {
const isLandscape = isOrientationLandscape(window);
this.setState({ isLandscape });
}
render() {
const { position, statusBarHeight = null, children } = this.props;
const { isLandscape } = this.state;
const _statusBarHeight = getFlashMessageStatusBarHeight(isLandscape, statusBarHeight);
/**
* This wrapper will return data about extra inset padding, statusBarHeight and some device detection like iPhoneX and iPad
*/
const wrapper = {
isLandscape,
isIPhoneX: isIPhoneX,
isIPad: isIPad,
statusBarHeight: _statusBarHeight,
insetTop: position === "top" ? _statusBarHeight : 0,
insetLeft: (position === "top" || position === "bottom") && isLandscape ? (isIPhoneX ? 21 : 0) : 0,
insetRight: (position === "top" || position === "bottom") && isLandscape ? (isIPhoneX ? 21 : 0) : 0,
insetBottom: isIPhoneX && position === "bottom" ? (isLandscape ? 24 : 34) : isAndroid ? 2 : 0,
};
return children(wrapper);
}
}