mirror of
https://github.com/moku-project/Moku.git
synced 2026-06-13 01:09:56 -05:00
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { store } from "@store/state.svelte";
|
|
|
|
let themeStyleEl: HTMLStyleElement | null = null;
|
|
|
|
export function applyTheme() {
|
|
const themeId = store.settings.theme ?? "dark";
|
|
const isCustom = themeId.startsWith("custom:");
|
|
|
|
if (!isCustom) {
|
|
themeStyleEl?.remove();
|
|
themeStyleEl = null;
|
|
document.documentElement.setAttribute("data-theme", themeId);
|
|
return;
|
|
}
|
|
|
|
const custom = store.settings.customThemes?.find(t => t.id === themeId);
|
|
if (!custom) {
|
|
themeStyleEl?.remove();
|
|
themeStyleEl = null;
|
|
document.documentElement.setAttribute("data-theme", "dark");
|
|
return;
|
|
}
|
|
|
|
const vars = Object.entries(custom.tokens)
|
|
.map(([k, v]) => ` --${k}: ${v};`)
|
|
.join("\n");
|
|
const css = `[data-theme="custom"] {\n${vars}\n}`;
|
|
|
|
if (!themeStyleEl) {
|
|
themeStyleEl = document.createElement("style");
|
|
themeStyleEl.id = "moku-custom-theme";
|
|
document.head.appendChild(themeStyleEl);
|
|
}
|
|
themeStyleEl.textContent = css;
|
|
document.documentElement.setAttribute("data-theme", "custom");
|
|
}
|