Skip to content

Commit 08b640e

Browse files
fix: split useStatusBarHeightDetector hook into web and native flavors (TheWidlarzGroup#266)
fix: split useStatusBarHeightDetector hook into web and native flavours
1 parent 08f9b95 commit 08b640e

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useEffect, useState } from 'react'
2+
import { NativeModules, Platform, StatusBar } from 'react-native'
3+
4+
type Props = {
5+
isPortraitMode: boolean
6+
}
7+
8+
const NativeStatusBarManager = require('react-native/Libraries/Components/StatusBar/NativeStatusBarManagerIOS')
9+
10+
export const useStatusBarHeightDetector = ({ isPortraitMode }: Props) => {
11+
const { StatusBarManager } = NativeModules
12+
const [barHeight, setBarHeight] = useState(0)
13+
14+
useEffect(() => {
15+
if (Platform.OS !== 'ios') return setBarHeight(StatusBar.currentHeight ?? 0)
16+
// handling edge case when app is opened in landscape mode and barHeight = 0
17+
const StatusBarManagerModule = NativeStatusBarManager?.default || StatusBarManager
18+
19+
StatusBarManagerModule?.getHeight(({ height }: { height: number }) =>
20+
setBarHeight(isPortraitMode && height !== 0 ? height : 50)
21+
)
22+
}, [StatusBarManager, isPortraitMode])
23+
24+
return {
25+
statusBarHeight: barHeight,
26+
}
27+
}
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
import { useEffect, useState } from 'react'
2-
import { NativeModules, Platform, StatusBar } from 'react-native'
3-
41
type Props = {
52
isPortraitMode: boolean
63
}
74

8-
const NativeStatusBarManager = require('react-native/Libraries/Components/StatusBar/NativeStatusBarManagerIOS')
9-
10-
export const useStatusBarHeightDetector = ({ isPortraitMode }: Props) => {
11-
const { StatusBarManager } = NativeModules
12-
const [barHeight, setBarHeight] = useState(0)
13-
14-
useEffect(() => {
15-
if (Platform.OS !== 'ios') return setBarHeight(StatusBar.currentHeight ?? 0)
16-
// handling edge case when app is opened in landscape mode and barHeight = 0
17-
const StatusBarManagerModule = NativeStatusBarManager?.default || StatusBarManager
18-
19-
StatusBarManagerModule?.getHeight(({ height }: { height: number }) =>
20-
setBarHeight(isPortraitMode && height !== 0 ? height : 50)
21-
)
22-
}, [StatusBarManager, isPortraitMode])
23-
5+
export const useStatusBarHeightDetector = ({ isPortraitMode: _ }: Props) => {
246
return {
25-
statusBarHeight: barHeight,
7+
statusBarHeight: 0,
268
}
279
}

0 commit comments

Comments
 (0)