Themes
Themes allow you to change the colors and fonts of various components provided by React Navigation. You can use themes to:
- Customize the colors and fonts to match your brand
- Provide light and dark themes based on the time of the day or user preference
Basic usage
To pass a custom theme, you can pass the theme
prop to the navigation container.
- Static
- Dynamic
import * as React from 'react';
import {
useNavigation,
createStaticNavigation,
DefaultTheme,
} from '@react-navigation/native';
const MyTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: 'rgb(140, 201, 125)',
primary: 'rgb(255, 45, 85)',
},
};
const Navigation = createStaticNavigation(Drawer);
export default function App() {
return <Navigation theme={MyTheme} />;
}
import * as React from 'react';
import {
NavigationContainer,
DefaultTheme,
useNavigation,
} from '@react-navigation/native';
const MyTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: 'rgb(140, 201, 125)',
primary: 'rgb(255, 45, 85)',
},
};
export default function App() {
return (
<NavigationContainer theme={MyTheme}>
<Drawer.Navigator initialRouteName="Root">
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen
name="Root"
component={Root}
options={{ headerShown: false }}
/>
</Drawer.Navigator>
</NavigationContainer>
);
}
You can change the theme prop dynamically and all the components will automatically update to reflect the new theme. If you haven't provided a theme
prop, the default theme will be used.
Properties
A theme is a JS object containing a list of colors to use. It contains the following properties:
dark
(boolean
): Whether this is a dark theme or a light themecolors
(object
): Various colors used by react navigation components:primary
(string
): The primary color of the app used to tint various elements. Usually you'll want to use your brand color for this.background
(string
): The color of various backgrounds, such as the background color for the screens.card
(string
): The background color of card-like elements, such as headers, tab bars etc.text
(string
): The text color of various elements.border
(string
): The color of borders, e.g. header border, tab bar border etc.notification
(string
): The color of notifications and badge (e.g. badge in bottom tabs).
fonts
(object
): Various fonts used by react navigation components:regular
(object
): Style object for the primary font used in the app.medium
(object
): Style object for the semi-bold variant of the primary font.bold
(object
): Style object for the bold variant of the primary font.heavy
(object
): Style object for the extra-bold variant of the primary font.
The style objects for fonts contain the following properties:
fontFamily
(string
): The name of the font family (or font stack on Web) to use, e.g.Roboto
orHelvetica Neue
. The system fonts are used by default.fontWeight
(string
): The font weight to use. Valid values arenormal
,bold
,100
,200
,300
,400
,500
,600
,700
,800
,900
.
When creating a custom theme, you will need to provide all of these properties.
Example theme:
const WEB_FONT_STACK =
'system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"';
const MyTheme = {
dark: false,
colors: {
primary: 'rgb(255, 45, 85)',
background: 'rgb(242, 242, 242)',
card: 'rgb(255, 255, 255)',
text: 'rgb(28, 28, 30)',
border: 'rgb(199, 199, 204)',
notification: 'rgb(255, 69, 58)',
},
fonts: Platform.select({
web: {
regular: {
fontFamily: WEB_FONT_STACK,
fontWeight: '400',
},
medium: {
fontFamily: WEB_FONT_STACK,
fontWeight: '500',
},
bold: {
fontFamily: WEB_FONT_STACK,
fontWeight: '600',
},
heavy: {
fontFamily: WEB_FONT_STACK,
fontWeight: '700',
},
},
ios: {
regular: {
fontFamily: 'System',
fontWeight: '400',
},
medium: {
fontFamily: 'System',
fontWeight: '500',
},
bold: {
fontFamily: 'System',
fontWeight: '600',
},
heavy: {
fontFamily: 'System',
fontWeight: '700',
},
},
default: {
regular: {
fontFamily: 'sans-serif',
fontWeight: 'normal',
},
medium: {
fontFamily: 'sans-serif-medium',
fontWeight: 'normal',
},
bold: {
fontFamily: 'sans-serif',
fontWeight: '600',
},
heavy: {
fontFamily: 'sans-serif',
fontWeight: '700',
},
},
}),
};
Providing a theme will take care of styling of all the official navigators. React Navigation also provides several tools to help you make your customizations of those navigators and the screens within the navigators can use the theme too.
Built-in themes
As operating systems add built-in support for light and dark modes, supporting dark mode is less about keeping hip to trends and more about conforming to the average user expectations for how apps should work. In order to provide support for light and dark mode in a way that is reasonably consistent with the OS defaults, these themes are built in to React Navigation.
You can import the default and dark themes like so:
import { DefaultTheme, DarkTheme } from '@react-navigation/native';
Keeping the native theme in sync
If you're changing the theme in the app, native UI elements such as Alert, ActionSheet etc. won't reflect the new theme. You can do the following to keep the native theme in sync:
React.useEffect(() => {
const colorScheme = theme.dark ? 'dark' : 'light';
if (Platform.OS === 'web') {
document.documentElement.style.colorScheme = colorScheme;
} else {
Appearance.setColorScheme(colorScheme);
}
}, [theme.dark]);
Alternatively, you can use the useColorScheme
hook to get the current native color scheme and update the theme accordingly.
Using the operating system preferences
On iOS 13+ and Android 10+, you can get user's preferred color scheme ('dark'
or 'light'
) with the (useColorScheme
hook).
- Static
- Dynamic
import {
useNavigation,
createStaticNavigation,
DefaultTheme,
DarkTheme,
useTheme,
} from '@react-navigation/native';
import { View, Text, TouchableOpacity, useColorScheme } from 'react-native';
const Navigation = createStaticNavigation(Drawer);
export default function App() {
const scheme = useColorScheme();
return <Navigation theme={scheme === 'dark' ? DarkTheme : DefaultTheme} />;
}
import { View, Text, TouchableOpacity, useColorScheme } from 'react-native';
import {
NavigationContainer,
DefaultTheme,
DarkTheme,
useTheme,
} from '@react-navigation/native';
export default function App() {
const scheme = useColorScheme();
return (
<NavigationContainer theme={scheme === 'dark' ? DarkTheme : DefaultTheme}>
<Drawer.Navigator>
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen
name="Root"
component={Root}
options={{ headerShown: false }}
/>
</Drawer.Navigator>
</NavigationContainer>
);
}
Using the current theme in your own components
To gain access to the theme in any component that is rendered inside the navigation container:, you can use the useTheme
hook. It returns the theme object:
- Static
- Dynamic
import {
useNavigation,
createStaticNavigation,
DefaultTheme,
DarkTheme,
useTheme,
} from '@react-navigation/native';
import { View, Text, TouchableOpacity, useColorScheme } from 'react-native';
function MyButton() {
const { colors } = useTheme();
return (
<TouchableOpacity style={{ backgroundColor: colors.card }}>
<Text style={{ color: colors.text }}>Button!</Text>
</TouchableOpacity>
);
}
import { View, Text, TouchableOpacity, useColorScheme } from 'react-native';
import {
NavigationContainer,
DefaultTheme,
DarkTheme,
useTheme,
useNavigation,
} from '@react-navigation/native';
function MyButton() {
const { colors } = useTheme();
return (
<TouchableOpacity style={{ backgroundColor: colors.card }}>
<Text style={{ color: colors.text }}>Button!</Text>
</TouchableOpacity>
);
}