Skip to content

Commit 234f89f

Browse files
authored
feat: handle notch internally by the library (#154)
1 parent 937dff6 commit 234f89f

File tree

7 files changed

+48
-16
lines changed

7 files changed

+48
-16
lines changed

docs/docs/intro/comprehensive-configuration/global-notifications-settings.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ const { useNotifications, NotificationsProvider } = createNotifications({
3737
gestureConfig: { direction: 'y' },
3838
})
3939
```
40+
41+
## Notch handling
42+
43+
The library handles notch detection automatically so you don't have to worry about configuring this on your side. However, if you wish to take control over this by yourself, you can do that by declaring `isNotch` property in the global config.
44+
45+
```jsx
46+
const { useNotifications, NotificationsProvider } = createNotifications({
47+
...
48+
isNotch: true,
49+
})
50+
```
51+
52+
Feel free to use for example `react-native-device-info`.

example/ios/Podfile.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ PODS:
341341
- React-perflogger (= 0.66.3)
342342
- RNCMaskedView (0.2.6):
343343
- React-Core
344-
- RNDeviceInfo (8.7.1):
345-
- React-Core
346344
- RNGestureHandler (2.1.1):
347345
- React-Core
348346
- RNReanimated (2.3.1):
@@ -435,7 +433,6 @@ DEPENDENCIES:
435433
- React-runtimeexecutor (from `../node_modules/react-native/ReactCommon/runtimeexecutor`)
436434
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
437435
- "RNCMaskedView (from `../node_modules/@react-native-masked-view/masked-view`)"
438-
- RNDeviceInfo (from `../node_modules/react-native-device-info`)
439436
- RNGestureHandler (from `../node_modules/react-native-gesture-handler`)
440437
- RNReanimated (from `../node_modules/react-native-reanimated`)
441438
- RNScreens (from `../node_modules/react-native-screens`)
@@ -521,8 +518,6 @@ EXTERNAL SOURCES:
521518
:path: "../node_modules/react-native/ReactCommon"
522519
RNCMaskedView:
523520
:path: "../node_modules/@react-native-masked-view/masked-view"
524-
RNDeviceInfo:
525-
:path: "../node_modules/react-native-device-info"
526521
RNGestureHandler:
527522
:path: "../node_modules/react-native-gesture-handler"
528523
RNReanimated:
@@ -577,7 +572,6 @@ SPEC CHECKSUMS:
577572
React-runtimeexecutor: bbbdb3d8fcf327c6e2249ee71b6ef1764b7dc266
578573
ReactCommon: 9bac022ab71596f2b0fde1268272543184c63971
579574
RNCMaskedView: c298b644a10c0c142055b3ae24d83879ecb13ccd
580-
RNDeviceInfo: aad3c663b25752a52bf8fce93f2354001dd185aa
581575
RNGestureHandler: e1ad51d31a580755079d5124d3ab66f0fcaa8311
582576
RNReanimated: da3860204e5660c0dd66739936732197d359d753
583577
RNScreens: 522705f2e5c9d27efb17f24aceb2bf8335bc7b8e

example/src/screens/DefaultExamples.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { RemoveButton } from '../components/basicExamples/RemoveButton'
1010
import { styles } from './styles'
1111

1212
const { useNotifications, NotificationsProvider } = createNotifications({
13-
isNotch: true,
1413
notificationPosition: 'top',
1514
defaultStylesSettings: {
1615
errorConfig: {

example/yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5629,11 +5629,6 @@ react-native-codegen@^0.0.7:
56295629
jscodeshift "^0.11.0"
56305630
nullthrows "^1.1.1"
56315631

5632-
react-native-device-info@^8.7.1:
5633-
version "8.7.1"
5634-
resolved "https://registry.yarnpkg.com/react-native-device-info/-/react-native-device-info-8.7.1.tgz#fbb06f87dbbc4423abe713874699fb2e6e99fd15"
5635-
integrity sha512-cVMZztFa2Qn6qpQa601W61CtUwZQ1KXfqCOeltejAWEXmgIWivC692WGSdtGudj4upSi1UgMSaGcvKjfcpdGjg==
5636-
56375632
56385633
version "2.1.1"
56395634
resolved "https://registry.npmjs.org/react-native-gesture-handler/-/react-native-gesture-handler-2.1.1.tgz#1d417bc7b551ec76129ce09861b3a01622b4ef99"

src/core/hooks/useNotificationsStates.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
import { useReducer, useRef, useState } from 'react'
12
import { useWindowDimensions } from 'react-native'
23
import { useNotificationConfig } from './useNotificationConfig'
3-
import { useReducer, useRef, useState } from 'react'
44
import { getTopOffset, mergeConfigs } from '../utils/pickers'
55
import { queueReducer } from '../utils/queueReducer'
6+
import { useStatusBarHeightDetector } from './useStatusBarHeightDetector'
67

78
export const useNotificationsStates = () => {
89
const panHandlerRef = useRef(null)
910
const longPressHandlerRef = useRef(null)
1011
const { height: windowHeight, width: windowWidth } = useWindowDimensions()
12+
const isPortaitMode = windowHeight > windowWidth
13+
const { statusBarHeight } = useStatusBarHeightDetector({ isPortaitMode })
1114
const globalConfig = useNotificationConfig()
1215
const [notificationsQueue, dispatch] = useReducer(queueReducer, [])
1316
const [notificationHeight, setNotificationHeight] = useState(0)
1417

15-
const isPortaitMode = windowHeight > windowWidth
1618
const notificationEvent = notificationsQueue[0]
1719
const config = mergeConfigs(globalConfig, notificationEvent)
1820

@@ -21,6 +23,7 @@ export const useNotificationsStates = () => {
2123
notificationHeight,
2224
isPortaitMode,
2325
windowHeight,
26+
statusBarHeight,
2427
})
2528

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

src/core/utils/pickers.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ import type { EmitParam } from '../services/types'
33
import type { DefaultKeys } from '../../defaultConfig/defaultConfig'
44
import type { DefaultStylesConfigs } from '../../defaultConfig/types'
55
import type { KeyType } from '../../types/misc'
6+
import { Constants } from '../config'
67

78
type GetOffsetTopProps = {
89
globalConfig: NotificationsConfig<VariantsMap>
910
notificationHeight: number
1011
isPortaitMode: boolean
1112
windowHeight: number
13+
statusBarHeight: number
1214
}
1315

1416
export const getTopOffset = ({
1517
globalConfig,
1618
notificationHeight,
1719
isPortaitMode,
1820
windowHeight,
21+
statusBarHeight,
1922
}: GetOffsetTopProps) => {
2023
const isNotch = globalConfig.isNotch
21-
const extraSpace = 50
22-
const topPosition = isNotch && isPortaitMode ? extraSpace : 10
24+
const extraSpace = statusBarHeight + 10
25+
26+
const shouldRenderExtraSpace = isNotch ?? (isPortaitMode && !Constants.isAndroid)
27+
const topPosition = shouldRenderExtraSpace ? extraSpace : 10
2328
const notificationPosition = globalConfig.notificationPosition
2429

2530
switch (notificationPosition) {

0 commit comments

Comments
 (0)