53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
/**
|
|
* Theme Hook - Manages dark/light mode
|
|
*/
|
|
|
|
import { useState, useEffect } from 'react';
|
|
|
|
type Theme = 'light' | 'dark';
|
|
|
|
export function useTheme() {
|
|
const [theme, setTheme] = useState<Theme>(() => {
|
|
// Check localStorage first
|
|
const stored = localStorage.getItem('theme') as Theme | null;
|
|
if (stored) return stored;
|
|
|
|
// Check system preference
|
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
return 'dark';
|
|
}
|
|
|
|
// Default to dark for trading platform
|
|
return 'dark';
|
|
});
|
|
|
|
useEffect(() => {
|
|
const root = window.document.documentElement;
|
|
|
|
// Remove both classes first
|
|
root.classList.remove('light', 'dark');
|
|
|
|
// Add the current theme class
|
|
root.classList.add(theme);
|
|
|
|
// Store in localStorage
|
|
localStorage.setItem('theme', theme);
|
|
}, [theme]);
|
|
|
|
const toggleTheme = () => {
|
|
setTheme((prev) => (prev === 'dark' ? 'light' : 'dark'));
|
|
};
|
|
|
|
const setDarkMode = () => setTheme('dark');
|
|
const setLightMode = () => setTheme('light');
|
|
|
|
return {
|
|
theme,
|
|
isDark: theme === 'dark',
|
|
isLight: theme === 'light',
|
|
toggleTheme,
|
|
setDarkMode,
|
|
setLightMode,
|
|
};
|
|
}
|