saas-veille / lib /ThemeContext.tsx
PrestaGhis's picture
Update lib/ThemeContext.tsx
350fece verified
'use client';
import React, { createContext, useContext, useEffect, useState } from 'react';
type Theme = 'obsidian' | 'classic' | 'soft-gray' | 'warm-linen' | 'midnight' | 'cyberpunk' | 'atlantic' | 'slate' | 'crimson' | 'sapphire' | 'mint' | 'orange' | 'rose';
interface ThemeContextType {
theme: Theme;
setTheme: (theme: Theme) => void;
}
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
export function ThemeProvider({ children }: { children: React.ReactNode }) {
const [theme, setThemeState] = useState<Theme>('obsidian');
useEffect(() => {
const savedTheme = localStorage.getItem('app-theme') as Theme;
if (savedTheme) {
setThemeState(savedTheme);
document.documentElement.setAttribute('data-theme', savedTheme);
} else {
// Default to obsidian if no theme saved
document.documentElement.setAttribute('data-theme', 'obsidian');
}
}, []);
const setTheme = (newTheme: Theme) => {
setThemeState(newTheme);
localStorage.setItem('app-theme', newTheme);
document.documentElement.setAttribute('data-theme', newTheme);
};
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{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;
}