-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
76 lines (60 loc) · 1.89 KB
/
index.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
import { NativeModules, DeviceEventEmitter, Platform } from 'react-native';
const { RNImmersiveMode } = NativeModules;
const checkModule = () => {
if (Platform.OS === 'android' && !RNImmersiveMode) {
throw Error('RNImmersiveMode is not properly linked');
}
// else: (maybe iOS)
return Platform.OS === 'android' && RNImmersiveMode;
}
const ImmersiveMode = {
fullLayout(full) {
if (checkModule()) {
RNImmersiveMode.fullLayout(full);
}
},
setBarMode(mode) {
if (checkModule()) {
RNImmersiveMode.setBarMode(mode);
}
},
setBarStyle(style) {
if (checkModule()) {
RNImmersiveMode.setBarStyle(style);
}
},
setBarTranslucent(enable) {
if (checkModule()) {
RNImmersiveMode.setBarTranslucent(enable);
}
},
setBarColor(color) {
if (checkModule()) {
if (typeof color === 'string') {
if (color.length === 9) {
// convert #rgba to #argb
color = '#' + color.substr(7, 2) + color.substr(1, 6);
} else if (color.length === 4) {
color = '#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
}
}
RNImmersiveMode.setBarColor(color);
}
},
setBarDefaultColor() {
if (checkModule()) {
RNImmersiveMode.setBarColor(null);
}
},
addEventListener(callback) {
if (checkModule()) {
if (typeof callback !== 'function') return;
RNImmersiveMode.setOnSystemUiVisibilityChangeListener();
const subscription = DeviceEventEmitter.addListener(
RNImmersiveMode.OnSystemUiVisibilityChange,
(e) => callback(e));
return subscription;
}
}
}
export default ImmersiveMode;