Skip to content

Lesson 15 ยท Components and Styling

Themes, Dark Mode and Custom Fonts

Support light and dark mode in Expo with useColorScheme and a theme context, load custom fonts with expo-font and expo-splash-screen, and manage the status bar.

A polished app respects the user's light or dark preference, uses consistent colors and spacing, and shows its own typeface instead of the system default. In Expo this takes three pieces: a color-scheme hook, a small theme object, and font loading gated by the splash screen.

Code that touches the device is read-only here; live stand-ins model the theme resolution and the font-loading flow.

Enable automatic dark mode

In app.json set "userInterfaceStyle": "automatic" (the template does). Without it, Android and iOS keep the app in light mode regardless of the system. Rebuild the native app after changing it (a development or production build; Expo Go already supports it).

Reading the color scheme

TSX
import { useColorScheme } from 'react-native';

export function Screen() {
  const scheme = useColorScheme();          // 'light' | 'dark' | null
  const isDark = scheme === 'dark';
  return <View style={{ flex: 1, backgroundColor: isDark ? '#0d1117' : '#ffffff' }} />;
}

The hook re-renders your component when the user switches system appearance. Appearance.setColorScheme('dark' | 'light' | null) lets the app override the system (pass null to follow it again), which is how an in-app "Theme: System / Light / Dark" setting is built.

A theme object

Hard-coding colors in components does not scale. Define semantic tokens (names for roles, not shades) once:

TSX
// constants/theme.ts
export const palette = {
  light: { background: '#ffffff', surface: '#f2f4f8', text: '#111827', muted: '#6b7280', primary: '#0b5fff', border: '#d9dee7' },
  dark:  { background: '#0d1117', surface: '#161b22', text: '#e6edf3', muted: '#8b949e', primary: '#58a6ff', border: '#30363d' },
};
export const spacing = { xs: 4, sm: 8, md: 16, lg: 24, xl: 32 };
export const radius = { sm: 6, md: 12, pill: 999 };
TSX
// hooks/useTheme.ts
import { useColorScheme } from 'react-native';
import { palette, spacing, radius } from '@/constants/theme';

export function useTheme() {
  const scheme = useColorScheme() ?? 'light';
  return { colors: palette[scheme], spacing, radius, isDark: scheme === 'dark' };
}
TSX
function Card({ children }: { children: React.ReactNode }) {
  const { colors, spacing, radius } = useTheme();
  return (
    <View style={{ backgroundColor: colors.surface, padding: spacing.md, borderRadius: radius.md, borderColor: colors.border, borderWidth: 1 }}>
      {children}
    </View>
  );
}

Components say colors.surface, never #f2f4f8. Changing the look of the entire app is then one edit.

JS Resolve a theme with a user override

Press Run (or Ctrl + Enter) to see the console output here.

No output.

To remember the user's choice across launches, store it with AsyncStorage or expo-secure-store (Data, Storage and Networking). React Navigation's own components (headers, tab bars) take a theme through ThemeProvider from @react-navigation/native (DarkTheme / DefaultTheme); wrap your root layout so navigation chrome matches your colors.

TSX
import { ThemeProvider, DarkTheme, DefaultTheme } from '@react-navigation/native';

<ThemeProvider value={scheme === 'dark' ? DarkTheme : DefaultTheme}>
  <Stack />
</ThemeProvider>

Contrast and accessibility

Dark mode is not "invert the colors". Avoid pure black on pure white glare, check contrast (text should reach at least 4.5:1 against its background, 3:1 for large text), and test both modes for every screen. The stand-in computes the WCAG contrast ratio.

JS Check color contrast

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The status bar

expo-status-bar sets the color of the system status bar text so it stays readable:

TSX
import { StatusBar } from 'expo-status-bar';
<StatusBar style="auto" />     // 'auto' follows the theme; or 'light' / 'dark'

Custom fonts

Install expo-font and load fonts with useFonts. The simplest, build-time approach uses the config plugin, which embeds the files in the native app (needs a development build):

JSON
{
  "expo": {
    "plugins": [
      ["expo-font", { "fonts": ["./assets/fonts/Inter-Regular.ttf", "./assets/fonts/Inter-Bold.ttf"] }]
    ]
  }
}

Or load at runtime (works in Expo Go):

TSX
// app/_layout.tsx
import { useFonts } from 'expo-font';
import * as SplashScreen from 'expo-splash-screen';
import { useEffect } from 'react';
import { Stack } from 'expo-router';

SplashScreen.preventAutoHideAsync();          // keep the splash up while we load

export default function RootLayout() {
  const [loaded, error] = useFonts({
    'Inter-Regular': require('../assets/fonts/Inter-Regular.ttf'),
    'Inter-Bold': require('../assets/fonts/Inter-Bold.ttf'),
  });

  useEffect(() => {
    if (loaded || error) SplashScreen.hideAsync();
  }, [loaded, error]);

  if (!loaded && !error) return null;
  return <Stack />;
}

Then use the name you registered: fontFamily: 'Inter-Bold'. Important facts: native platforms have no synthetic bold, so each weight is a separate font file and a separate fontFamily name; do not combine fontFamily: 'Inter-Regular' with fontWeight: '700' and expect bold. Google Fonts are available as ready packages (@expo-google-fonts/inter) with a matching useFonts import.

JS Gate rendering on font loading

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The splash screen

Configure the static splash (shown before JavaScript starts) with the expo-splash-screen plugin: image, backgroundColor, resizeMode, and a separate dark variant. Keep it simple, and hide it as soon as your first screen is ready; holding it longer than a second or two feels broken.

Common mistakes

  • Hard-coded colors scattered through components, so dark mode requires touching every file.
  • Forgetting userInterfaceStyle, so the app is always light.
  • Light text on a light card in dark mode because only the background was themed.
  • fontWeight: '700' with a custom regular font, giving no bold.
  • Not hiding the splash screen when font loading fails.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which hook returns 'light', 'dark' or null for the device appearance?

    useColorScheme from react-native follows the system setting and re-renders on change.
  2. 2. What is a semantic color token?

    Tokens describe purpose, so one name works in both light and dark palettes.
  3. 3. How do you show a bold weight of a custom font on native platforms?

    Native platforms do not fake bold for custom fonts; each weight is a file with its own family name.
  4. 4. Why call SplashScreen.preventAutoHideAsync()?

    Otherwise the splash disappears before your first real screen can render.

Summary

  • Set userInterfaceStyle: 'automatic', read useColorScheme, and optionally allow an in-app override with Appearance.setColorScheme.
  • Centralize colors, spacing and radius as tokens and theme navigation with ThemeProvider.
  • Load fonts with useFonts (or the expo-font plugin) and gate rendering with the splash screen.
  • Check contrast in both modes; each font weight is a separate file.

Next: Camera and Image Picker