Spaces:
Sleeping
Sleeping
Download Frontend/src/context/ThemeContext.jsx from smit9604/ExamCraftAI: direct link, hf CLI and curl.
- Browser
- Download file 983 Bytes
-
https://huggingface.co/spaces/smit9604/ExamCraftAI/resolve/main/Frontend/src/context/ThemeContext.jsx
- Command line
-
hf download hf://spaces/smit9604/ExamCraftAI/Frontend/src/context/ThemeContext.jsx
-
curl -L -o ThemeContext.jsx https://huggingface.co/spaces/smit9604/ExamCraftAI/resolve/main/Frontend/src/context/ThemeContext.jsx
983 Bytes
| /* eslint-disable react-refresh/only-export-components */ | |
| import { createContext, useContext, useEffect, useState } from "react"; | |
| const ThemeContext = createContext(); | |
| export function ThemeProvider({ children }) { | |
| const [theme, setTheme] = useState(() => { | |
| return localStorage.getItem("theme") || "dark"; | |
| }); | |
| useEffect(() => { | |
| const root = window.document.documentElement; | |
| root.classList.remove("light", "dark"); | |
| root.classList.add(theme); | |
| localStorage.setItem("theme", theme); | |
| }, [theme]); | |
| const toggleTheme = () => { | |
| setTheme((prev) => (prev === "dark" ? "light" : "dark")); | |
| }; | |
| return ( | |
| <ThemeContext.Provider value={{ theme, toggleTheme }}> | |
| {children} | |
| </ThemeContext.Provider> | |
| ); | |
| } | |
| export const useTheme = () => { | |
| const context = useContext(ThemeContext); | |
| if (!context) throw new Error("useTheme must be used within a ThemeProvider"); | |
| return context; | |
| }; | |