Skip to main content

React Navigation

Theme React Navigation with Vortex Native tokens. Navigators paint their own headers, tab bars, and screen backgrounds, so pass them colors from the same theme as your components.

bun add @react-navigation/native @react-navigation/native-stack react-native-screens react-native-safe-area-context

In an Expo app without Expo Router, install them with bunx expo install instead. Expo Router already includes React Navigation; see Expo Router.

Create a navigation theme

Build a React Navigation theme from the current Vortex theme and color mode. Start from DefaultTheme or DarkTheme to keep their fonts:

src/useNavigationTheme.ts
import { DarkTheme, DefaultTheme, type Theme } from "@react-navigation/native";
import { useTheme } from "@vortexlabs/vortex-native";
import { useMemo } from "react";

export const useNavigationTheme = (): Theme => {
  const { theme, colorMode } = useTheme();

  return useMemo(() => {
    const base = colorMode === "dark" ? DarkTheme : DefaultTheme;
    const color = theme.color[colorMode];

    return {
      ...base,
      colors: {
        ...base.colors,
        primary: color.foregroundPrimaryDefault,
        background: color.backgroundDefault,
        card: color.backgroundDefault,
        text: color.foregroundDefault,
        border: color.borderDefault,
        notification: color.backgroundCriticalDefault,
      },
    };
  }, [theme, colorMode]);
};

Pass it to the navigator

Call the hook inside VortexProvider and pass the result to NavigationContainer. Set the status bar style there too, so it follows the same color mode:

App.tsx
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { useTheme, VortexProvider } from "@vortexlabs/vortex-native";
import { StatusBar } from "react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import HomeScreen from "./src/HomeScreen";
import SettingsScreen from "./src/SettingsScreen";
import acmeTheme from "./src/themes/acme/theme.native";
import { useNavigationTheme } from "./src/useNavigationTheme";

const RootStack = createNativeStackNavigator();

const Navigation = () => {
  const navigationTheme = useNavigationTheme();
  const { colorMode } = useTheme();

  return (
    <NavigationContainer theme={navigationTheme}>
      <StatusBar barStyle={colorMode === "dark" ? "light-content" : "dark-content"} />
      <RootStack.Navigator>
        <RootStack.Screen name="Home" component={HomeScreen} />
        <RootStack.Screen name="Settings" component={SettingsScreen} />
      </RootStack.Navigator>
    </NavigationContainer>
  );
};

const App = () => (
  <SafeAreaProvider>
    <VortexProvider theme={acmeTheme} defaultColorMode="system">
      <Navigation />
    </VortexProvider>
  </SafeAreaProvider>
);

export default App;

Headers and screen backgrounds now switch with the color mode, so screens don't need their own background surface.

Expo Router

Expo Router includes its own copy of React Navigation. Import DarkTheme, DefaultTheme, and the Theme type from expo-router in the hook above, since themes from @react-navigation/native don't reach Expo Router's navigators:

src/useNavigationTheme.ts
import { DarkTheme, DefaultTheme, type Theme } from "expo-router";

Pass the hook's result to ThemeProvider from expo-router in your root layout:

app/_layout.tsx
import { VortexProvider } from "@vortexlabs/vortex-native";
import { Stack, ThemeProvider } from "expo-router";
import acmeTheme from "../src/themes/acme/theme.native";
import { useNavigationTheme } from "../src/useNavigationTheme";

const RootNavigator = () => {
  const navigationTheme = useNavigationTheme();

  return (
    <ThemeProvider value={navigationTheme}>
      <Stack />
    </ThemeProvider>
  );
};

const RootLayout = () => (
  <VortexProvider theme={acmeTheme} defaultColorMode="system">
    <RootNavigator />
  </VortexProvider>
);

export default RootLayout;