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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
import React, { useState } from "react";
import { useTheme, type ThemeName } from "../styles/ThemeProvider";
import "./ThemeSwitcher.css";
interface ThemeSwitcherProps {
variant?: "dropdown" | "buttons" | "compact";
showLabel?: boolean;
}
const themeIcons: Record<ThemeName, string> = {
light: "☀️",
dark: "🌙",
sepia: "📜",
noir: "⚫",
ocean: "🌊",
forest: "🌲",
gruvbox: "🍂",
};
const themeLabels: Record<ThemeName, string> = {
light: "Light",
dark: "Dark",
sepia: "Sepia",
noir: "Noir",
ocean: "Ocean",
forest: "Forest",
gruvbox: "Gruvbox",
};
export const ThemeSwitcher: React.FC<ThemeSwitcherProps> = ({
variant = "dropdown",
showLabel = true,
}) => {
const { themeName, setTheme, availableThemes } = useTheme();
const [isOpen, setIsOpen] = useState(false);
const handleThemeChange = (theme: ThemeName) => {
setTheme(theme);
setIsOpen(false);
};
const cycleTheme = () => {
const currentIndex = availableThemes.indexOf(themeName);
const nextIndex = (currentIndex + 1) % availableThemes.length;
setTheme(availableThemes[nextIndex]);
};
if (variant === "compact") {
return (
<button
className="theme-switcher-compact"
onClick={cycleTheme}
title={`Current theme: ${themeLabels[themeName]}. Click to switch.`}
aria-label="Switch theme"
>
<span className="theme-icon">{themeIcons[themeName]}</span>
{showLabel && (
<span className="theme-label">{themeLabels[themeName]}</span>
)}
</button>
);
}
if (variant === "buttons") {
return (
<div className="theme-switcher-buttons">
{showLabel && <span className="theme-label">Theme:</span>}
<div className="theme-buttons-group">
{availableThemes.map((theme) => (
<button
key={theme}
className={`theme-button ${themeName === theme ? "active" : ""}`}
onClick={() => handleThemeChange(theme)}
title={themeLabels[theme]}
aria-label={`Switch to ${themeLabels[theme]} theme`}
aria-pressed={themeName === theme}
>
<span className="theme-icon">{themeIcons[theme]}</span>
{showLabel && (
<span className="theme-name">{themeLabels[theme]}</span>
)}
</button>
))}
</div>
</div>
);
}
// Default dropdown variant
return (
<div className="theme-switcher-dropdown">
<button
className="theme-dropdown-toggle"
onClick={() => setIsOpen(!isOpen)}
aria-haspopup="true"
aria-expanded={isOpen}
>
<span className="theme-icon">{themeIcons[themeName]}</span>
{showLabel && (
<span className="theme-label">{themeLabels[themeName]}</span>
)}
<span className="dropdown-arrow">▼</span>
</button>
{isOpen && (
<>
<div
className="theme-dropdown-backdrop"
onClick={() => setIsOpen(false)}
aria-hidden="true"
/>
<div className="theme-dropdown-menu" role="menu">
{availableThemes.map((theme) => (
<button
key={theme}
className={`theme-dropdown-item ${themeName === theme ? "active" : ""}`}
onClick={() => handleThemeChange(theme)}
role="menuitem"
aria-selected={themeName === theme}
>
<span className="theme-icon">{themeIcons[theme]}</span>
<span className="theme-name">{themeLabels[theme]}</span>
{themeName === theme && <span className="checkmark">✓</span>}
</button>
))}
</div>
</>
)}
</div>
);
};
|