feat: update ThemeContext to use data-theme attribute, simplify to light/dark
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,59 +1,40 @@
|
||||
import React, { useEffect, useState, type ReactNode } from 'react';
|
||||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
|
||||
import { darkTheme, dimTheme, lightTheme, type ThemeMode } from '../types/theme';
|
||||
import { ThemeContext } from './theme-context';
|
||||
|
||||
const STORAGE_KEY = 'app-theme-mode';
|
||||
|
||||
function getInitialMode(): ThemeMode {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
if (stored === 'dark' || stored === 'dim' || stored === 'light') return stored;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return 'dark';
|
||||
type Theme = 'light' | 'dark';
|
||||
interface ThemeContextValue {
|
||||
theme: Theme;
|
||||
toggleTheme: () => void;
|
||||
}
|
||||
|
||||
const THEME_MAP = { dark: darkTheme, dim: dimTheme, light: lightTheme };
|
||||
const BG_MAP: Record<ThemeMode, string> = {
|
||||
dark: '#0a0a12',
|
||||
dim: '#1e1b2e',
|
||||
light: '#ffffff',
|
||||
};
|
||||
const ThemeContext = createContext<ThemeContextValue>({
|
||||
theme: 'light',
|
||||
toggleTheme: () => {},
|
||||
});
|
||||
|
||||
interface ThemeProviderProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const ThemeProvider: React.FC<ThemeProviderProps> = ({ children }) => {
|
||||
const [themeMode, setThemeMode] = useState<ThemeMode>(getInitialMode);
|
||||
const theme = THEME_MAP[themeMode];
|
||||
const isDark = themeMode === 'dark';
|
||||
|
||||
const toggleTheme = () => {
|
||||
setThemeMode((prev) =>
|
||||
prev === 'dark' ? 'dim' : prev === 'dim' ? 'light' : 'dark'
|
||||
);
|
||||
};
|
||||
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
||||
const [theme, setTheme] = useState<Theme>(() => {
|
||||
const saved = localStorage.getItem('theme');
|
||||
return (saved === 'dark' || saved === 'light') ? saved : 'light';
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const root = document.documentElement;
|
||||
root.classList.remove('dark', 'dim');
|
||||
if (themeMode !== 'light') root.classList.add(themeMode);
|
||||
|
||||
document.body.style.background = BG_MAP[themeMode];
|
||||
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, themeMode);
|
||||
} catch {
|
||||
// ignore
|
||||
if (theme === 'dark') {
|
||||
document.documentElement.setAttribute('data-theme', 'dark');
|
||||
} else {
|
||||
document.documentElement.removeAttribute('data-theme');
|
||||
}
|
||||
}, [themeMode]);
|
||||
localStorage.setItem('theme', theme);
|
||||
}, [theme]);
|
||||
|
||||
const toggleTheme = () => setTheme(t => t === 'light' ? 'dark' : 'light');
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ isDark, themeMode, theme, toggleTheme }}>
|
||||
<ThemeContext.Provider value={{ theme, toggleTheme }}>
|
||||
{children}
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
return useContext(ThemeContext);
|
||||
}
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
export { ThemeProvider } from './ThemeContext';
|
||||
export { useTheme } from './useTheme';
|
||||
export { ThemeProvider, useTheme } from './ThemeContext';
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
import type { ThemeColors, ThemeMode } from '../types/theme';
|
||||
|
||||
export interface ThemeContextValue {
|
||||
isDark: boolean;
|
||||
themeMode: ThemeMode;
|
||||
theme: ThemeColors;
|
||||
toggleTheme: () => void;
|
||||
}
|
||||
|
||||
export const ThemeContext = createContext<ThemeContextValue | undefined>(undefined);
|
||||
@@ -1,11 +0,0 @@
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { ThemeContext, type ThemeContextValue } from './theme-context';
|
||||
|
||||
export function useTheme(): ThemeContextValue {
|
||||
const context = useContext(ThemeContext);
|
||||
if (!context) {
|
||||
throw new Error('useTheme must be used within a ThemeProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user