100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
// Light/dark theme with system default, persistence, and no-flash startup.
|
|
// index.html applies the stored/system theme before first paint. Until the user
|
|
// chooses explicitly, this module continues following system changes.
|
|
export type Theme = "light" | "dark";
|
|
|
|
export const THEME_STORAGE_KEY = "piik:ui-theme";
|
|
const DARK_THEME_QUERY = "(prefers-color-scheme: dark)";
|
|
|
|
function detectTheme(): Theme {
|
|
return typeof window !== "undefined" &&
|
|
window.matchMedia?.(DARK_THEME_QUERY).matches
|
|
? "dark"
|
|
: "light";
|
|
}
|
|
|
|
function readInitial(): Theme {
|
|
const attr = document.documentElement.dataset.theme;
|
|
if (attr === "dark" || attr === "light") {
|
|
return attr;
|
|
}
|
|
return detectTheme();
|
|
}
|
|
|
|
function storedTheme(): Theme | null {
|
|
try {
|
|
const value = window.localStorage.getItem(THEME_STORAGE_KEY);
|
|
return value === "light" || value === "dark" ? value : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const state: { theme: Theme; explicit: boolean; initialized: boolean } = {
|
|
theme: "light",
|
|
explicit: false,
|
|
initialized: false,
|
|
};
|
|
const listeners = new Set<() => void>();
|
|
let followsWindowTheme = false;
|
|
|
|
function commitTheme(theme: Theme): void {
|
|
if (state.theme === theme) return;
|
|
state.theme = theme;
|
|
document.documentElement.dataset.theme = theme;
|
|
listeners.forEach((listener) => listener());
|
|
}
|
|
|
|
/** A native utility window follows the main window without saving a site preference. */
|
|
export function applyWindowTheme(theme: Theme): void {
|
|
followsWindowTheme = true;
|
|
commitTheme(theme);
|
|
}
|
|
|
|
export function applyTheme(theme: Theme): void {
|
|
followsWindowTheme = false;
|
|
state.explicit = true;
|
|
commitTheme(theme);
|
|
try {
|
|
window.localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
} catch {
|
|
// Restricted storage only disables persistence.
|
|
}
|
|
}
|
|
|
|
export function currentThemePreference(): Theme | null {
|
|
return state.explicit && !followsWindowTheme ? state.theme : null;
|
|
}
|
|
|
|
export function initTheme(preference?: Theme | null, explicitChoice = true): void {
|
|
const committed = explicitChoice && preference !== undefined;
|
|
if (committed) {
|
|
try {
|
|
if (preference === null) window.localStorage.removeItem(THEME_STORAGE_KEY);
|
|
else window.localStorage.setItem(THEME_STORAGE_KEY, preference);
|
|
} catch {
|
|
// The launch choice still applies when persistence is unavailable.
|
|
}
|
|
}
|
|
const stored = committed ? preference
|
|
: state.explicit ? state.theme : storedTheme();
|
|
state.explicit = stored !== null;
|
|
state.theme = stored ?? preference ?? (preference === null ? detectTheme() : readInitial());
|
|
document.documentElement.dataset.theme = state.theme;
|
|
if (!state.initialized && typeof window.matchMedia === "function") {
|
|
state.initialized = true;
|
|
window.matchMedia(DARK_THEME_QUERY).addEventListener("change", (event) => {
|
|
if (!state.explicit && !followsWindowTheme) commitTheme(event.matches ? "dark" : "light");
|
|
});
|
|
}
|
|
}
|
|
|
|
export function currentTheme(): Theme {
|
|
return state.theme;
|
|
}
|
|
|
|
export function subscribeTheme(listener: () => void): () => void {
|
|
listeners.add(listener);
|
|
return () => { listeners.delete(listener); };
|
|
}
|