mirror of
https://github.com/moku-project/Moku.git
synced 2026-06-14 01:39:56 -05:00
Chore: Fix Zoom & Attempt Theming
This commit is contained in:
+38
-49
@@ -1,67 +1,56 @@
|
||||
import { settingsState, updateSettings } from "$lib/state/settings.svelte";
|
||||
import type { CustomTheme } from '$lib/types/settings'
|
||||
|
||||
let themeStyleEl: HTMLStyleElement | null = null;
|
||||
let mediaQuery: MediaQueryList | null = null;
|
||||
let mediaHandler: (() => void) | null = null;
|
||||
let themeStyleEl: HTMLStyleElement | null = null
|
||||
let mediaQuery: MediaQueryList | null = null
|
||||
let mediaHandler: (() => void) | null = null
|
||||
|
||||
export function applyTheme() {
|
||||
const themeId = settingsState.theme ?? "dark";
|
||||
const isCustom = themeId.startsWith("custom:");
|
||||
export function applyTheme(themeId: string, customThemes: CustomTheme[] = []) {
|
||||
const custom = customThemes.find(t => t.id === themeId)
|
||||
|
||||
if (!isCustom) {
|
||||
themeStyleEl?.remove();
|
||||
themeStyleEl = null;
|
||||
document.documentElement.setAttribute("data-theme", themeId);
|
||||
return;
|
||||
if (custom) {
|
||||
const vars = Object.entries(custom.tokens)
|
||||
.map(([k, v]) => ` --${k}: ${v};`)
|
||||
.join('\n')
|
||||
if (!themeStyleEl) {
|
||||
themeStyleEl = document.createElement('style')
|
||||
themeStyleEl.id = 'moku-custom-theme'
|
||||
document.head.appendChild(themeStyleEl)
|
||||
}
|
||||
themeStyleEl.textContent = `:root {\n${vars}\n}`
|
||||
document.documentElement.removeAttribute('data-theme')
|
||||
return
|
||||
}
|
||||
|
||||
const custom = settingsState.customThemes?.find(t => t.id === themeId);
|
||||
if (!custom) {
|
||||
themeStyleEl?.remove();
|
||||
themeStyleEl = null;
|
||||
document.documentElement.setAttribute("data-theme", "dark");
|
||||
return;
|
||||
if (themeStyleEl) {
|
||||
themeStyleEl.remove()
|
||||
themeStyleEl = null
|
||||
}
|
||||
|
||||
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");
|
||||
document.documentElement.setAttribute('data-theme', themeId)
|
||||
}
|
||||
|
||||
function applySystemTheme(dark: boolean) {
|
||||
const themeId = dark
|
||||
? (settingsState.systemThemeDark ?? "dark")
|
||||
: (settingsState.systemThemeLight ?? "light");
|
||||
updateSettings({ theme: themeId });
|
||||
}
|
||||
|
||||
export function mountSystemThemeSync() {
|
||||
export function mountSystemThemeSync(
|
||||
enabled: boolean,
|
||||
darkTheme: string,
|
||||
lightTheme: string,
|
||||
onSwitch: (themeId: string) => void
|
||||
) {
|
||||
if (mediaQuery && mediaHandler) {
|
||||
mediaQuery.removeEventListener("change", mediaHandler);
|
||||
mediaHandler = null;
|
||||
mediaQuery.removeEventListener('change', mediaHandler)
|
||||
mediaHandler = null
|
||||
}
|
||||
|
||||
if (!settingsState.systemThemeSync) return;
|
||||
if (!enabled) { mediaQuery = null; return }
|
||||
|
||||
mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
mediaHandler = () => applySystemTheme(mediaQuery!.matches);
|
||||
mediaQuery.addEventListener("change", mediaHandler);
|
||||
applySystemTheme(mediaQuery.matches);
|
||||
mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
mediaHandler = () => onSwitch(mediaQuery!.matches ? darkTheme : lightTheme)
|
||||
mediaQuery.addEventListener('change', mediaHandler)
|
||||
onSwitch(mediaQuery.matches ? darkTheme : lightTheme)
|
||||
}
|
||||
|
||||
export function unmountSystemThemeSync() {
|
||||
if (mediaQuery && mediaHandler) {
|
||||
mediaQuery.removeEventListener("change", mediaHandler);
|
||||
mediaHandler = null;
|
||||
mediaQuery = null;
|
||||
mediaQuery.removeEventListener('change', mediaHandler)
|
||||
mediaHandler = null
|
||||
mediaQuery = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user