import { useState, useRef, useCallback } from 'react'; import { useTheme } from '../lib/theme'; import { useI18n, type TranslationKey } from '../lib/i18n'; import { useClickOutside } from '../hooks/useClickOutside'; const themes: Array<{ key: 'light' | 'dark' | 'system'; icon: JSX.Element }> = [ { key: 'light', icon: ( ), }, { key: 'dark', icon: ( ), }, { key: 'system', icon: ( ), }, ]; export default function ThemeToggle() { const { theme, setTheme } = useTheme(); const { t } = useI18n(); const [open, setOpen] = useState(false); const ref = useRef(null); useClickOutside(ref, useCallback(() => setOpen(false), []), open); const current = themes.find(th => th.key === theme)!; return ( setOpen(!open)} className="flex items-center gap-1 px-2 py-1.5 rounded-lg text-xs font-medium transition-all duration-150 cursor-pointer hover:bg-bg-tertiary" style={{ color: 'var(--text-secondary)' }} aria-label="Switch theme" > {current.icon} {open && ( {themes.map(th => ( { setTheme(th.key); setOpen(false); }} className="flex items-center gap-2.5 w-full px-3 py-2 text-[13px] transition-colors cursor-pointer rounded-lg mx-0.5" style={{ width: 'calc(100% - 4px)', color: theme === th.key ? 'var(--text-primary)' : 'var(--text-secondary)', fontWeight: theme === th.key ? 500 : 400, background: theme === th.key ? 'var(--bg-tertiary)' : 'transparent', }} > {th.icon} {t(`theme.${th.key}` as TranslationKey)} ))} )} ); }