Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -90,7 +89,6 @@ function MultifactorAuthenticationModalNavigator() {
const {dispatch} = useMultifactorAuthenticationActions();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const theme = useTheme();
const themePreference = useThemePreference();
const styles = useThemeStyles();
const {translate} = useLocalize();

Expand All @@ -108,7 +106,7 @@ function MultifactorAuthenticationModalNavigator() {
setPhase('closing');
}

const navigationThemeBase = getNavigationBaseTheme(themePreference);
const navigationThemeBase = getNavigationBaseTheme(theme.colorScheme);
const navigationTheme = {
...navigationThemeBase,
colors: {
Expand Down
6 changes: 2 additions & 4 deletions src/libs/Navigation/NavigationRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
16 changes: 6 additions & 10 deletions src/libs/Navigation/getNavigationBaseTheme.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions tests/unit/NavigationRootInitialStateTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/NavigationThemeTest.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof ReactNavigation>('@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(
<NavigationRoot
authenticated
lastVisitedPath={ROUTES.HOME}
initialUrl={null}
onReady={() => {}}
/>,
);

// 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);
});
});
Loading