2026-05-18 16:32:42 +08:00
|
|
|
import React, { useEffect, useState, type ReactNode } from 'react';
|
2026-05-14 15:07:34 +08:00
|
|
|
|
2026-05-18 16:32:42 +08:00
|
|
|
import { darkTheme, lightTheme } from '../types/theme';
|
|
|
|
|
import { ThemeContext } from './theme-context';
|
2026-05-14 15:07:34 +08:00
|
|
|
|
|
|
|
|
interface ThemeProviderProps {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ThemeProvider: React.FC<ThemeProviderProps> = ({ children }) => {
|
|
|
|
|
const [isDark, setIsDark] = useState<boolean>(true);
|
|
|
|
|
const theme = isDark ? darkTheme : lightTheme;
|
|
|
|
|
|
|
|
|
|
const toggleTheme = () => {
|
|
|
|
|
setIsDark((prev) => !prev);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-25 16:19:18 +08:00
|
|
|
document.documentElement.classList.toggle('dark', isDark);
|
|
|
|
|
document.body.classList.toggle('dark-mode', isDark);
|
|
|
|
|
|
2026-05-14 15:07:34 +08:00
|
|
|
if (isDark) {
|
|
|
|
|
document.body.style.background = '#0a0a12';
|
2026-05-18 16:32:42 +08:00
|
|
|
return;
|
2026-05-14 15:07:34 +08:00
|
|
|
}
|
2026-05-18 16:32:42 +08:00
|
|
|
|
|
|
|
|
document.body.style.background = '#ffffff';
|
2026-05-14 15:07:34 +08:00
|
|
|
}, [isDark]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ThemeContext.Provider value={{ isDark, theme, toggleTheme }}>
|
|
|
|
|
{children}
|
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
|
);
|
2026-05-18 16:32:42 +08:00
|
|
|
};
|