-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathThemeContext.tsx
More file actions
57 lines (47 loc) · 1.71 KB
/
ThemeContext.tsx
File metadata and controls
57 lines (47 loc) · 1.71 KB
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
import React, { createContext, useState, useEffect, useContext, ReactNode } from 'react';
export type Theme = 'dark' | 'light';
interface ThemeContextType {
theme: Theme;
toggleTheme: () => void;
setTheme: (theme: Theme) => void;
isDark: boolean;
}
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
const THEME_KEY = 'brocode_theme';
export const ThemeProvider = ({ children }: { children: ReactNode }) => {
const [theme, setThemeState] = useState<Theme>(() => {
const saved = localStorage.getItem(THEME_KEY);
return (saved === 'light' || saved === 'dark') ? saved : 'dark';
});
useEffect(() => {
localStorage.setItem(THEME_KEY, theme);
const root = document.documentElement;
if (theme === 'light') {
root.classList.add('light-theme');
root.classList.remove('dark-theme');
document.body.classList.remove('bg-black', 'text-gray-200');
document.body.classList.add('bg-white', 'text-gray-800');
} else {
root.classList.add('dark-theme');
root.classList.remove('light-theme');
document.body.classList.remove('bg-white', 'text-gray-800');
document.body.classList.add('bg-black', 'text-gray-200');
}
}, [theme]);
const toggleTheme = () => {
setThemeState(prev => prev === 'dark' ? 'light' : 'dark');
};
const setTheme = (newTheme: Theme) => {
setThemeState(newTheme);
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme, setTheme, isDark: theme === 'dark' }}>
{children}
</ThemeContext.Provider>
);
};
export const useTheme = () => {
const context = useContext(ThemeContext);
if (!context) throw new Error('useTheme must be used within ThemeProvider');
return context;
};