diff --git a/src/libs/Navigation/AppNavigator/Navigators/MultifactorAuthenticationModalNavigator.tsx b/src/libs/Navigation/AppNavigator/Navigators/MultifactorAuthenticationModalNavigator.tsx index 7f37a4cd94e1..e8f761de8acc 100644 --- a/src/libs/Navigation/AppNavigator/Navigators/MultifactorAuthenticationModalNavigator.tsx +++ b/src/libs/Navigation/AppNavigator/Navigators/MultifactorAuthenticationModalNavigator.tsx @@ -9,7 +9,6 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useSidePanelActions from '@hooks/useSidePanelActions'; import useSidePanelState from '@hooks/useSidePanelState'; import useTheme from '@hooks/useTheme'; -import useThemePreference from '@hooks/useThemePreference'; import useThemeStyles from '@hooks/useThemeStyles'; import getNavigationBaseTheme from '@libs/Navigation/getNavigationBaseTheme'; @@ -90,7 +89,6 @@ function MultifactorAuthenticationModalNavigator() { const {dispatch} = useMultifactorAuthenticationActions(); const {shouldUseNarrowLayout} = useResponsiveLayout(); const theme = useTheme(); - const themePreference = useThemePreference(); const styles = useThemeStyles(); const {translate} = useLocalize(); @@ -108,7 +106,7 @@ function MultifactorAuthenticationModalNavigator() { setPhase('closing'); } - const navigationThemeBase = getNavigationBaseTheme(themePreference); + const navigationThemeBase = getNavigationBaseTheme(theme.colorScheme); const navigationTheme = { ...navigationThemeBase, colors: { diff --git a/src/libs/Navigation/NavigationRoot.tsx b/src/libs/Navigation/NavigationRoot.tsx index 543b69655ea0..328acfc6efbd 100644 --- a/src/libs/Navigation/NavigationRoot.tsx +++ b/src/libs/Navigation/NavigationRoot.tsx @@ -5,7 +5,6 @@ import useOnyx from '@hooks/useOnyx'; import usePrevious from '@hooks/usePrevious'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; -import useThemePreference from '@hooks/useThemePreference'; import FS from '@libs/Fullstory'; import {buildPageViewedEvent, trackFullstoryEvent} from '@libs/Fullstory/utils'; @@ -137,7 +136,6 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N ]); const firstRenderRef = useRef(true); - const themePreference = useThemePreference(); const theme = useTheme(); const {cleanStaleScrollOffsets} = useContext(ScrollOffsetContext); @@ -191,7 +189,7 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N // https://reactnavigation.org/docs/themes const navigationTheme = useMemo(() => { - const defaultNavigationTheme = getNavigationBaseTheme(themePreference); + const defaultNavigationTheme = getNavigationBaseTheme(theme.colorScheme); return { ...defaultNavigationTheme, @@ -207,7 +205,7 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N background: shouldUseNarrowLayout ? theme.appBG : 'transparent', }, }; - }, [shouldUseNarrowLayout, theme.appBG, themePreference]); + }, [shouldUseNarrowLayout, theme.appBG, theme.colorScheme]); useEffect(() => { if (firstRenderRef.current) { diff --git a/src/libs/Navigation/getNavigationBaseTheme.ts b/src/libs/Navigation/getNavigationBaseTheme.ts index 2fbbb0845f1a..584f27a10700 100644 --- a/src/libs/Navigation/getNavigationBaseTheme.ts +++ b/src/libs/Navigation/getNavigationBaseTheme.ts @@ -1,24 +1,20 @@ -import type {ThemePreferenceWithoutSystem} from '@styles/theme/types'; -import {getBaseTheme} from '@styles/theme/utils'; +import type {ColorScheme} from '@styles/index'; import CONST from '@src/CONST'; import {DarkTheme, DefaultTheme} from '@react-navigation/native'; /** - * Resolves a user's theme preference to the matching react-navigation base theme. + * Resolves the app theme's color scheme to the matching react-navigation base theme. * - * `CONST.THEME` covers more than just `dark` / `light` — high-contrast and system - * variants exist as well. A naive `themePreference === CONST.THEME.DARK` check - * mis-buckets every non-DARK variant (e.g. `dark-contrast`) into the light base. - * Callers must go through `getBaseTheme()` so contrast and system variants - * resolve to their underlying DARK/LIGHT base first. + * Takes `theme.colorScheme` from `useTheme()` rather than the raw theme preference: the app theme switches after a + * debounce, and the native tab bar reads its color scheme from the navigation theme, so both must switch together. * * Use this helper anywhere a `NavigationContainer` / `BaseNavigationContainer` * needs a `theme` prop so the choice stays consistent across every navigator. */ -function getNavigationBaseTheme(themePreference: ThemePreferenceWithoutSystem): typeof DarkTheme | typeof DefaultTheme { - return getBaseTheme(themePreference) === CONST.THEME.DARK ? DarkTheme : DefaultTheme; +function getNavigationBaseTheme(colorScheme: ColorScheme): typeof DarkTheme | typeof DefaultTheme { + return colorScheme === CONST.COLOR_SCHEME.DARK ? DarkTheme : DefaultTheme; } export default getNavigationBaseTheme; diff --git a/tests/unit/NavigationRootInitialStateTest.tsx b/tests/unit/NavigationRootInitialStateTest.tsx index 58136a74f434..25a6036c6fce 100644 --- a/tests/unit/NavigationRootInitialStateTest.tsx +++ b/tests/unit/NavigationRootInitialStateTest.tsx @@ -33,8 +33,7 @@ jest.mock('@react-navigation/native', () => { jest.mock('@libs/Navigation/AppNavigator', () => () => null); jest.mock('@libs/NavigationFocusReturn', () => ({setupNavigationFocusReturn: jest.fn(), teardownNavigationFocusReturn: jest.fn()})); jest.mock('@src/libs/telemetry/useStartSpansOnRender', () => jest.fn()); -jest.mock('@hooks/useThemePreference', () => () => 'dark'); -jest.mock('@hooks/useTheme', () => () => ({appBG: '#000000'})); +jest.mock('@hooks/useTheme', () => () => ({appBG: '#000000', colorScheme: 'dark'})); jest.mock('@hooks/useResponsiveLayout', () => () => ({shouldUseNarrowLayout: true})); const DISTANCE_EDIT_ROUTE = ROUTES.MONEY_REQUEST_STEP_DISTANCE.getRoute(CONST.IOU.ACTION.EDIT, CONST.IOU.TYPE.SUBMIT, '1', '2'); diff --git a/tests/unit/NavigationThemeTest.tsx b/tests/unit/NavigationThemeTest.tsx new file mode 100644 index 000000000000..3a057707978c --- /dev/null +++ b/tests/unit/NavigationThemeTest.tsx @@ -0,0 +1,80 @@ +import {render} from '@testing-library/react-native'; + +import getNavigationBaseTheme from '@libs/Navigation/getNavigationBaseTheme'; +import NavigationRoot from '@libs/Navigation/NavigationRoot'; + +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import ROUTES from '@src/ROUTES'; + +import type * as ReactNavigation from '@react-navigation/native'; + +import {DarkTheme, DefaultTheme} from '@react-navigation/native'; +import React from 'react'; +import Onyx from 'react-native-onyx'; + +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +// The `mock` prefix lets the hoisted jest.mock factory capture the theme NavigationRoot passes down. +let mockCapturedTheme: ReactNavigation.Theme | undefined; + +jest.mock('@react-navigation/native', () => { + const actualNav = jest.requireActual('@react-navigation/native'); + return { + ...actualNav, + NavigationContainer: ({theme}: {theme?: ReactNavigation.Theme}) => { + mockCapturedTheme = theme; + return null; + }, + }; +}); +jest.mock('@libs/Navigation/AppNavigator', () => () => null); +jest.mock('@libs/NavigationFocusReturn', () => ({setupNavigationFocusReturn: jest.fn(), teardownNavigationFocusReturn: jest.fn()})); +jest.mock('@src/libs/telemetry/useStartSpansOnRender', () => jest.fn()); +jest.mock('@hooks/useThemePreference', () => () => 'light'); +jest.mock('@hooks/useTheme', () => () => ({appBG: '#000000', colorScheme: 'dark'})); +jest.mock('@hooks/useResponsiveLayout', () => () => ({shouldUseNarrowLayout: true})); + +describe('getNavigationBaseTheme', () => { + it('returns the dark base theme for the dark color scheme', () => { + // Given the app theme resolved to the dark color scheme + // When the navigation base theme is picked for it + // Then it is the dark one, so native navigation UI renders dark + expect(getNavigationBaseTheme(CONST.COLOR_SCHEME.DARK)).toBe(DarkTheme); + }); + + it('returns the default base theme for the light color scheme', () => { + // Given the app theme resolved to the light color scheme + // When the navigation base theme is picked for it + // Then it is the default light one + expect(getNavigationBaseTheme(CONST.COLOR_SCHEME.LIGHT)).toBe(DefaultTheme); + }); +}); + +describe('NavigationRoot theme', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(async () => { + mockCapturedTheme = undefined; + await Onyx.clear(); + await waitForBatchedUpdates(); + }); + + it('follows the app theme color scheme while the theme preference has already changed', () => { + // Given the theme preference already switched to light, but the debounced app theme is still dark + // When the navigation root renders + render( + {}} + />, + ); + + // Then the navigation theme stays dark, because the native tab bar reads its color scheme from it and must switch together with the app + expect(mockCapturedTheme?.dark).toBe(true); + }); +});