Skip to main content

useTheme

useTheme is a hook that reads the theme and changes the color mode, such as styling a custom view or adding a dark-mode switch.

Import

import { useTheme } from '@vortexlabs/vortex-native';

Other links

import { Stack, Text, useTheme } from "@vortexlabs/vortex-native";
import { Switch } from "react-native";

const DarkModeSwitch = () => {
  const { colorMode, setColorMode } = useTheme();

  return (
    <Stack direction="row" gap={2} align="center">
      <Switch
        aria-label="Dark mode"
        value={colorMode === "dark"}
        onValueChange={(dark) => setColorMode(dark ? "dark" : "light")}
      />
      <Text>Dark mode</Text>
    </Stack>
  );
};

Features

  • Reads the nearest theme and color mode.
  • Exposes theme tokens for custom styles.
  • Sets a light, dark, or system preference, or toggles the mode.

Theme Tokens

theme holds the resolved tokens of the closest VortexProvider or ThemeScope. Read colors for the current colorMode to style your own views. Browse names and values in the design tokens reference.

import { useTheme } from "@vortexlabs/vortex-native";
import { View } from "react-native";

const Swatch = () => {
  const { theme, colorMode } = useTheme();
  const color = theme.color[colorMode];

  return (
    <View
      style={{
        backgroundColor: color.backgroundPrimaryDefault,
        borderRadius: theme.borderRadius.md,
        padding: theme.spacing.md,
      }}
    />
  );
};

Changing Color Mode

setColorMode takes light, dark, or system; system follows the device setting. invertColorMode switches between light and dark. colorModePreference is the requested value, including system.

const { colorModePreference, setColorMode, invertColorMode } = useTheme();

setColorMode("system");
invertColorMode();
colorModePreference; // "light", "dark", or "system"

Inside a ThemeScope, theme and colorMode come from the scope, while the preference and setters belong to the provider. With a controlled provider, the setters call onColorModeChange instead of changing the mode.