MCPcopy Create free account
hub / github.com/chokcoco/iCSS / ThemeToggle

Function ThemeToggle

website/app/components/ThemeToggle.tsx:9–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7import { ChevronDown, Sun, Moon, Monitor } from 'lucide-react';
8
9export default function ThemeToggle() {
10 const { theme, setTheme, t } = useApp();
11 const [isOpen, setIsOpen] = useState(false);
12 const dropdownRef = useRef<HTMLDivElement>(null);
13
14 const currentTheme = themes.find(t => t.value === theme) || themes[2];
15
16 const getThemeIcon = (themeValue: string) => {
17 switch (themeValue) {
18 case 'light':
19 return <Sun className="w-4 h-4" />;
20 case 'dark':
21 return <Moon className="w-4 h-4" />;
22 case 'system':
23 return <Monitor className="w-4 h-4" />;
24 default:
25 return <Monitor className="w-4 h-4" />;
26 }
27 };
28
29 useEffect(() => {
30 function handleClickOutside(event: MouseEvent) {
31 if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
32 setIsOpen(false);
33 }
34 }
35
36 document.addEventListener('mousedown', handleClickOutside);
37 return () => {
38 document.removeEventListener('mousedown', handleClickOutside);
39 };
40 }, []);
41
42 return (
43 <div className="relative" ref={dropdownRef}>
44 <button
45 onClick={() => setIsOpen(!isOpen)}
46 className="flex items-center space-x-2 px-3 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors"
47 >
48 {getThemeIcon(currentTheme.value)}
49 <span>{t(currentTheme.value as keyof Translations)}</span>
50 <ChevronDown className={`w-4 h-4 transition-transform ${isOpen ? 'rotate-180' : ''}`} />
51 </button>
52
53 {isOpen && (
54 <div className="absolute right-0 mt-2 w-48 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg shadow-lg z-50">
55 <div className="py-1">
56 {themes.map((themeOption: ThemeConfig) => (
57 <button
58 key={themeOption.value}
59 onClick={() => {
60 setTheme(themeOption.value);
61 setIsOpen(false);
62 }}
63 className={`w-full flex items-center space-x-3 px-4 py-2 text-sm text-left hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors ${
64 theme === themeOption.value
65 ? 'bg-primary-50 dark:bg-primary-900/20 text-primary-700 dark:text-primary-300'
66 : 'text-gray-700 dark:text-gray-200'

Callers

nothing calls this directly

Calls 4

useAppFunction · 0.90
getThemeIconFunction · 0.85
tFunction · 0.85
setThemeFunction · 0.85

Tested by

no test coverage detected