trading-platform-frontend-v2/src/hooks/useTheme.ts
rckrdmrd 737303d177 Migración desde trading-platform/apps/frontend - Estándar multi-repo v2
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-16 08:32:49 -06:00

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,
};
}