2026-04-11 23:27:29 -04:00
|
|
|
"use client";
|
|
|
|
|
|
2026-05-25 09:49:40 -04:00
|
|
|
import { useSyncExternalStore } from "react";
|
|
|
|
|
|
2026-04-11 23:27:29 -04:00
|
|
|
type Theme = "light" | "dark";
|
|
|
|
|
|
|
|
|
|
const STORAGE_KEY = "theme-preference";
|
2026-05-25 09:49:40 -04:00
|
|
|
const THEME_CHANGE_EVENT = "theme-preference-change";
|
|
|
|
|
|
|
|
|
|
function getSystemTheme(): Theme {
|
|
|
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
|
|
|
}
|
2026-04-11 23:27:29 -04:00
|
|
|
|
|
|
|
|
function applyTheme(theme: Theme) {
|
|
|
|
|
document.documentElement.dataset.theme = theme;
|
2026-05-25 09:49:40 -04:00
|
|
|
try {
|
|
|
|
|
window.localStorage.setItem(STORAGE_KEY, theme);
|
|
|
|
|
} catch {
|
|
|
|
|
}
|
|
|
|
|
window.dispatchEvent(new Event(THEME_CHANGE_EVENT));
|
2026-04-11 23:27:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCurrentTheme(): Theme {
|
2026-05-25 09:49:40 -04:00
|
|
|
if (typeof window === "undefined") return "light";
|
|
|
|
|
|
2026-04-11 23:27:29 -04:00
|
|
|
const currentTheme = document.documentElement.dataset.theme;
|
|
|
|
|
if (currentTheme === "light" || currentTheme === "dark") {
|
|
|
|
|
return currentTheme;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 09:49:40 -04:00
|
|
|
return getSystemTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function subscribeToTheme(callback: () => void) {
|
|
|
|
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
|
|
|
const handleSystemThemeChange = () => {
|
|
|
|
|
try {
|
|
|
|
|
if (!window.localStorage.getItem(STORAGE_KEY)) {
|
|
|
|
|
document.documentElement.dataset.theme = getSystemTheme();
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
document.documentElement.dataset.theme = getSystemTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mediaQuery.addEventListener("change", handleSystemThemeChange);
|
|
|
|
|
window.addEventListener("storage", callback);
|
|
|
|
|
window.addEventListener(THEME_CHANGE_EVENT, callback);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
mediaQuery.removeEventListener("change", handleSystemThemeChange);
|
|
|
|
|
window.removeEventListener("storage", callback);
|
|
|
|
|
window.removeEventListener(THEME_CHANGE_EVENT, callback);
|
|
|
|
|
};
|
2026-04-11 23:27:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ThemeToggle() {
|
2026-05-25 09:49:40 -04:00
|
|
|
const theme = useSyncExternalStore(subscribeToTheme, getCurrentTheme, () => "light");
|
|
|
|
|
|
2026-04-11 23:27:29 -04:00
|
|
|
const toggleTheme = () => {
|
|
|
|
|
const nextTheme = getCurrentTheme() === "dark" ? "light" : "dark";
|
|
|
|
|
applyTheme(nextTheme);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={toggleTheme}
|
|
|
|
|
title="Toggle color theme"
|
2026-05-25 09:49:40 -04:00
|
|
|
aria-label={`Switch to ${theme === "dark" ? "light" : "dark"} theme`}
|
|
|
|
|
aria-pressed={theme === "dark"}
|
|
|
|
|
className="theme-toggle inline-flex h-8 w-8 items-center justify-center rounded-full border border-transparent text-muted-strong transition-colors hover:border-line hover:bg-accent-soft hover:text-ink"
|
2026-04-11 23:27:29 -04:00
|
|
|
>
|
|
|
|
|
<span className="sr-only">Toggle color theme</span>
|
|
|
|
|
<svg
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
className="theme-toggle__sun h-3.5 w-3.5 fill-none stroke-current"
|
|
|
|
|
strokeWidth="1.7"
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
>
|
|
|
|
|
<circle cx="12" cy="12" r="4" />
|
|
|
|
|
<path d="M12 2.5v2.2" />
|
|
|
|
|
<path d="M12 19.3v2.2" />
|
|
|
|
|
<path d="m4.9 4.9 1.6 1.6" />
|
|
|
|
|
<path d="m17.5 17.5 1.6 1.6" />
|
|
|
|
|
<path d="M2.5 12h2.2" />
|
|
|
|
|
<path d="M19.3 12h2.2" />
|
|
|
|
|
<path d="m4.9 19.1 1.6-1.6" />
|
|
|
|
|
<path d="m17.5 6.5 1.6-1.6" />
|
|
|
|
|
</svg>
|
|
|
|
|
<svg
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
className="theme-toggle__moon h-3.5 w-3.5 fill-none stroke-current"
|
|
|
|
|
strokeWidth="1.7"
|
|
|
|
|
strokeLinecap="round"
|
|
|
|
|
strokeLinejoin="round"
|
|
|
|
|
>
|
|
|
|
|
<path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8Z" />
|
|
|
|
|
</svg>
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|