You've already forked Atomcms-edit
Initial commit
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
const ThemeSwitcher = {
|
||||
currentTheme: "light",
|
||||
|
||||
init() {
|
||||
if (
|
||||
localStorage.theme === "dark" ||
|
||||
(typeof window.matchMedia != "undefined" &&
|
||||
window.matchMedia("(prefers-color-scheme: dark)").matches &&
|
||||
localStorage.theme !== "light")
|
||||
) {
|
||||
this.toggleTheme();
|
||||
}
|
||||
|
||||
document.addEventListener("turbolinks:load", () => this.initButton());
|
||||
},
|
||||
|
||||
initButton() {
|
||||
let themeSwitcher = document.getElementById("theme-switcher");
|
||||
|
||||
themeSwitcher?.addEventListener("click", () => this.toggleTheme());
|
||||
},
|
||||
|
||||
toggleTheme() {
|
||||
if (this.currentTheme === "light") {
|
||||
this.currentTheme = "dark";
|
||||
document.documentElement.classList.add("dark");
|
||||
} else {
|
||||
this.currentTheme = "light";
|
||||
document.documentElement.classList.remove("dark");
|
||||
}
|
||||
|
||||
localStorage.setItem("theme", this.currentTheme);
|
||||
},
|
||||
};
|
||||
|
||||
export { ThemeSwitcher as default };
|
||||
Reference in New Issue
Block a user