diff --git a/patches/react-native-screens/details.md b/patches/react-native-screens/details.md index 16fdf13a141b..e053770b6b88 100644 --- a/patches/react-native-screens/details.md +++ b/patches/react-native-screens/details.md @@ -90,3 +90,10 @@ - Upstream PR/issue: not reported, because dropping the tint for every icon fits only a bar whose icons all arrive pre-colored, as the App's do. The disabled and focused icon colors go with it, which the App does not use. - E/App issue: [#101169](https://github.com/Expensify/App/issues/101169) - PR introducing patch: [#101339](https://github.com/Expensify/App/pull/101339) + +### [react-native-screens+4.28.0+005+hidden-android-tab-bar-touches.patch](react-native-screens+4.28.0+005+hidden-android-tab-bar-touches.patch) + +- Reason: On Android, a hidden native tab bar is set to `View.GONE`. `TabsContainer` is a `FrameLayout`, which skips `GONE` children during layout, so the bar keeps the bounds it had while visible. React Native's `TouchTargetHelper` walks native children without checking visibility, so a touch inside those stale bounds targets the hidden bar instead of the React views drawn there. On a chat, that swallowed taps on the composer's emoji and + buttons while the keyboard was closed. The touch target search skips a child whose transform is non-invertible, so the patch scales the hidden bar to zero on the Y axis and back to one when it is shown. Its layout is left alone, so the bar's height and the insets derived from it do not change. +- Upstream PR/issue: TBD +- E/App issue: [#104226](https://github.com/Expensify/App/issues/104226) +- PR introducing patch: [#104238](https://github.com/Expensify/App/pull/104238) diff --git a/patches/react-native-screens/react-native-screens+4.28.0+005+hidden-android-tab-bar-touches.patch b/patches/react-native-screens/react-native-screens+4.28.0+005+hidden-android-tab-bar-touches.patch new file mode 100644 index 000000000000..9eea6a155402 --- /dev/null +++ b/patches/react-native-screens/react-native-screens+4.28.0+005+hidden-android-tab-bar-touches.patch @@ -0,0 +1,14 @@ +diff --git a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/gamma/tabs/appearance/TabsAppearanceApplicator.kt b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/gamma/tabs/appearance/TabsAppearanceApplicator.kt +--- a/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/gamma/tabs/appearance/TabsAppearanceApplicator.kt ++++ b/node_modules/react-native-screens/android/src/main/java/com/swmansion/rnscreens/gamma/tabs/appearance/TabsAppearanceApplicator.kt +@@ -36,6 +36,10 @@ + isTabBarHidden: Boolean, + ) { + bottomNavigationView.isVisible = !isTabBarHidden ++ // A GONE child keeps the bounds it had while visible, and React Native's TouchTargetHelper does not check ++ // visibility, so the hidden bar would take touches meant for the React views drawn there. The touch target search ++ // skips a child with a non-invertible transform, so the hidden bar is scaled to zero, which leaves its layout as is. ++ bottomNavigationView.scaleY = if (isTabBarHidden) 0f else 1f + bottomNavigationView.setBackgroundColor( + tabBarAppearance?.tabBarBackgroundColor + ?: resolveColorAttr(context, R.attr.colorSurfaceContainer), diff --git a/patches/react-navigation/@react-navigation+bottom-tabs+7.19.2+004+stable-native-tab-identity.patch b/patches/react-navigation/@react-navigation+bottom-tabs+7.19.2+004+stable-native-tab-identity.patch new file mode 100644 index 000000000000..71f8fa440f21 --- /dev/null +++ b/patches/react-navigation/@react-navigation+bottom-tabs+7.19.2+004+stable-native-tab-identity.patch @@ -0,0 +1,88 @@ +diff --git a/node_modules/@react-navigation/bottom-tabs/lib/module/unstable/NativeBottomTabView.native.js b/node_modules/@react-navigation/bottom-tabs/lib/module/unstable/NativeBottomTabView.native.js +index 5a152a3d182..d2d69feda98 100644 +--- a/node_modules/@react-navigation/bottom-tabs/lib/module/unstable/NativeBottomTabView.native.js ++++ b/node_modules/@react-navigation/bottom-tabs/lib/module/unstable/NativeBottomTabView.native.js +@@ -39,6 +39,10 @@ export function NativeBottomTabView({ + // Derived from the history rather than remembered, so a route that became hidden since it was focused is skipped. + const isRouteKeyVisible = key => state.routes.some(route => route.key === key && !isRouteHidden(route)); + const nativeSelectedRouteKey = isFocusedRouteHidden ? state.history.findLast(entry => isRouteKeyVisible(entry.key))?.key ?? state.routes.find(route => !isRouteHidden(route))?.key ?? focusedRouteKey : focusedRouteKey; ++ // Native tabs are identified by route name, which survives a route getting a new key. Keyed by route key, a re-keyed ++ // route replaces its native tab, and Android never removes the old one, so the bar shows the tab twice. ++ const getRouteForScreenKey = screenKey => state.routes.find(route => route.name === screenKey); ++ const nativeSelectedScreenKey = state.routes.find(route => route.key === nativeSelectedRouteKey)?.name ?? nativeSelectedRouteKey; + // Hidden routes stay mounted once visited, the same as the native tabs. + const visitedHiddenRouteKeysRef = React.useRef(new Set()); + if (isFocusedRouteHidden) { +@@ -46,7 +50,7 @@ export function NativeBottomTabView({ + } + const [nativeState, dispatch] = React.useReducer(reducer, { + confirmed: { +- routeKey: nativeSelectedRouteKey, ++ routeKey: nativeSelectedScreenKey, + provenance: 0 + } + }); +@@ -89,7 +93,7 @@ export function NativeBottomTabView({ + routeKey: selectedScreenKey, + provenance + }; +- const route = state.routes.find(route => route.key === selectedScreenKey); ++ const route = getRouteForScreenKey(selectedScreenKey); + if (!route) { + console.error(`Received 'tabSelected' for route that doesn't exist: ${selectedScreenKey}`); + return; +@@ -121,7 +125,7 @@ export function NativeBottomTabView({ + routeKey: selectedScreenKey, + provenance + }; +- const route = state.routes.find(route => route.key === selectedScreenKey); ++ const route = getRouteForScreenKey(selectedScreenKey); + if (!route) { + console.error(`Received 'tabSelectionRejected' for route that doesn't exist: ${selectedScreenKey}`); + return; +@@ -150,7 +154,7 @@ export function NativeBottomTabView({ + }); + navigation.emit({ + type: 'tabPress', +- target: preventedScreenKey ++ target: getRouteForScreenKey(preventedScreenKey)?.key ?? preventedScreenKey + }); + }; + const currentOptions = descriptors[state.routes[state.index].key]?.options; +@@ -204,7 +208,7 @@ export function NativeBottomTabView({ + return /*#__PURE__*/_jsxs(SafeAreaProviderCompat, { + children: [/*#__PURE__*/_jsx(Tabs.Host, { + navStateRequest: { +- selectedScreenKey: nativeSelectedRouteKey, ++ selectedScreenKey: nativeSelectedScreenKey, + baseProvenance: nativeState.confirmed.provenance + }, + rejectStaleNavStateUpdates: true, +@@ -302,7 +306,7 @@ export function NativeBottomTabView({ + closing: true, + route + }), +- screenKey: route.key, ++ screenKey: route.name, + preventNativeSelection: tabBarSelectionEnabled === false, + badgeValue: tabBarBadge?.toString(), + title: tabTitle, +@@ -370,6 +374,7 @@ export function NativeBottomTabView({ + }, + overrideScrollViewContentInsetAdjustmentBehavior + }, ++ // Keyed by route key, so a re-keyed route still remounts its content inside the same native tab. + children: /*#__PURE__*/_jsx(Deferred, { + lazy: lazy, + visible: isFocused || isPreloaded, +@@ -383,8 +388,8 @@ export function NativeBottomTabView({ + children: render() + }) + }) +- }) +- }, route.key); ++ }, route.key) ++ }, route.name); + }) + }, "native-tabs-host"), ...state.routes.map((route, index) => { + if (!isRouteHidden(route) || !visitedHiddenRouteKeysRef.current.has(route.key)) { diff --git a/patches/react-navigation/details.md b/patches/react-navigation/details.md index 6a91d61be2f1..5767e72179a1 100644 --- a/patches/react-navigation/details.md +++ b/patches/react-navigation/details.md @@ -78,6 +78,14 @@ - PR Introducing Patch: [#101339](https://github.com/Expensify/App/pull/101339) - PR Updating Patch: N/A +### [@react-navigation+bottom-tabs+7.19.2+004+stable-native-tab-identity.patch](@react-navigation+bottom-tabs+7.19.2+004+stable-native-tab-identity.patch) + +- Reason: Identifies each native tab by its route name instead of its route key. The App gives the focused tab route a new key on purpose when it pre-inserts a screen under the RHP (`markFocusedTabRouteForRemount` in `GetStateForActionHandlers.ts`, #90985), so the split navigator remounts without a push-transition flash. Keyed by route key, that new key replaced the whole `Tabs.Screen`. Android never removes the old one: `TabsHostViewManager` does not override `getChildAt`/`getChildCount`, so Fabric's `removeViewAt` finds no child at that index ("view already removed from parent!") and skips `TabsHost.unmountReactSubviewAt`, while the insert goes through. The bar then shows the tab twice, and the stale copy opens a blank page. With this patch the native tab (`key`, `screenKey`, `navStateRequest.selectedScreenKey` and the selection events) uses the route name, which stays the same, and only the tab's content is keyed by the route key, so a re-keyed route still remounts its content inside the same native tab. **Remove this patch** once react-native-screens removes Android tabs correctly and React Navigation's native tab view no longer recreates a tab when its route key changes. +- Upstream PR/issue: not reported yet. The Android removal bug belongs to react-native-screens (`TabsHostViewManager`). +- E/App issue: [#104230](https://github.com/Expensify/App/issues/104230), [#104233](https://github.com/Expensify/App/issues/104233) +- PR Introducing Patch: [#104238](https://github.com/Expensify/App/pull/104238) +- PR Updating Patch: N/A + ### Removed patches - `@react-navigation+native+7.1.33+003+increase-history-go-popstate-fallback-timeout.patch` was dropped: upstream raised the `createMemoryHistory.go()` fallback timeout from 100ms to 1000ms in 7.3.18 (https://github.com/react-navigation/react-navigation/pull/13217), with the same Firefox rationale as our patch. Original context: [#94571](https://github.com/Expensify/App/issues/94571) / [#95980](https://github.com/Expensify/App/pull/95980), upstream issue https://github.com/react-navigation/react-navigation/issues/11145. diff --git a/tests/unit/NativeBottomTabViewTest.tsx b/tests/unit/NativeBottomTabViewTest.tsx new file mode 100644 index 000000000000..d8e985001132 --- /dev/null +++ b/tests/unit/NativeBottomTabViewTest.tsx @@ -0,0 +1,144 @@ +import {act, render} from '@testing-library/react-native'; + +import type {ParamListBase, TabNavigationState} from '@react-navigation/native'; + +import {NativeBottomTabView} from '@react-navigation/bottom-tabs/unstable'; +import {DefaultTheme, ThemeProvider} from '@react-navigation/native'; +import React, {useEffect} from 'react'; + +type NativeBottomTabViewProps = Parameters[0]; + +type MockTabsHostProps = { + navStateRequest: {selectedScreenKey: string}; + onTabSelected: (event: {nativeEvent: {selectedScreenKey: string; provenance: number; actionOrigin: string}}) => void; + children: React.ReactNode; +}; + +type MockTabsScreenProps = { + screenKey: string; + children: React.ReactNode; +}; + +// The published types describe the web stub, which renders nothing. Jest resolves the native view. +// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion +const NativeTabView = NativeBottomTabView as unknown as React.ComponentType; + +// Each mounted native tab and each mounted tab content, in mount order +const mockNativeTabMounts: string[] = []; +const contentMounts: string[] = []; +let mockLatestHostProps: MockTabsHostProps | undefined; + +jest.mock('react-native-screens', () => { + const {useEffect: mockUseEffect} = jest.requireActual('react'); + + function Host(props: MockTabsHostProps) { + mockLatestHostProps = props; + return props.children; + } + + function Screen({screenKey, children}: MockTabsScreenProps) { + mockUseEffect(() => { + mockNativeTabMounts.push(screenKey); + }, []); + return children; + } + + return {Tabs: {Host, Screen}}; +}); + +function TabContent({routeKey}: {routeKey: string}) { + useEffect(() => { + contentMounts.push(routeKey); + }, [routeKey]); + return null; +} + +function buildTabState(inboxKey: string): TabNavigationState { + return { + type: 'tab', + key: 'tab', + index: 0, + stale: false, + routeNames: ['Inbox', 'Spend'], + routes: [ + {name: 'Inbox', key: inboxKey}, + {name: 'Spend', key: 'Spend-key'}, + ], + history: [{type: 'route', key: inboxKey}], + preloadedRouteKeys: [], + }; +} + +function buildNavigation() { + return {dispatch: jest.fn(), emit: jest.fn(), getState: jest.fn()}; +} + +function buildDescriptors(state: TabNavigationState): NativeBottomTabViewProps['descriptors'] { + const descriptors = Object.fromEntries( + state.routes.map((route) => [ + route.key, + { + route, + options: {}, + navigation: {}, + render: () => , + }, + ]), + ); + // The view reads only the fields built above. + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion + return descriptors as unknown as NativeBottomTabViewProps['descriptors']; +} + +function renderTabView(state: TabNavigationState, navigation: ReturnType) { + return ( + + + + ); +} + +describe('NativeBottomTabView', () => { + beforeEach(() => { + mockNativeTabMounts.length = 0; + contentMounts.length = 0; + mockLatestHostProps = undefined; + }); + + it('keeps the native tab when the focused route gets a new key, and remounts only its content', () => { + // Given the Inbox tab focused under a native tab bar + const navigation = buildNavigation(); + const {rerender} = render(renderTabView(buildTabState('Inbox-key-1'), navigation)); + + // When the Inbox route gets a new key, as the pre-insert under the RHP does to remount it without a push flash (#90985) + rerender(renderTabView(buildTabState('Inbox-key-2'), navigation)); + + // Then no native tab is mounted again, since Android never removes the replaced one and would show Inbox twice + expect(mockNativeTabMounts).toEqual(['Inbox', 'Spend']); + // And the Inbox content still remounts under its new key, so the remount the new key asks for still happens + expect(contentMounts.filter((routeKey) => routeKey.startsWith('Inbox'))).toEqual(['Inbox-key-1', 'Inbox-key-2']); + // And native is asked to select the tab by its route name, which did not change + expect(mockLatestHostProps?.navStateRequest.selectedScreenKey).toBe('Inbox'); + }); + + it('switches to the route whose name native reports as selected', () => { + // Given the Inbox tab focused under a native tab bar + const navigation = buildNavigation(); + render(renderTabView(buildTabState('Inbox-key-1'), navigation)); + + // When the user selects the Spend tab natively, which reports the tab by its route name + act(() => { + mockLatestHostProps?.onTabSelected({nativeEvent: {selectedScreenKey: 'Spend', provenance: 1, actionOrigin: 'user'}}); + }); + + // Then JS presses and navigates to that route, so JS follows native instead of dropping the selection + expect(navigation.emit).toHaveBeenCalledWith({type: 'tabPress', target: 'Spend-key'}); + expect(navigation.dispatch).toHaveBeenCalledWith(expect.objectContaining({type: 'NAVIGATE', payload: expect.objectContaining({name: 'Spend'}), target: 'tab'})); + }); +});