mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
95 lines
4.0 KiB
TypeScript
95 lines
4.0 KiB
TypeScript
export type ThemeMode = "system" | "light" | "dark";
|
|
|
|
import lightManifest from "../skins/builtin/light/manifest.json";
|
|
import lightCss from "../skins/builtin/light/skin.css?inline";
|
|
import darkManifest from "../skins/builtin/dark/manifest.json";
|
|
import darkCss from "../skins/builtin/dark/skin.css?inline";
|
|
|
|
const THEME_KEY = "webspeak:theme";
|
|
const SKIN_STYLE_ELEMENT_ID = "webspeak-active-built-in-skin";
|
|
const LIGHT_THEME_COLOR = "#006a64";
|
|
const DARK_THEME_COLOR = "#101918";
|
|
let activeThemeMode: ThemeMode = "system";
|
|
|
|
const builtinSkins = {
|
|
light: { manifest: lightManifest, css: lightCss },
|
|
dark: { manifest: darkManifest, css: darkCss },
|
|
} as const;
|
|
|
|
export function getBuiltinSkinCss(theme: Exclude<ThemeMode, "system">): string {
|
|
return theme === "dark" ? builtinSkins.dark.css : builtinSkins.light.css;
|
|
}
|
|
|
|
export function getStoredTheme(): ThemeMode {
|
|
let value: string | null = null;
|
|
try { value = typeof localStorage === "undefined" ? null : localStorage.getItem(THEME_KEY); }
|
|
catch { /* A restricted browser still has a usable system theme. */ }
|
|
return value === "light" || value === "dark" || value === "system" ? value : "system";
|
|
}
|
|
|
|
export function applyTheme(theme: ThemeMode, options: { preserveCustomSkins?: boolean } = {}): void {
|
|
if (typeof document === "undefined") return;
|
|
activeThemeMode = theme;
|
|
const root = document.documentElement;
|
|
const dark = isDarkTheme(theme);
|
|
const activeSkin = isDarkTheme(theme) ? builtinSkins.dark : builtinSkins.light;
|
|
|
|
// Keep data-theme for the admin console's independent appearance rules.
|
|
// Built-in skin CSS is scoped to public roots and can never restyle /admin.
|
|
root.dataset.theme = theme;
|
|
if (root.style) root.style.colorScheme = dark ? "dark" : "light";
|
|
if (typeof document.querySelector === "function" && typeof document.createElement === "function" && document.head) {
|
|
let themeColor = document.querySelector<HTMLMetaElement>('meta[name="theme-color"]');
|
|
if (!themeColor) {
|
|
themeColor = document.createElement("meta");
|
|
themeColor.name = "theme-color";
|
|
document.head.append(themeColor);
|
|
}
|
|
themeColor.content = dark ? DARK_THEME_COLOR : LIGHT_THEME_COLOR;
|
|
}
|
|
document.querySelectorAll<HTMLElement>(".ws-skin-root").forEach((clientRoot) => {
|
|
const currentSkin = clientRoot.dataset.wsSkin;
|
|
const isCustomSkin = currentSkin !== builtinSkins.light.manifest.id && currentSkin !== builtinSkins.dark.manifest.id;
|
|
if (options.preserveCustomSkins && currentSkin && isCustomSkin) return;
|
|
clientRoot.dataset.wsSkin = activeSkin.manifest.id;
|
|
});
|
|
|
|
let style = document.getElementById(SKIN_STYLE_ELEMENT_ID) as HTMLStyleElement | null;
|
|
if (!style) {
|
|
style = document.createElement("style");
|
|
style.id = SKIN_STYLE_ELEMENT_ID;
|
|
style.dataset.skinPackage = activeSkin.manifest.id;
|
|
document.head.append(style);
|
|
}
|
|
style.dataset.skinPackage = activeSkin.manifest.id;
|
|
style.textContent = activeSkin.css;
|
|
}
|
|
|
|
export function saveTheme(theme: ThemeMode, options: { preserveCustomSkins?: boolean } = {}): void {
|
|
if (typeof localStorage !== "undefined") localStorage.setItem(THEME_KEY, theme);
|
|
applyTheme(theme, options);
|
|
}
|
|
|
|
export function isDarkTheme(theme: ThemeMode): boolean {
|
|
if (theme === "dark") return true;
|
|
if (theme === "light") return false;
|
|
return typeof window !== "undefined" && typeof window.matchMedia === "function"
|
|
? window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
: false;
|
|
}
|
|
|
|
export function nextTheme(theme: ThemeMode): ThemeMode {
|
|
return isDarkTheme(theme) ? "light" : "dark";
|
|
}
|
|
|
|
applyTheme(getStoredTheme());
|
|
|
|
if (typeof window !== "undefined" && typeof window.matchMedia === "function") {
|
|
const systemColorScheme = window.matchMedia("(prefers-color-scheme: dark)");
|
|
const updateSystemSkin = () => {
|
|
if (activeThemeMode === "system") applyTheme("system", { preserveCustomSkins: true });
|
|
};
|
|
if (systemColorScheme.addEventListener) systemColorScheme.addEventListener("change", updateSystemSkin);
|
|
else systemColorScheme.addListener?.(updateSystemSkin);
|
|
}
|