Skip to content

Commit 3b77b34

Browse files
authored
Merge pull request #5 from logicwind/dev
Added bottom inset support
2 parents 7723828 + 3cd4b96 commit 3b77b34

File tree

8 files changed

+146
-26
lines changed

8 files changed

+146
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.0
2+
3+
- Added support for fetching **bottom safe area inset** using native modules.
4+
- Improved example layout for better visual clarity and margin usage.
5+
16
## 0.1.0
27

38
- Fetch status bar height on **iOS** using native modules.

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @logicwind/react-native-status-bar-height
22

3-
A simple and lightweight React Native package to get the height of the status bar on both Android and iOS.
3+
A simple and lightweight React Native package to get the **status bar height** and **bottom safe area inset** on both Android and iOS.
44

55
### Installation
66

@@ -30,18 +30,43 @@ No additional setup is required.
3030

3131
## Usage
3232

33-
Import and use the `fetchStatusBarHeight` function to get the status bar height.
33+
Import and use the utility functions to fetch values:
3434

3535
```tsx md title="App.tsx"
36-
import { fetchStatusBarHeight } from '@logicwind/react-native-status-bar-height';
36+
import {
37+
fetchStatusBarHeight,
38+
fetchBottonInset,
39+
} from '@logicwind/react-native-status-bar-height';
3740

3841
const statusBarHeight = fetchStatusBarHeight();
42+
const bottomInset = fetchBottonInset();
3943
```
4044

45+
## Example
46+
```tsx md title="App.tsx"
47+
<View style={{ flex: 1 }}>
48+
<Text style={{ marginTop: fetchStatusBarHeight() }}>
49+
Status Bar Height
50+
</Text>
51+
<View style={{ flex: 1 }} />
52+
<Text style={{ marginBottom: fetchBottonInset() }}>
53+
Bottom Safe Area
54+
</Text>
55+
</View>
56+
```
57+
58+
## API
59+
`fetchStatusBarHeight(): number` - Returns the status bar height in pixels.
60+
61+
`fetchBottomInset(): number` - Returns the bottom safe area inset in pixels, helpful for avoiding overlap with gesture/navigation bars.
62+
4163
## How It Works
4264

43-
- On **iOS**, it attempts to fetch the status bar height using native modules. If unavailable, it falls back to predefined values based on screen dimensions.
44-
- On **Android**, it retrieves `StatusBar.currentHeight`.
65+
- On iOS, it retrieves values using native APIs or falls back to common values.
66+
67+
- On Android, it uses StatusBar.currentHeight for the status bar height, and WindowInsets for the bottom inset when available.
68+
69+
- A fallback mechanism is in place to provide reasonable defaults.
4570

4671
## react-native-status-bar-height is crafted mindfully at [Logicwind](https://www.logicwind.com?utm_source=github&utm_medium=github.com-logicwind&utm_campaign=react-native-status-bar-height)
4772

android/src/main/java/com/logicwind/reactnativestatusbarheight/ReactNativeStatusBarHeightModule.kt

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.logicwind.reactnativestatusbarheight
22

3+
import android.app.Activity
4+
import android.os.Build
5+
import android.view.WindowInsets
36
import com.facebook.react.bridge.ReactApplicationContext
47
import com.facebook.react.bridge.ReactContextBaseJavaModule
5-
import com.facebook.react.bridge.ReactMethod
6-
import com.facebook.react.bridge.Promise
78

89
class ReactNativeStatusBarHeightModule(reactContext: ReactApplicationContext) :
910
ReactContextBaseJavaModule(reactContext) {
@@ -12,11 +13,35 @@ class ReactNativeStatusBarHeightModule(reactContext: ReactApplicationContext) :
1213
return NAME
1314
}
1415

15-
// Example method
16-
// See https://reactnative.dev/docs/native-modules-android
17-
@ReactMethod
18-
fun multiply(a: Double, b: Double, promise: Promise) {
19-
promise.resolve(a * b)
16+
override fun getConstants(): Map<String, Any> {
17+
val constants = mutableMapOf<String, Any>()
18+
19+
val activity: Activity? = currentActivity
20+
var bottomInset = 0
21+
22+
if (activity != null) {
23+
val rootView = activity.window.decorView.rootView
24+
val insets: WindowInsets? = rootView.rootWindowInsets
25+
26+
if (insets != null) {
27+
bottomInset = when {
28+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
29+
insets.getInsets(WindowInsets.Type.systemBars()).bottom
30+
}
31+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> {
32+
insets.systemWindowInsetBottom
33+
}
34+
else -> 0
35+
}
36+
}
37+
}
38+
39+
val density = reactApplicationContext.resources.displayMetrics.density
40+
val bottomInsetDp = bottomInset / density
41+
42+
constants["BOTTOM_SAFE_AREA"] = bottomInsetDp
43+
44+
return constants
2045
}
2146

