Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: drop unmountOnBlur in favor of popToTopOnBlur #11896

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/bottom-tabs/src/types.tsx
Expand Up @@ -259,10 +259,10 @@ export type BottomTabNavigationOptions = HeaderOptions & {
headerShown?: boolean;

/**
* Whether this screen should be unmounted when navigating away from it.
* Whether any nested stack should be popped to top when navigating away from it.
* Defaults to `false`.
*/
unmountOnBlur?: boolean;
popToTopOnBlur?: boolean;

/**
* Whether inactive screens should be suspended from re-rendering. Defaults to `false`.
Expand Down
34 changes: 26 additions & 8 deletions packages/bottom-tabs/src/views/BottomTabView.tsx
Expand Up @@ -4,9 +4,10 @@ import {
SafeAreaProviderCompat,
Screen,
} from '@react-navigation/elements';
import type {
ParamListBase,
TabNavigationState,
import {
type ParamListBase,
StackActions,
type TabNavigationState,
} from '@react-navigation/native';
import * as React from 'react';
import { Animated, Platform, StyleSheet } from 'react-native';
Expand Down Expand Up @@ -137,6 +138,28 @@ export function BottomTabView(props: Props) {
animateToIndex();
}, [descriptors, focusedRouteKey, state.index, state.routes, tabAnims]);

// TODO: make sure this looks right of tab switching has animations
React.useEffect(() => {
const previousRouteKey = previousRouteKeyRef.current;
const popToTopOnBlur =
descriptors[previousRouteKey]?.options.popToTopOnBlur;

if (previousRouteKey !== focusedRouteKey && popToTopOnBlur) {
const prevRoute = state.routes.find(
(route) => route.key === previousRouteKey
);

if (prevRoute?.state?.type === 'stack' && prevRoute.state.key) {
navigation.dispatch({
...StackActions.popToTop(),
target: prevRoute.state.key,
});
}
}

previousRouteKeyRef.current = focusedRouteKey;
}, [descriptors, focusedRouteKey, navigation, state.routes]);

const dimensions = SafeAreaProviderCompat.initialMetrics.frame;
const [tabBarHeight, setTabBarHeight] = React.useState(() =>
getTabBarHeight({
Expand Down Expand Up @@ -205,17 +228,12 @@ export function BottomTabView(props: Props) {
const descriptor = descriptors[route.key];
const {
lazy = true,
unmountOnBlur,
animation = 'none',
sceneStyleInterpolator = NAMED_TRANSITIONS_PRESETS[animation]
.sceneStyleInterpolator,
} = descriptor.options;
const isFocused = state.index === index;

if (unmountOnBlur && !isFocused) {
return null;
}

if (
lazy &&
!loaded.includes(route.key) &&
Expand Down
4 changes: 2 additions & 2 deletions packages/drawer/src/types.tsx
Expand Up @@ -204,10 +204,10 @@ export type DrawerNavigationOptions = HeaderOptions & {
keyboardDismissMode?: 'on-drag' | 'none';

/**
* Whether this screen should be unmounted when navigating away from it.
* Whether any nested stack should be popped to top when navigating away from it.
* Defaults to `false`.
*/
unmountOnBlur?: boolean;
popToTopOnBlur?: boolean;

/**
* Whether inactive screens should be suspended from re-rendering. Defaults to `false`.
Expand Down
30 changes: 25 additions & 5 deletions packages/drawer/src/views/DrawerView.tsx
Expand Up @@ -10,6 +10,7 @@ import {
type DrawerNavigationState,
type DrawerStatus,
type ParamListBase,
StackActions,
useLocale,
useTheme,
} from '@react-navigation/native';
Expand Down Expand Up @@ -82,6 +83,29 @@ function DrawerViewBase({
setLoaded([...loaded, focusedRouteKey]);
}

const previousRouteKeyRef = React.useRef(focusedRouteKey);

React.useEffect(() => {
const previousRouteKey = previousRouteKeyRef.current;
const popToTopOnBlur =
descriptors[previousRouteKey]?.options.popToTopOnBlur;

if (previousRouteKey !== focusedRouteKey && popToTopOnBlur) {
const prevRoute = state.routes.find(
(route) => route.key === previousRouteKey
);

if (prevRoute?.state?.type === 'stack' && prevRoute.state.key) {
navigation.dispatch({
...StackActions.popToTop(),
target: prevRoute.state.key,
});
}
}

previousRouteKeyRef.current = focusedRouteKey;
}, [descriptors, focusedRouteKey, navigation, state.routes]);

const dimensions = useSafeAreaFrame();

const { colors } = useTheme();
Expand Down Expand Up @@ -194,13 +218,9 @@ function DrawerViewBase({
>
{state.routes.map((route, index) => {
const descriptor = descriptors[route.key];
const { lazy = true, unmountOnBlur } = descriptor.options;
const { lazy = true } = descriptor.options;
const isFocused = state.index === index;

if (unmountOnBlur && !isFocused) {
return null;
}

if (
lazy &&
!loaded.includes(route.key) &&
Expand Down