import { useState, useEffect } from 'react'; import { Sun, Moon, Monitor } from 'lucide-react'; /** * ThemeToggle - Dark/Light mode toggle with SVG icons * * Provides accessible theme switching with smooth animations. */ type Theme = 'light' | 'dark' | 'system'; export default function ThemeToggle() { const [theme, setTheme] = useState('system'); useEffect(() => { const savedTheme = localStorage.getItem('theme') as Theme | null; if (savedTheme) { setTheme(savedTheme); applyTheme(savedTheme); } else { applyTheme('system'); } }, []); function applyTheme(newTheme: Theme) { const root = window.document.documentElement; root.classList.remove('light', 'dark'); if (newTheme === 'system') { const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; root.classList.add(systemTheme); } else { root.classList.add(newTheme); } } function handleThemeChange(newTheme: Theme) { setTheme(newTheme); localStorage.setItem('theme', newTheme); applyTheme(newTheme); } const themes = [ { value: 'light' as Theme, icon: Sun, label: 'Light' }, { value: 'dark' as Theme, icon: Moon, label: 'Dark' }, { value: 'system' as Theme, icon: Monitor, label: 'Auto' }, ]; return (
{themes.map(({ value, icon: Icon, label }) => ( ))}
); }