forked from githru/githru-vscode-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemeSelector.tsx
More file actions
102 lines (90 loc) · 2.81 KB
/
Copy pathThemeSelector.tsx
File metadata and controls
102 lines (90 loc) · 2.81 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
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
import { useEffect, useRef, useState } from "react";
import "./ThemeSelector.scss";
import AutoAwesomeIcon from "@mui/icons-material/AutoAwesome";
import CloseIcon from "@mui/icons-material/Close";
import { sendUpdateThemeCommand } from "services";
import { useThemeStore } from "store/theme";
import type { ThemeName } from "theme";
import { THEME_INFO } from "./ThemeSelector.const";
import type { ThemeInfo } from "./ThemeSelector.type";
type ThemeIconsProps = ThemeInfo[keyof ThemeInfo] & {
theme: string;
onClick: () => void;
};
const ThemeIcons = ({ title, value, colors, theme, onClick }: ThemeIconsProps) => {
return (
<div
className={`theme-icon${theme === value ? "--selected" : ""}`}
onClick={onClick}
role="presentation"
>
<div className="theme-icon__container">
{Object.values(colors).map((color, index) => (
<div
key={Number(index)}
className="theme-icon__color"
style={{ backgroundColor: color }}
/>
))}
</div>
<p className="theme-icon__title">{title}</p>
</div>
);
};
const ThemeSelector = () => {
const [isOpen, setIsOpen] = useState(false);
const themeSelectorRef = useRef<HTMLDivElement>(null);
const { theme, setTheme } = useThemeStore();
const handleTheme = (value: ThemeName) => {
setTheme(value);
sendUpdateThemeCommand(value);
document.documentElement.setAttribute("theme", value);
};
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (themeSelectorRef.current && !themeSelectorRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, []);
useEffect(() => {
document.documentElement.setAttribute("theme", theme);
}, [theme]);
return (
<div
className="theme-selector"
ref={themeSelectorRef}
>
<AutoAwesomeIcon onClick={() => setIsOpen(true)} />
{isOpen && (
<div className="theme-selector__container">
<div className="theme-selector__header">
<p>Theme</p>
<CloseIcon
fontSize="small"
onClick={() => setIsOpen(false)}
/>
</div>
<div className="theme-selector__list">
{Object.entries(THEME_INFO).map(([_, themeInfo]) => (
<ThemeIcons
key={themeInfo.value}
{...themeInfo}
theme={theme}
onClick={() => {
handleTheme(themeInfo.value);
setIsOpen(false);
}}
/>
))}
</div>
</div>
)}
</div>
);
};
export default ThemeSelector;