2247
companion object {

example/src/App.tsx

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
1-
import { Text, View } from 'react-native';
2-
import { fetchStatusBarHeight } from '@logicwind/react-native-status-bar-height';
1+
import { View, Text, StyleSheet } from 'react-native';
2+
import {
3+
fetchBottomInset,
4+
fetchStatusBarHeight,
5+
} from '@logicwind/react-native-status-bar-height';
36

4-
export default function App() {
5-
const height = fetchStatusBarHeight();
7+
const App = () => {
8+
const statusBarHeight = fetchStatusBarHeight();
9+
const bottomInset = fetchBottomInset();
610

711
return (
8-
<View style={{ marginTop: height }}>
9-
<Text>Status Bar Height: {height}</Text>
12+
<View style={styles.container}>
13+
<View style={[styles.header, { marginTop: statusBarHeight }]}>
14+
<Text style={styles.label}>Status Bar Height: {statusBarHeight}px</Text>
15+
</View>
16+
17+
<View style={styles.content}>
18+
<Text style={styles.title}>Welcome to the App</Text>
19+
</View>
20+
21+
<View style={[styles.footer, { marginBottom: bottomInset }]}>
22+
<Text style={styles.label}>Bottom Inset: {bottomInset}px</Text>
23+
</View>
1024
</View>
1125
);
12-
}
26+
};
27+
28+
const styles = StyleSheet.create({
29+
container: {
30+
flex: 1,
31+
backgroundColor: '#4B7BE5',
32+
},
33+
header: {
34+
alignItems: 'center',
35+
backgroundColor: '#f5f5f5',
36+
},
37+
content: {
38+
flex: 1,
39+
alignItems: 'center',
40+
justifyContent: 'center',
41+
backgroundColor: '#f5f5f5',
42+
},
43+
footer: {
44+
alignItems: 'center',
45+
backgroundColor: '#f5f5f5',
46+
},
47+
label: {
48+
fontSize: 16,
49+
},
50+
title: {
51+
fontSize: 20,
52+
fontWeight: '600',
53+
},
54+
});
55+
56+
export default App;

ios/ReactNativeStatusBarHeight.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ class ReactNativeStatusBarHeight: NSObject {
77
@objc
88
func constantsToExport() -> [AnyHashable: Any] {
99
var statusBarHeight: CGFloat = 0
10-
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
11-
statusBarHeight = windowScene.statusBarManager?.statusBarFrame.height ?? 0
10+
var bottomSafeArea: CGFloat = 0
11+
12+
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
13+
let window = windowScene.windows.first {
14+
statusBarHeight = window.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
15+
bottomSafeArea = window.safeAreaInsets.bottom
1216
}
13-
return ["STATUS_BAR_HEIGHT": statusBarHeight]
17+
18+
return [
19+
"STATUS_BAR_HEIGHT": statusBarHeight,
20+
"BOTTOM_SAFE_AREA": bottomSafeArea
21+
]
1422
}
1523

1624
@objc

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@logicwind/react-native-status-bar-height",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "A React Native library for retrieving the status bar height on both Android and iOS.",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",
@@ -55,7 +55,12 @@
5555
"react-native-status-bar",
5656
"react-native-status-bar-height-ios",
5757
"react-native-status-bar-height-android",
58-
"react-native-status-bar-height-ios-android"
58+
"react-native-status-bar-height-ios-android",
59+
"bottom-inset",
60+
"safe-area",
61+
"safe-area-inset",
62+
"react-native-bottom-inset",
63+
"react-native-safe-area"
5964
],
6065
"repository": {
6166
"type": "git",

src/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,11 @@ export const fetchStatusBarHeight = (): number => {
7070

7171
return StatusBar.currentHeight ?? 0;
7272
};
73+
74+
export const fetchBottomInset = (): number => {
75+
try {
76+
return ReactNativeStatusBarHeight.BOTTOM_SAFE_AREA;
77+
} catch {
78+
return 0;
79+
}
80+
};

0 commit comments

Comments
 (0)