@jay-js/system
Version:
A powerful and flexible TypeScript library for UI, state management, lazy loading, routing and managing draggable elements in modern web applications.
27 lines (26 loc) • 826 B
JavaScript
import { themeOptions } from "./themeDefineOptions.js";
export function setTheme(theme) {
if (themeOptions.useAsDataset) {
themeOptions.target.dataset.theme = theme;
}
if (themeOptions.useAsClass) {
themeOptions.target.classList.remove(...themeOptions.themeList || [
themeOptions.defaultTheme,
themeOptions.defaultDarkTheme
]);
themeOptions.target.classList.add(theme);
}
if (themeOptions.saveToLocalStorage) {
try {
localStorage.setItem("theme", theme);
} catch (e) {
console.warn("Failed to save theme to localStorage:", e);
}
}
const themeChangedEvent = new CustomEvent("themeChanged", {
detail: {
theme
}
});
document.dispatchEvent(themeChangedEvent);
}