-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(uikit): support compatibility for removing AppState listener unde…
…r 0.65
- Loading branch information
Showing
6 changed files
with
71 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useEffect, useMemo, useRef } from 'react'; | ||
import { AppState, AppStateEvent, AppStateStatus } from 'react-native'; | ||
import { EdgeInsets, useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
|
||
type EdgePaddingMap = { | ||
left: 'paddingLeft'; | ||
right: 'paddingRight'; | ||
top: 'paddingTop'; | ||
bottom: 'paddingBottom'; | ||
}; | ||
const edgePaddingMap: EdgePaddingMap = { | ||
left: 'paddingLeft', | ||
right: 'paddingRight', | ||
top: 'paddingTop', | ||
bottom: 'paddingBottom', | ||
}; | ||
|
||
export const useSafeAreaPadding = < | ||
T extends keyof EdgeInsets, | ||
Result extends { [key in EdgePaddingMap[T]]: EdgeInsets[T] }, | ||
>( | ||
edges: T[], | ||
): Result => { | ||
const safeAreaInsets = useSafeAreaInsets(); | ||
return useMemo(() => { | ||
return edges.reduce((map, edge) => { | ||
const paddingKey = edgePaddingMap[edge]; | ||
map[paddingKey] = safeAreaInsets[edge]; | ||
return map; | ||
}, {} as { [key in EdgePaddingMap[T]]: EdgeInsets[T] }); | ||
}, edges) as Result; | ||
}; | ||
|
||
type AppStateListener = (status: AppStateStatus) => void; | ||
|
||
export const useAppState = (type: AppStateEvent, listener: AppStateListener) => { | ||
const callbackRef = useRef<AppStateListener>(listener); | ||
callbackRef.current = listener; | ||
|
||
useEffect(() => { | ||
const eventListener = (state: AppStateStatus) => callbackRef.current(state); | ||
const subscriber = AppState.addEventListener(type, eventListener); | ||
|
||
return () => { | ||
// @ts-ignore | ||
if (subscriber?.remove) { | ||
subscriber.remove(); | ||
} | ||
// @ts-ignore | ||
else if (AppState.removeEventListener) { | ||
// @ts-ignore | ||
AppState.removeEventListener(type, eventListener); | ||
} | ||
}; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters