File size: 4,643 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import React, { createContext, useContext, useEffect, useMemo, useCallback, useRef } from 'react';
import { useAtom } from 'jotai';
import { IThemeRGB } from '../types';
import applyTheme from '../utils/applyTheme';
import { themeModeAtom, themeColorsAtom, themeNameAtom } from '../atoms/themeAtoms';
type ThemeContextType = {
theme: string; // 'light' | 'dark' | 'system'
setTheme: (theme: string) => void;
themeRGB?: IThemeRGB;
setThemeRGB: (colors?: IThemeRGB) => void;
themeName?: string;
setThemeName: (name?: string) => void;
resetTheme: () => void;
};
// Export ThemeContext so it can be imported from hooks
export const ThemeContext = createContext<ThemeContextType>({
theme: 'system',
setTheme: () => undefined,
setThemeRGB: () => undefined,
setThemeName: () => undefined,
resetTheme: () => undefined,
});
export interface ThemeProviderProps {
children: React.ReactNode;
themeRGB?: IThemeRGB;
themeName?: string;
initialTheme?: string;
}
/**
* Check if theme is dark
*/
export const isDark = (theme: string): boolean => {
if (theme === 'system') {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
return theme === 'dark';
};
/**
* ThemeProvider component that handles both dark/light mode switching
* and dynamic color themes via CSS variables with localStorage persistence
*/
export function ThemeProvider({
children,
themeRGB: propThemeRGB,
themeName: propThemeName,
initialTheme,
}: ThemeProviderProps) {
// Use jotai atoms for persistent state
const [theme, setTheme] = useAtom(themeModeAtom);
const [storedThemeRGB, setStoredThemeRGB] = useAtom(themeColorsAtom);
const [storedThemeName, setStoredThemeName] = useAtom(themeNameAtom);
// Track if props have been initialized
const propsInitialized = useRef(false);
// Initialize from props only once on mount
useEffect(() => {
if (!propsInitialized.current) {
propsInitialized.current = true;
// Set initial theme if provided
if (initialTheme) {
setTheme(initialTheme);
}
// Set initial theme colors if provided
if (propThemeRGB) {
setStoredThemeRGB(propThemeRGB);
}
// Set initial theme name if provided
if (propThemeName) {
setStoredThemeName(propThemeName);
}
}
}, [initialTheme, propThemeRGB, propThemeName, setTheme, setStoredThemeRGB, setStoredThemeName]);
// Apply class-based dark mode
const applyThemeMode = useCallback((rawTheme: string) => {
const root = window.document.documentElement;
const darkMode = isDark(rawTheme);
root.classList.remove(darkMode ? 'light' : 'dark');
root.classList.add(darkMode ? 'dark' : 'light');
}, []);
// Handle system theme changes
useEffect(() => {
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const changeThemeOnSystemChange = () => {
if (theme === 'system') {
applyThemeMode('system');
}
};
mediaQuery.addEventListener('change', changeThemeOnSystemChange);
return () => {
mediaQuery.removeEventListener('change', changeThemeOnSystemChange);
};
}, [theme, applyThemeMode]);
// Apply dark/light mode class
useEffect(() => {
applyThemeMode(theme);
}, [theme, applyThemeMode]);
// Apply dynamic color theme
useEffect(() => {
if (storedThemeRGB) {
applyTheme(storedThemeRGB);
}
}, [storedThemeRGB]);
// Reset theme function
const resetTheme = useCallback(() => {
setTheme('system');
setStoredThemeRGB(undefined);
setStoredThemeName(undefined);
// Remove any custom CSS variables
const root = document.documentElement;
const customProps = Array.from(root.style).filter((prop) => prop.startsWith('--'));
customProps.forEach((prop) => root.style.removeProperty(prop));
}, [setTheme, setStoredThemeRGB, setStoredThemeName]);
const value = useMemo(
() => ({
theme,
setTheme,
themeRGB: storedThemeRGB,
setThemeRGB: setStoredThemeRGB,
themeName: storedThemeName,
setThemeName: setStoredThemeName,
resetTheme,
}),
[
theme,
setTheme,
storedThemeRGB,
setStoredThemeRGB,
storedThemeName,
setStoredThemeName,
resetTheme,
],
);
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
}
/**
* Hook to access the current theme context
*/
export function useTheme() {
const context = useContext(ThemeContext);
if (!context) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
}
export default ThemeProvider;
|