forked from jgkim/react-native-status-bar-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusBarSizeIOS.js
117 lines (105 loc) · 3.21 KB
/
StatusBarSizeIOS.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
/**
* @providesModule StatusBarSizeIOS
* @flow
*/
'use strict';
const { NativeEventEmitter, StatusBarIOS, NativeModules } = require('react-native');
const { RNStatusBarSize } = NativeModules;
const StatusBarEmitter = new NativeEventEmitter(RNStatusBarSize);
var DEVICE_STATUS_BAR_HEIGHT_EVENTS = {
willChange: 'statusBarFrameWillChange',
didChange: 'statusBarFrameDidChange',
change: 'statusBarFrameDidChange',
};
var _statusBarSizeHandlers = {};
var noop = function() {};
/**
* `StatusBarSizeIOS` can tell you what the current height of the status bar
* is, so that you can adjust your layout accordingly when a phone call
* notification comes up, for example.
*
* ### Basic Usage
*
* To see the current height, you can check `StatusBarSizeIOS.currentHeight`, which
* will be kept up-to-date. However, `currentHeight` will be null at launch
* while `StatusBarSizeIOS` retrieves it over the bridge.
*
* ```
* getInitialState: function() {
* return {
* currentStatusBarHeight: StatusBarSizeIOS.currentHeight,
* };
* },
* componentDidMount: function() {
* StatusBarSizeIOS.addEventListener('willChange', this._handleStatusBarFrameWillChange);
* StatusBarSizeIOS.addEventListener('didChange', this._handleStatusBarFrameDidChange);
* },
* componentWillUnmount: function() {
* StatusBarSizeIOS.removeEventListener('willChange', this._handleStatusBarFrameWillChange);
* StatusBarSizeIOS.removeEventListener('didChange', this._handleStatusBarFrameDidChange);
* },
* _handleStatusBarFrameWillChange: function(upcomingStatusBarHeight) {
* console.log('Upcoming StatusBar Height:' + upcomingStatusBarHeight);
* },
* _handleStatusBarFrameDidChange: function(currentStatusBarHeight) {
* this.setState({ currentStatusBarHeight, });
* },
* render: function() {
* return (
* <Text>Current status bar height is: {this.state.currentStatusBarHeight}</Text>
* );
* },
* ```
*
* Open up the phone call status bar in the simulator to see it change.
*/
var StatusBarSizeIOS = {
/**
* Add a handler to Status Bar size changes by listening to the event type
* and providing the handler.
*
* Possible event types: change (deprecated), willChange, didChange
*/
addEventListener: function(
type: string,
handler: Function
) {
_statusBarSizeHandlers[handler] = StatusBarIOS.addListener(
DEVICE_STATUS_BAR_HEIGHT_EVENTS[type],
(statusBarSizeData) => {
handler(statusBarSizeData.height);
}
);
},
/**
* Remove a handler by passing the event type and the handler
*/
removeEventListener: function(
type: string,
handler: Function
) {
if (!_statusBarSizeHandlers[handler]) {
return;
}
_statusBarSizeHandlers[handler].remove();
_statusBarSizeHandlers[handler] = null;
},
currentHeight: (null : ?number),
};
StatusBarIOS.addListener(
DEVICE_STATUS_BAR_HEIGHT_EVENTS.didChange,
(statusBarData) => {
StatusBarSizeIOS.currentHeight = statusBarData.height;
}
);
//Wrap in try catch to avoid error on android
try {
RNStatusBarSize.getCurrentStatusBarHeight(
(statusBarData) => {
StatusBarSizeIOS.currentHeight = statusBarData.height;
},
noop
);
} catch (e) {
}
module.exports = StatusBarSizeIOS;