-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSafeAreaView.js
More file actions
83 lines (70 loc) · 2.01 KB
/
SafeAreaView.js
File metadata and controls
83 lines (70 loc) · 2.01 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
import React, { Component } from 'react'
import {
View,
SafeAreaView
} from 'react-native'
import {
deviceWidth,
deviceHeight,
ifIPhoneX,
ifHorizontal,
getStatusBarHeight,
getBottomSpace
} from './device'
class CompatibleSafeAreaView extends Component {
constructor (props) {
super(props)
this.state = {
deviceWidth: deviceWidth(),
children: props.children
}
this._monitoring = this._monitoring.bind(this)
this.children = props.children
}
render() {
const { style, children } = this.props
return (
<View
{ ...this.props }
style={ [style, this._container()] }
onLayout={ this._monitoring }
ref={ ref => this.container = ref }
>
{children}
</View>
)
}
_container () {
return ifIPhoneX(() => ifHorizontal({
paddingHorizontal: 44
}, {
paddingTop: 44
}))
}
_calculation (e) {
const currentWindow = e.nativeEvent.layout
const currentDeviceHeight = deviceHeight()
const correctHeight = currentDeviceHeight - getStatusBarHeight() - getBottomSpace()
const currentHeight = ifHorizontal(() => currentWindow.width, () => currentWindow.height)
if (currentHeight >= correctHeight) {
ifIPhoneX(() => {
this.container.setNativeProps({
style: {
paddingBottom: getBottomSpace()
}
})
})
}
}
_monitoring (e) {
this.props.onLayout && this.props.onLayout(e)
const currentDeviceWidth = deviceWidth()
this._calculation(e)
if (currentDeviceWidth !== this.state.deviceWidth) {
this.setState({
deviceWidth: currentDeviceWidth
})
}
}
}
export default SafeAreaView || CompatibleSafeAreaView