deckhearth/lib/theme-context.js
varutasu 81bed51369
fix(lint): clear lib/config baseline and make CI lint blocking. (#63)
Lazy-init theme from localStorage, hoist checkAuth with useCallback,
named config exports for PostCSS/Tailwind, and remove the non-blocking
|| true wrapper from ci.yml (requires #61 + #62 merged first).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 01:03:42 -05:00

37 lines
No EOL
1,012 B
JavaScript

import { createContext, useContext, useEffect, useState } from 'react';
const ThemeContext = createContext();
function readThemeFromStorage() {
if (typeof window === 'undefined') return 'light';
return localStorage.getItem('theme') || 'light';
}
export function ThemeProvider({ children }) {
const [theme, setTheme] = useState(readThemeFromStorage);
useEffect(() => {
document.documentElement.setAttribute('data-theme', theme);
}, [theme]);
const toggleTheme = () => {
const newTheme = theme === 'light' ? 'dark' : 'light';
setTheme(newTheme);
localStorage.setItem('theme', newTheme);
document.documentElement.setAttribute('data-theme', newTheme);
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
}
export function useTheme() {
const context = useContext(ThemeContext);
if (context === undefined) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
}