SafeAreaView.js
12.8 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactNative = require('react-native');
var _withOrientation = require('./withOrientation');
var _withOrientation2 = _interopRequireDefault(_withOrientation);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
// See https://mydevice.io/devices/ for device dimensions
var X_WIDTH = 375;
var X_HEIGHT = 812;
var PAD_WIDTH = 768;
var PAD_HEIGHT = 1024;
var _Dimensions$get = _reactNative.Dimensions.get('window'),
D_HEIGHT = _Dimensions$get.height,
D_WIDTH = _Dimensions$get.width;
var _NativeModules$Platfo = _reactNative.NativeModules.PlatformConstants,
PlatformConstants = _NativeModules$Platfo === undefined ? {} : _NativeModules$Platfo;
var _ref = PlatformConstants.reactNativeVersion || {},
_ref$minor = _ref.minor,
minor = _ref$minor === undefined ? 0 : _ref$minor;
var isIPhoneX = function () {
if (_reactNative.Platform.OS === 'web') return false;
if (minor >= 50) {
return _reactNative.DeviceInfo.isIPhoneX_deprecated;
}
return _reactNative.Platform.OS === 'ios' && (D_HEIGHT === X_HEIGHT && D_WIDTH === X_WIDTH || D_HEIGHT === X_WIDTH && D_WIDTH === X_HEIGHT);
}();
var isIPad = function () {
if (_reactNative.Platform.OS !== 'ios' || isIPhoneX) return false;
// if portrait and width is smaller than iPad width
if (D_HEIGHT > D_WIDTH && D_WIDTH < PAD_WIDTH) {
return false;
}
// if landscape and height is smaller that iPad height
if (D_WIDTH > D_HEIGHT && D_HEIGHT < PAD_WIDTH) {
return false;
}
return true;
}();
var statusBarHeight = function statusBarHeight(isLandscape) {
if (isIPhoneX) {
return isLandscape ? 0 : 44;
}
if (isIPad) {
return 20;
}
return isLandscape ? 0 : 20;
};
var doubleFromPercentString = function doubleFromPercentString(percent) {
if (!percent.includes('%')) {
return 0;
}
var dbl = parseFloat(percent) / 100;
if (isNaN(dbl)) return 0;
return dbl;
};
var SafeView = function (_Component) {
_inherits(SafeView, _Component);
function SafeView() {
var _ref2;
var _temp, _this, _ret;
_classCallCheck(this, SafeView);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = SafeView.__proto__ || Object.getPrototypeOf(SafeView)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
touchesTop: true,
touchesBottom: true,
touchesLeft: true,
touchesRight: true,
orientation: null,
viewWidth: 0,
viewHeight: 0
}, _this._onLayout = function () {
if (!_this.view) return;
var isLandscape = _this.props.isLandscape;
var orientation = _this.state.orientation;
var newOrientation = isLandscape ? 'landscape' : 'portrait';
if (orientation && orientation === newOrientation) {
return;
}
var WIDTH = isLandscape ? X_HEIGHT : X_WIDTH;
var HEIGHT = isLandscape ? X_WIDTH : X_HEIGHT;
_this.view._component.measureInWindow(function (winX, winY, winWidth, winHeight) {
var realY = winY;
var realX = winX;
if (realY >= HEIGHT) {
realY = realY % HEIGHT;
} else if (realY < 0) {
realY = realY % HEIGHT + HEIGHT;
}
if (realX >= WIDTH) {
realX = realX % WIDTH;
} else if (realX < 0) {
realX = realX % WIDTH + WIDTH;
}
var touchesTop = realY === 0;
var touchesBottom = realY + winHeight >= HEIGHT;
var touchesLeft = realX === 0;
var touchesRight = realX + winWidth >= WIDTH;
_this.setState({
touchesTop: touchesTop,
touchesBottom: touchesBottom,
touchesLeft: touchesLeft,
touchesRight: touchesRight,
orientation: newOrientation,
viewWidth: winWidth,
viewHeight: winHeight
});
});
}, _this._getSafeAreaStyle = function () {
var _this$state = _this.state,
touchesTop = _this$state.touchesTop,
touchesBottom = _this$state.touchesBottom,
touchesLeft = _this$state.touchesLeft,
touchesRight = _this$state.touchesRight;
var _this$props = _this.props,
forceInset = _this$props.forceInset,
isLandscape = _this$props.isLandscape;
var _this$_getViewStyles = _this._getViewStyles(),
paddingTop = _this$_getViewStyles.paddingTop,
paddingBottom = _this$_getViewStyles.paddingBottom,
paddingLeft = _this$_getViewStyles.paddingLeft,
paddingRight = _this$_getViewStyles.paddingRight,
viewStyle = _this$_getViewStyles.viewStyle;
var style = _extends({}, viewStyle, {
paddingTop: touchesTop ? _this._getInset('top') : 0,
paddingBottom: touchesBottom ? _this._getInset('bottom') : 0,
paddingLeft: touchesLeft ? _this._getInset('left') : 0,
paddingRight: touchesRight ? _this._getInset('right') : 0
});
if (forceInset) {
Object.keys(forceInset).forEach(function (key) {
var inset = forceInset[key];
if (inset === 'always') {
inset = _this._getInset(key);
}
if (inset === 'never') {
inset = 0;
}
switch (key) {
case 'horizontal':
{
style.paddingLeft = inset;
style.paddingRight = inset;
break;
}
case 'vertical':
{
style.paddingTop = inset;
style.paddingBottom = inset;
break;
}
case 'left':
case 'right':
case 'top':
case 'bottom':
{
var padding = 'padding' + key[0].toUpperCase() + key.slice(1);
style[padding] = inset;
break;
}
}
});
}
// new height/width should only include padding from insets
// height/width should not be affected by padding from style obj
if (style.height && typeof style.height === 'number') {
style.height += style.paddingTop + style.paddingBottom;
}
if (style.width && typeof style.width === 'number') {
style.width += style.paddingLeft + style.paddingRight;
}
style.paddingTop += paddingTop;
style.paddingBottom += paddingBottom;
style.paddingLeft += paddingLeft;
style.paddingRight += paddingRight;
return style;
}, _this._getViewStyles = function () {
var viewWidth = _this.state.viewWidth;
// get padding values from style to add back in after insets are determined
// default precedence: padding[Side] -> vertical | horizontal -> padding -> 0
var _StyleSheet$flatten = _reactNative.StyleSheet.flatten(_this.props.style || {}),
_StyleSheet$flatten$p = _StyleSheet$flatten.padding,
padding = _StyleSheet$flatten$p === undefined ? 0 : _StyleSheet$flatten$p,
_StyleSheet$flatten$p2 = _StyleSheet$flatten.paddingVertical,
paddingVertical = _StyleSheet$flatten$p2 === undefined ? padding : _StyleSheet$flatten$p2,
_StyleSheet$flatten$p3 = _StyleSheet$flatten.paddingHorizontal,
paddingHorizontal = _StyleSheet$flatten$p3 === undefined ? padding : _StyleSheet$flatten$p3,
_StyleSheet$flatten$p4 = _StyleSheet$flatten.paddingTop,
paddingTop = _StyleSheet$flatten$p4 === undefined ? paddingVertical : _StyleSheet$flatten$p4,
_StyleSheet$flatten$p5 = _StyleSheet$flatten.paddingBottom,
paddingBottom = _StyleSheet$flatten$p5 === undefined ? paddingVertical : _StyleSheet$flatten$p5,
_StyleSheet$flatten$p6 = _StyleSheet$flatten.paddingLeft,
paddingLeft = _StyleSheet$flatten$p6 === undefined ? paddingHorizontal : _StyleSheet$flatten$p6,
_StyleSheet$flatten$p7 = _StyleSheet$flatten.paddingRight,
paddingRight = _StyleSheet$flatten$p7 === undefined ? paddingHorizontal : _StyleSheet$flatten$p7,
viewStyle = _objectWithoutProperties(_StyleSheet$flatten, ['padding', 'paddingVertical', 'paddingHorizontal', 'paddingTop', 'paddingBottom', 'paddingLeft', 'paddingRight']);
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 {
paddingTop: paddingTop,
paddingBottom: paddingBottom,
paddingLeft: paddingLeft,
paddingRight: paddingRight,
viewStyle: viewStyle
};
}, _this._getInset = function (key) {
var isLandscape = _this.props.isLandscape;
switch (key) {
case 'horizontal':
case 'right':
case 'left':
{
return isLandscape ? isIPhoneX ? 44 : 0 : 0;
}
case 'vertical':
case 'top':
{
return statusBarHeight(isLandscape);
}
case 'bottom':
{
return isIPhoneX ? isLandscape ? 24 : 34 : 0;
}
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(SafeView, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
_reactNative.InteractionManager.runAfterInteractions(function () {
_this2._onLayout();
});
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps() {
this._onLayout();
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var _props = this.props,
_props$forceInset = _props.forceInset,
forceInset = _props$forceInset === undefined ? false : _props$forceInset,
isLandscape = _props.isLandscape,
children = _props.children,
style = _props.style;
if (_reactNative.Platform.OS !== 'ios') {
return _react2.default.createElement(
_reactNative.Animated.View,
{ style: style },
this.props.children
);
}
var safeAreaStyle = this._getSafeAreaStyle();
return _react2.default.createElement(
_reactNative.Animated.View,
{
ref: function ref(c) {
return _this3.view = c;
},
onLayout: this._onLayout,
style: safeAreaStyle
},
this.props.children
);
}
}]);
return SafeView;
}(_react.Component);
exports.default = (0, _withOrientation2.default)(SafeView);