···
import { createSignal } from "solid-js";
3
-
const getInitialTheme = () => {
5
-
localStorage.theme === "dark" ||
6
-
(!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches);
7
-
return { color: isDarkMode ? "dark" : "light", system: !("theme" in localStorage) };
export const themeEvent = () => {
11
-
if (!theme().system) return;
4
+
if (localStorage.getItem("theme") !== null) return;
const isDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
13
-
setTheme({ color: isDark ? "dark" : "light", system: theme().system });
document.documentElement.classList.toggle("dark", isDark);
17
-
export const [theme, setTheme] = createSignal(getInitialTheme());
9
+
export const ThemeSelection = () => {
10
+
const [theme, setTheme] = createSignal(
11
+
localStorage.getItem("theme") === null ? "system"
12
+
: localStorage.theme === "dark" ? "dark"
19
-
export const ThemeSelection = () => {
20
-
const updateTheme = (newTheme: { color: string; system: boolean }) => {
16
+
const updateTheme = (newTheme: string) => {
22
-
document.documentElement.classList.toggle("dark", newTheme.color === "dark");
23
-
if (newTheme.system) localStorage.removeItem("theme");
24
-
else localStorage.theme = newTheme.color;
18
+
document.documentElement.classList.toggle(
20
+
newTheme === "dark" ||
21
+
(newTheme === "system" && window.matchMedia("(prefers-color-scheme: dark)").matches),
23
+
if (newTheme === "system") localStorage.removeItem("theme");
24
+
else localStorage.theme = newTheme;
28
-
<div class="mt-2 flex items-center justify-between gap-1 text-base">
32
-
"p-1.5 flex items-center rounded-full": true,
33
-
"bg-neutral-200 dark:bg-neutral-600": theme().system,
37
-
color: window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light",
42
-
<span class="iconify lucide--monitor"></span>
27
+
const ThemeButton = (props: { theme: string; icon: string }) => {
"p-1.5 flex items-center rounded-full": true,
48
-
"bg-neutral-200": theme().color === "light" && !theme().system,
32
+
"bg-neutral-200 dark:bg-neutral-600": theme() === props.theme,
50
-
onclick={() => updateTheme({ color: "light", system: false })}
34
+
onclick={() => updateTheme(props.theme)}
52
-
<span class="iconify lucide--sun"></span>
36
+
<span class={"iconify " + props.icon}></span>
57
-
"p-1.5 flex items-center rounded-full": true,
58
-
"bg-neutral-600": theme().color === "dark" && !theme().system,
60
-
onclick={() => updateTheme({ color: "dark", system: false })}
62
-
<span class="iconify lucide--moon"></span>
42
+
<div class="mt-2 flex items-center justify-between gap-1 text-base">
43
+
<ThemeButton theme="system" icon="lucide--monitor" />
44
+
<ThemeButton theme="light" icon="lucide--sun" />
45
+
<ThemeButton theme="dark" icon="lucide--moon" />