Work on frontend and fix the theme_switcher bug

This commit is contained in:
kitoy 2025-12-16 08:19:34 +01:00
parent c4e1318722
commit c3da453425
2 changed files with 71 additions and 20 deletions

View File

@ -22,11 +22,13 @@ body
header { header {
grid-area: header; grid-area: header;
width: 100%;
} }
main body > main
{ {
grid-area: main; grid-area: main;
width: 80%;
text-align: justify; text-align: justify;
overflow-y: scroll; overflow-y: scroll;
} }

View File

@ -2,34 +2,83 @@
* Minimal theme switcher * Minimal theme switcher
* *
* Pico.css - https://picocss.com * Pico.css - https://picocss.com
* Copyright 2020 - Licensed under MIT * Copyright 2019-2023 - Licensed under MIT
*/ */
const themeSwitcher = { const themeSwitcher = {
// Config // Config
_scheme: "auto",
menuTarget: "details[role='list']",
buttonsTarget: "a[data-theme-switcher]", buttonsTarget: "a[data-theme-switcher]",
buttonAttribute: "data-theme-switcher", buttonAttribute: "data-theme-switcher",
rootAttribute: "data-theme", rootAttribute: "data-theme",
localStorageKey: "picoPreferredColorScheme",
// Init // Init
init() { init() {
document.querySelectorAll(this.buttonsTarget).forEach( this.scheme = this.schemeFromLocalStorage;
function (button) { this.initSwitchers();
button.addEventListener( },
"click",
function (event) { // Get color scheme from local storage
event.preventDefault(); get schemeFromLocalStorage() {
document if (typeof window.localStorage !== "undefined") {
.querySelector("html") if (window.localStorage.getItem(this.localStorageKey) !== null) {
.setAttribute( return window.localStorage.getItem(this.localStorageKey);
this.rootAttribute, }
event.target.getAttribute(this.buttonAttribute) }
); return this._scheme;
}.bind(this), },
false
); // Preferred color scheme
}.bind(this) get preferredColorScheme() {
); return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
},
// Init switchers
initSwitchers() {
const buttons = document.querySelectorAll(this.buttonsTarget);
buttons.forEach((button) => {
button.addEventListener(
"click",
(event) => {
event.preventDefault();
// Set scheme
this.scheme = button.getAttribute(this.buttonAttribute);
// Close dropdown
document.querySelector(this.menuTarget).removeAttribute("open");
},
false
);
});
},
// Set scheme
set scheme(scheme) {
if (scheme == "auto") {
this.preferredColorScheme == "dark" ? (this._scheme = "dark") : (this._scheme = "light");
} else if (scheme == "dark" || scheme == "light") {
this._scheme = scheme;
}
this.applyScheme();
this.schemeToLocalStorage();
},
// Get scheme
get scheme() {
return this._scheme;
},
// Apply scheme
applyScheme() {
document.querySelector("html").setAttribute(this.rootAttribute, this.scheme);
},
// Store scheme to local storage
schemeToLocalStorage() {
if (typeof window.localStorage !== "undefined") {
window.localStorage.setItem(this.localStorageKey, this.scheme);
}
}, },
}; };