Download src/context/ThemeContext.js from SyedFouzan/taskflow-titan-workflow-wizardry: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/SyedFouzan/taskflow-titan-workflow-wizardry/resolve/main/src/context/ThemeContext.js
- Command line
-
hf download hf://spaces/SyedFouzan/taskflow-titan-workflow-wizardry/src/context/ThemeContext.js
-
curl -L -o ThemeContext.js https://huggingface.co/spaces/SyedFouzan/taskflow-titan-workflow-wizardry/resolve/main/src/context/ThemeContext.js
1.05 kB
| import React, { createContext, useContext, useState, useEffect } from 'react'; | |
| const ThemeContext = createContext(); | |
| export const useTheme = () => { | |
| const context = useContext(ThemeContext); | |
| if (!context) { | |
| throw new Error('useTheme must be used within a ThemeProvider'); | |
| } | |
| return context; | |
| }; | |
| export const ThemeProvider = ({ children }) => { | |
| const [isDark, setIsDark] = useState(() => { | |
| const savedTheme = localStorage.getItem('theme'); | |
| return savedTheme ? savedTheme === 'dark' : | |
| window.matchMedia('(prefers-color-scheme: dark)').matches; | |
| }); | |
| useEffect(() => { | |
| if (isDark) { | |
| document.documentElement.classList.add('dark'); | |
| } else { | |
| document.documentElement.classList.remove('dark'); | |
| } | |
| localStorage.setItem('theme', isDark ? 'dark' : 'light'); | |
| }, [isDark]); | |
| const toggleTheme = () => { | |
| setIsDark(!isDark); | |
| }; | |
| const value = { | |
| isDark, | |
| toggleTheme | |
| }; | |
| return ( | |
| <ThemeContext.Provider value={value}> | |
| {children} | |
| </ThemeContext.Provider> | |
| ); | |
| }; |