-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathColorThemeSwitcher.tsx
More file actions
68 lines (60 loc) · 2.3 KB
/
ColorThemeSwitcher.tsx
File metadata and controls
68 lines (60 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useEffect, useState } from 'react';
import { themes, changeTheme, subscribeToThemeChanges, initializeThemeWatcher } from '../lib/themeUtils';
export default function ColorThemeSwitcher() {
const [currentTheme, setCurrentTheme] = useState<string>('default');
const [isMounted, setIsMounted] = useState(false);
useEffect(() => {
setIsMounted(true);
// Initialize theme watcher (only once globally)
const watcherCleanup = initializeThemeWatcher();
// Subscribe to theme changes
const unsubscribe = subscribeToThemeChanges((themeId) => {
setCurrentTheme(themeId);
});
return () => {
unsubscribe();
watcherCleanup();
};
}, []);
const handleThemeChange = (themeId: string) => {
changeTheme(themeId);
};
if (!isMounted) return null;
return (
<div className="flex items-center gap-2 flex-wrap">
<span className="text-xs text-gray-600 dark:text-gray-400 mr-2">Color theme:</span>
<div className="flex gap-2 flex-wrap">
{themes.map((theme) => (
<button
key={theme.id}
onClick={() => handleThemeChange(theme.id)}
className={`w-6 h-6 rounded-full transition-all hover:scale-110 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-theme-secondary flex items-center justify-center ${
currentTheme === theme.id
? 'ring-2 ring-gray-900 dark:ring-white ring-offset-2 scale-110'
: 'opacity-60 hover:opacity-100'
} ${theme.id === 'random' ? 'bg-gray-200 dark:bg-gray-700' : ''}`}
style={theme.id !== 'random' ? { backgroundImage: theme.dotGradient } : {}}
aria-label={`Switch to ${theme.name} theme`}
title={theme.name}
>
{theme.id === 'random' ? (
<svg
className="w-4 h-4 text-gray-700 dark:text-gray-300"
fill="none"
stroke="currentColor"
strokeWidth="2"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M7 16V4m0 0L3 8m4-4l4 4m6 0v12m0 0l4-4m-4 4l-4-4"
/>
</svg>
) : null}
</button>
))}
</div>
</div>
);
}