Skip to main content

Color Modes

Every theme defines light and dark colors through the light and dark values in its semantic color tokens. Changing the color mode keeps the selected theme.

Set the initial mode

After loading your theme, set defaultColorMode on VortexProvider:

import { VortexProvider } from "@vortexlabs/vortex";

<VortexProvider defaultTheme="acme" defaultColorMode="dark">
  {children}
</VortexProvider>

defaultColorMode sets the initial preference; the app root defaults to "light". For nested providers, see Embedded Content.

Follow the system

Set defaultColorMode="system" to follow the operating system's appearance, including changes while your app is open:

<VortexProvider defaultTheme="acme" defaultColorMode="system">
  {children}
</VortexProvider>

Use setColorMode("system") from useTheme to resume following the OS after an explicit selection. colorModePreference reports "system", while colorMode resolves to "light" or "dark".

Switch modes

Render this toggle inside your provider:

src/ColorModeToggle.tsx
"use client";

import { Button, useTheme } from "@vortexlabs/vortex";

export const ColorModeToggle = () => {
  const { colorMode, invertColorMode } = useTheme();

  return (
    <Button onClick={invertColorMode}>
      {colorMode === "light" ? "Use dark mode" : "Use light mode"}
    </Button>
  );
};

Use setColorMode("light") or setColorMode("dark") from the same hook to select a specific mode. Either selection, or invertColorMode(), stops following the OS. These setters change the mode for the whole provider, even when called inside a ThemeScope. To keep one section dark or inverted, use Scoped Theming.

Tailwind CSS semantic color utilities follow the mode; use dark: only for extra changes.

Control and remember the mode

If your app owns the resolved mode, pass colorMode and handle change requests through onColorModeChange:

<VortexProvider
  defaultTheme="acme"
  colorMode={colorMode}
  onColorModeChange={setColorMode}
>
  {children}
</VortexProvider>

Here, colorMode and setColorMode are your app's state and setter. The controlled colorMode prop accepts only "light" or "dark".

Remember the preference

Use onColorModePreferenceChange to save the preference in a cookie or user setting. Restore a validated "light", "dark", or "system" value as savedPreference:

<VortexProvider
  defaultTheme="acme"
  defaultColorMode={savedPreference}
  onColorModePreferenceChange={(preference) => {
    document.cookie = "color-mode-preference=" + preference + "; path=/";
  }}
>
  {children}
</VortexProvider>

Keep this callback inside a client component. Vortex doesn't persist or restore preferences automatically. Automatic OS changes don't call onColorModePreferenceChange, so a saved "system" stays "system".

Server rendering

Set <html data-vortex-color-mode> to a resolved "light" or "dark" value, and pass the saved preference to defaultColorMode. Never write "system" to the HTML attribute.

System mode renders as light on the server and where matchMedia is unavailable. To match the OS before the first paint, resolve prefers-color-scheme in a client bootstrap script before hydration and set the HTML attribute. The root provider reads this initial mode during hydration.

See the Next.js guide for the root layout.