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
Showing
8 changed files
with
209 additions
and
104 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
6 changes: 3 additions & 3 deletions
6
src/libs/Navigation/AppNavigator/Navigators/FullScreenNavigator.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
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
src/libs/Navigation/AppNavigator/createCustomFullScreenNavigator/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,42 @@ | ||
import {createNavigatorFactory, ParamListBase, StackActionHelpers, StackNavigationState, useNavigationBuilder} from '@react-navigation/native'; | ||
import {StackNavigationEventMap, StackNavigationOptions, StackView} from '@react-navigation/stack'; | ||
import React, {useRef} from 'react'; | ||
import useWindowDimensions from '@hooks/useWindowDimensions'; | ||
import CustomFullScreenRouter from './CustomFullScreenRouter'; | ||
import type {ResponsiveStackNavigatorProps, ResponsiveStackNavigatorRouterOptions} from './types'; | ||
|
||
function ResponsiveStackNavigator(props: ResponsiveStackNavigatorProps) { | ||
const {isSmallScreenWidth} = useWindowDimensions(); | ||
|
||
const isSmallScreenWidthRef = useRef(isSmallScreenWidth); | ||
|
||
isSmallScreenWidthRef.current = isSmallScreenWidth; | ||
|
||
const {navigation, state, descriptors, NavigationContent} = useNavigationBuilder< | ||
StackNavigationState<ParamListBase>, | ||
ResponsiveStackNavigatorRouterOptions, | ||
StackActionHelpers<ParamListBase>, | ||
StackNavigationOptions, | ||
StackNavigationEventMap | ||
>(CustomFullScreenRouter, { | ||
children: props.children, | ||
screenOptions: props.screenOptions, | ||
initialRouteName: props.initialRouteName, | ||
}); | ||
|
||
return ( | ||
<NavigationContent> | ||
<StackView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
state={state} | ||
descriptors={descriptors} | ||
navigation={navigation} | ||
/> | ||
</NavigationContent> | ||
); | ||
} | ||
|
||
ResponsiveStackNavigator.displayName = 'ResponsiveStackNavigator'; | ||
|
||
export default createNavigatorFactory<StackNavigationState<ParamListBase>, StackNavigationOptions, StackNavigationEventMap, typeof ResponsiveStackNavigator>(ResponsiveStackNavigator); |
75 changes: 75 additions & 0 deletions
75
src/libs/Navigation/AppNavigator/createCustomFullScreenNavigator/index.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,75 @@ | ||
import {createNavigatorFactory, ParamListBase, StackActionHelpers, StackNavigationState, useNavigationBuilder} from '@react-navigation/native'; | ||
import {StackNavigationEventMap, StackNavigationOptions, StackView} from '@react-navigation/stack'; | ||
import React, {useMemo, useRef} from 'react'; | ||
import useWindowDimensions from '@hooks/useWindowDimensions'; | ||
import SCREENS from '@src/SCREENS'; | ||
import CustomFullScreenRouter from './CustomFullScreenRouter'; | ||
import type {ResponsiveStackNavigatorProps, ResponsiveStackNavigatorRouterOptions} from './types'; | ||
|
||
// TODO: Extract to utils with ./createCustomStackNavigator/index.tsx | ||
type Routes = StackNavigationState<ParamListBase>['routes']; | ||
function reduceReportRoutes(routes: Routes): Routes { | ||
const result: Routes = []; | ||
let count = 0; | ||
const reverseRoutes = [...routes].reverse(); | ||
|
||
reverseRoutes.forEach((route) => { | ||
if (route.name === SCREENS.SETTINGS_CENTRAL_PANE) { | ||
// Remove all report routes except the last 3. This will improve performance. | ||
if (count < 3) { | ||
result.push(route); | ||
count++; | ||
} | ||
} else { | ||
result.push(route); | ||
} | ||
}); | ||
|
||
return result.reverse(); | ||
} | ||
|
||
function ResponsiveStackNavigator(props: ResponsiveStackNavigatorProps) { | ||
const {isSmallScreenWidth} = useWindowDimensions(); | ||
|
||
const isSmallScreenWidthRef = useRef<boolean>(isSmallScreenWidth); | ||
|
||
isSmallScreenWidthRef.current = isSmallScreenWidth; | ||
|
||
const {navigation, state, descriptors, NavigationContent} = useNavigationBuilder< | ||
StackNavigationState<ParamListBase>, | ||
ResponsiveStackNavigatorRouterOptions, | ||
StackActionHelpers<ParamListBase>, | ||
StackNavigationOptions, | ||
StackNavigationEventMap | ||
>(CustomFullScreenRouter, { | ||
children: props.children, | ||
screenOptions: props.screenOptions, | ||
initialRouteName: props.initialRouteName, | ||
}); | ||
|
||
const stateToRender = useMemo(() => { | ||
const result = reduceReportRoutes(state.routes); | ||
|
||
return { | ||
...state, | ||
index: result.length - 1, | ||
routes: [...result], | ||
}; | ||
}, [state]); | ||
|
||
return ( | ||
<NavigationContent> | ||
<StackView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
state={stateToRender} | ||
descriptors={descriptors} | ||
navigation={navigation} | ||
/> | ||
</NavigationContent> | ||
); | ||
} | ||
|
||
ResponsiveStackNavigator.displayName = 'ResponsiveStackNavigator'; | ||
|
||
export default createNavigatorFactory<StackNavigationState<ParamListBase>, StackNavigationOptions, StackNavigationEventMap, typeof ResponsiveStackNavigator>(ResponsiveStackNavigator); |
13 changes: 13 additions & 0 deletions
13
src/libs/Navigation/AppNavigator/createCustomFullScreenNavigator/types.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,13 @@ | ||
import {DefaultNavigatorOptions, ParamListBase, StackNavigationState, StackRouterOptions} from '@react-navigation/native'; | ||
import {StackNavigationEventMap, StackNavigationOptions} from '@react-navigation/stack'; | ||
|
||
type ResponsiveStackNavigatorConfig = { | ||
isSmallScreenWidth: boolean; | ||
}; | ||
|
||
type ResponsiveStackNavigatorRouterOptions = StackRouterOptions; | ||
|
||
type ResponsiveStackNavigatorProps = DefaultNavigatorOptions<ParamListBase, StackNavigationState<ParamListBase>, StackNavigationOptions, StackNavigationEventMap> & | ||
ResponsiveStackNavigatorConfig; | ||
|
||
export type {ResponsiveStackNavigatorRouterOptions, ResponsiveStackNavigatorProps, ResponsiveStackNavigatorConfig}; |
78 changes: 35 additions & 43 deletions
78
src/libs/Navigation/AppNavigator/createCustomStackNavigator/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 |
---|---|---|
@@ -1,50 +1,42 @@ | ||
import {createNavigatorFactory, ParamListBase, RouterFactory, StackActionHelpers, StackNavigationState, useNavigationBuilder} from '@react-navigation/native'; | ||
import {createNavigatorFactory, ParamListBase, StackActionHelpers, StackNavigationState, useNavigationBuilder} from '@react-navigation/native'; | ||
import {StackNavigationEventMap, StackNavigationOptions, StackView} from '@react-navigation/stack'; | ||
import React, {useRef} from 'react'; | ||
import useWindowDimensions from '@hooks/useWindowDimensions'; | ||
import CustomRouter from './CustomRouter'; | ||
import type {ResponsiveStackNavigatorProps, ResponsiveStackNavigatorRouterOptions} from './types'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type CustomRouterFactory<ParamList extends ParamListBase> = RouterFactory<StackNavigationState<ParamList>, any, ResponsiveStackNavigatorRouterOptions>; | ||
|
||
function ResponsiveStackNavigatorFactory<ParamList extends ParamListBase>(customRouter: CustomRouterFactory<ParamList>) { | ||
function ResponsiveStackNavigator(props: ResponsiveStackNavigatorProps) { | ||
const {isSmallScreenWidth} = useWindowDimensions(); | ||
|
||
const isSmallScreenWidthRef = useRef(isSmallScreenWidth); | ||
|
||
isSmallScreenWidthRef.current = isSmallScreenWidth; | ||
|
||
const {navigation, state, descriptors, NavigationContent} = useNavigationBuilder< | ||
StackNavigationState<ParamListBase>, | ||
ResponsiveStackNavigatorRouterOptions, | ||
StackActionHelpers<ParamListBase>, | ||
StackNavigationOptions, | ||
StackNavigationEventMap | ||
>(customRouter, { | ||
children: props.children, | ||
screenOptions: props.screenOptions, | ||
initialRouteName: props.initialRouteName, | ||
}); | ||
|
||
return ( | ||
<NavigationContent> | ||
<StackView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
state={state} | ||
descriptors={descriptors} | ||
navigation={navigation} | ||
/> | ||
</NavigationContent> | ||
); | ||
} | ||
|
||
ResponsiveStackNavigator.displayName = 'ResponsiveStackNavigator'; | ||
return ResponsiveStackNavigator; | ||
function ResponsiveStackNavigator(props: ResponsiveStackNavigatorProps) { | ||
const {isSmallScreenWidth} = useWindowDimensions(); | ||
|
||
const isSmallScreenWidthRef = useRef(isSmallScreenWidth); | ||
|
||
isSmallScreenWidthRef.current = isSmallScreenWidth; | ||
|
||
const {navigation, state, descriptors, NavigationContent} = useNavigationBuilder< | ||
StackNavigationState<ParamListBase>, | ||
ResponsiveStackNavigatorRouterOptions, | ||
StackActionHelpers<ParamListBase>, | ||
StackNavigationOptions, | ||
StackNavigationEventMap | ||
>(CustomRouter, { | ||
children: props.children, | ||
screenOptions: props.screenOptions, | ||
initialRouteName: props.initialRouteName, | ||
}); | ||
|
||
return ( | ||
<NavigationContent> | ||
<StackView | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
state={state} | ||
descriptors={descriptors} | ||
navigation={navigation} | ||
/> | ||
</NavigationContent> | ||
); | ||
} | ||
|
||
export default <ParamList extends ParamListBase>(customRouter: CustomRouterFactory<ParamList>) => | ||
createNavigatorFactory<StackNavigationState<ParamList>, StackNavigationOptions, StackNavigationEventMap, (props: ResponsiveStackNavigatorProps) => React.JSX.Element>( | ||
ResponsiveStackNavigatorFactory(customRouter), | ||
)(); | ||
ResponsiveStackNavigator.displayName = 'ResponsiveStackNavigator'; | ||
|
||
export default createNavigatorFactory<StackNavigationState<ParamListBase>, StackNavigationOptions, StackNavigationEventMap, typeof ResponsiveStackNavigator>(ResponsiveStackNavigator); |
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