Skip to main content

Overview

The ColorThemeContext provides theme management functionality for the application, allowing users to choose between system, light, or dark color themes.

Type Definitions

type ColorTheme = "system" | "light" | "dark";

interface IColorThemeContext {
    colorTheme: ColorTheme;
    setColorTheme: (colorTheme: ColorTheme) => void;
}

Context Values

colorTheme
ColorTheme
required
The current color theme setting. Can be "system", "light", or "dark".
setColorTheme
(colorTheme: ColorTheme) => void
required
Function to update the color theme preference. Accepts one of the three valid theme values.

Provider Usage

The ColorThemeContext.Provider is configured in _app.tsx:
import ColorThemeContext from "@/context/colorThemeContext";
import useColorTheme from "@/hooks/useColorTheme";

function MyApp({ Component, pageProps }: AppProps) {
    const [colorTheme, setColorTheme] = useColorTheme();

    return (
        <ColorThemeContext.Provider value={{ colorTheme, setColorTheme }}>
            {/* App content */}
        </ColorThemeContext.Provider>
    );
}

Consumer Usage

Using useContext Hook

import { useContext } from "react";
import ColorThemeContext from "@/context/colorThemeContext";

function ThemeSwitcher() {
    const { colorTheme, setColorTheme } = useContext(ColorThemeContext);

    return (
        <div>
            <button onClick={() => setColorTheme("light")}>Light</button>
            <button onClick={() => setColorTheme("dark")}>Dark</button>
            <button onClick={() => setColorTheme("system")}>System</button>
            <p>Current theme: {colorTheme}</p>
        </div>
    );
}

Utility Functions

isColorTheme

Type guard function to validate if a string is a valid color theme:
function isColorTheme(value?: string | null): value is ColorTheme
Example:
import { isColorTheme } from "@/context/colorThemeContext";

const userPref = localStorage.getItem("theme");

if (isColorTheme(userPref)) {
    setColorTheme(userPref);
}

Source

Source code: src/context/colorThemeContext.ts

Build docs developers (and LLMs) love