picocss/docs/js/SwitchColorMode.js

77 lines
1.8 KiB
JavaScript
Raw Normal View History

/*!
2025-01-11 02:16:13 -05:00
* Minimal theme switcher using a checkbox
*
* Pico.css - https://picocss.com
* Copyright 2019-2024 - Licensed under MIT
2025-01-11 02:16:13 -05:00
* Modified by Yohn https://github.com/Yohn/PicoCSS
*/
2025-01-11 02:16:13 -05:00
const SwitchColorMode = {
// Config
_scheme: "auto",
2025-01-11 02:16:13 -05:00
toggleSelector: "input[name='color-mode-toggle']",
rootAttribute: "data-theme",
localStorageKey: "picoPreferredColorScheme",
// Init
init() {
2025-01-11 02:16:13 -05:00
this.checkbox = document.querySelector(this.toggleSelector);
if (!this.checkbox) {
console.error("Theme switcher toggle not found");
return;
}
this.scheme = this.schemeFromLocalStorage;
2025-01-11 02:16:13 -05:00
this.initToggle();
},
// Get color scheme from local storage
get schemeFromLocalStorage() {
return window.localStorage?.getItem(this.localStorageKey) ?? this._scheme;
},
// Preferred color scheme
get preferredColorScheme() {
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
},
2025-01-11 02:16:13 -05:00
// Initialize the toggle switch
initToggle() {
// Set initial checkbox state based on the current scheme
this.checkbox.checked = this.scheme === "dark";
// Listen for changes to the checkbox
this.checkbox.addEventListener("change", () => {
this.scheme = this.checkbox.checked ? "dark" : "light";
});
},
// Set scheme
set scheme(scheme) {
2025-01-11 02:16:13 -05:00
if (scheme === "auto") {
this._scheme = this.preferredColorScheme;
2025-01-11 02:16:13 -05:00
} 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() {
window.localStorage?.setItem(this.localStorageKey, this.scheme);
},
};
// Init
2025-01-11 02:16:13 -05:00
SwitchColorMode.init();