| import { createContext, useContext, useState, useEffect } from "react"; |
|
|
| const ThemeContext = createContext(); |
|
|
| export function ThemeProvider({ children }) { |
| const [theme, setTheme] = useState("dark"); |
|
|
| useEffect(() => { |
| |
| const savedTheme = localStorage.getItem("intelli_theme"); |
| if (savedTheme) { |
| setTheme(savedTheme); |
| document.documentElement.setAttribute("data-theme", savedTheme); |
| } else { |
| const systemPreference = window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark"; |
| setTheme(systemPreference); |
| document.documentElement.setAttribute("data-theme", systemPreference); |
| } |
| }, []); |
|
|
| const toggleTheme = () => { |
| const newTheme = theme === "dark" ? "light" : "dark"; |
| setTheme(newTheme); |
| localStorage.setItem("intelli_theme", newTheme); |
| document.documentElement.setAttribute("data-theme", newTheme); |
| }; |
|
|
| return ( |
| <ThemeContext.Provider value={{ theme, toggleTheme }}> |
| {children} |
| </ThemeContext.Provider> |
| ); |
| } |
|
|
| export const useTheme = () => useContext(ThemeContext); |
|
|