forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fc9f27
commit c52cbaa
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/libs/Navigation/AppNavigator/FreezeWrapper/getIsScreenBlurred.ts
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,9 @@ | ||
import type {NavigationState} from '@react-navigation/native'; | ||
import {isFullScreenName} from '@libs/NavigationUtils'; | ||
|
||
function getIsScreenBlurred(state: NavigationState, currentRouteKey: string) { | ||
const lastFullScreenRoute = state.routes.findLast((route) => isFullScreenName(route.name)); | ||
return lastFullScreenRoute?.key !== currentRouteKey; | ||
} | ||
|
||
export default getIsScreenBlurred; |
28 changes: 28 additions & 0 deletions
28
src/libs/Navigation/AppNavigator/FreezeWrapper/index.native.tsx
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,28 @@ | ||
import {useNavigation, useRoute} from '@react-navigation/native'; | ||
import React, {useEffect, useState} from 'react'; | ||
import {Freeze} from 'react-freeze'; | ||
import type ChildrenProps from '@src/types/utils/ChildrenProps'; | ||
import getIsScreenBlurred from './getIsScreenBlurred'; | ||
|
||
type FreezeWrapperProps = ChildrenProps & { | ||
/** Prop to disable freeze */ | ||
keepVisible?: boolean; | ||
}; | ||
|
||
function FreezeWrapper({keepVisible = false, children}: FreezeWrapperProps) { | ||
const navigation = useNavigation(); | ||
const currentRoute = useRoute(); | ||
|
||
const [isScreenBlurred, setIsScreenBlurred] = useState(false); | ||
|
||
useEffect(() => { | ||
const unsubscribe = navigation.addListener('state', (e) => setIsScreenBlurred(getIsScreenBlurred(e.data.state, currentRoute.key))); | ||
return () => unsubscribe(); | ||
}, [currentRoute.key, navigation]); | ||
|
||
return <Freeze freeze={isScreenBlurred && !keepVisible}>{children}</Freeze>; | ||
} | ||
|
||
FreezeWrapper.displayName = 'FreezeWrapper'; | ||
|
||
export default FreezeWrapper; |
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,35 @@ | ||
import {useNavigation, useRoute} from '@react-navigation/native'; | ||
import React, {useEffect, useLayoutEffect, useState} from 'react'; | ||
import {Freeze} from 'react-freeze'; | ||
import type ChildrenProps from '@src/types/utils/ChildrenProps'; | ||
import getIsScreenBlurred from './getIsScreenBlurred'; | ||
|
||
type FreezeWrapperProps = ChildrenProps & { | ||
/** Prop to disable freeze */ | ||
keepVisible?: boolean; | ||
}; | ||
|
||
function FreezeWrapper({keepVisible = false, children}: FreezeWrapperProps) { | ||
const navigation = useNavigation(); | ||
const currentRoute = useRoute(); | ||
|
||
const [isScreenBlurred, setIsScreenBlurred] = useState(false); | ||
const [freezed, setFreezed] = useState(false); | ||
|
||
useEffect(() => { | ||
const unsubscribe = navigation.addListener('state', (e) => setIsScreenBlurred(getIsScreenBlurred(e.data.state, currentRoute.key))); | ||
return () => unsubscribe(); | ||
}, [currentRoute.key, navigation]); | ||
|
||
// Decouple the Suspense render task so it won't be interrupted by React's concurrent mode | ||
// and stuck in an infinite loop | ||
useLayoutEffect(() => { | ||
setFreezed(isScreenBlurred && !keepVisible); | ||
}, [isScreenBlurred, keepVisible]); | ||
|
||
return <Freeze freeze={freezed}>{children}</Freeze>; | ||
} | ||
|
||
FreezeWrapper.displayName = 'FreezeWrapper'; | ||
|
||
export default FreezeWrapper; |