picocss/docs/js/src/theme-switcher.js

102 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-07-09 17:42:10 +07:00
/*
* Theme switcher
*
* Pico.css - https://picocss.com
2023-01-28 13:14:38 +07:00
* Copyright 2019-2023 - Licensed under MIT
2021-07-09 17:42:10 +07:00
*/
export const themeSwitcher = {
// Config
2024-01-27 13:53:19 +07:00
_scheme: "auto",
2021-07-09 18:18:39 +07:00
change: {
2024-01-27 13:53:19 +07:00
light: "<i>Turn on dark mode</i>",
dark: "<i>Turn off dark mode</i>",
2021-07-09 17:42:10 +07:00
},
2024-01-27 13:53:19 +07:00
buttonsTarget: ".theme-switcher",
localStorageKey: "picoPreferredColorScheme",
2021-07-09 18:18:39 +07:00
2021-07-09 17:42:10 +07:00
// Init
init() {
2024-01-27 13:53:19 +07:00
this.scheme = this.schemeFromLocalStorage
this.initSwitchers()
2021-07-09 17:42:10 +07:00
},
// Get color scheme from local storage
get schemeFromLocalStorage() {
2024-01-27 13:53:19 +07:00
if (typeof window.localStorage !== "undefined") {
if (window.localStorage.getItem(this.localStorageKey) !== null) {
2024-01-27 13:53:19 +07:00
return window.localStorage.getItem(this.localStorageKey)
}
}
2024-01-27 13:53:19 +07:00
return this._scheme
},
// Preferred color scheme
get preferredColorScheme() {
2024-01-27 13:53:19 +07:00
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light"
2021-07-09 17:42:10 +07:00
},
// Init switchers
initSwitchers() {
2024-01-27 13:53:19 +07:00
const buttons = document.querySelectorAll(this.buttonsTarget)
buttons.forEach(button => {
2024-01-27 13:53:19 +07:00
button.addEventListener(
"click",
() => {
this.scheme == "dark"
? (this.scheme = "light")
: (this.scheme = "dark")
},
false
)
})
2021-07-09 17:42:10 +07:00
},
// Add new button
addButton(config) {
2024-01-27 13:53:19 +07:00
let button = document.createElement(config.tag)
button.className = config.class
document.querySelector(config.target).appendChild(button)
2021-07-09 17:42:10 +07:00
},
// Set scheme
set scheme(scheme) {
2024-01-27 13:53:19 +07:00
if (scheme == "auto") {
this.preferredColorScheme == "dark"
? (this._scheme = "dark")
: (this._scheme = "light")
} else if (scheme == "dark" || scheme == "light") {
this._scheme = scheme
2021-07-09 17:42:10 +07:00
}
2024-01-27 13:53:19 +07:00
this.applyScheme()
this.schemeToLocalStorage()
2021-07-09 17:42:10 +07:00
},
2021-07-09 18:18:39 +07:00
// Get scheme
get scheme() {
2024-01-27 13:53:19 +07:00
return this._scheme
2021-07-09 18:18:39 +07:00
},
2021-07-09 17:42:10 +07:00
// Apply scheme
applyScheme() {
2024-01-27 13:53:19 +07:00
document.querySelector("html").setAttribute("data-theme", this.scheme)
const buttons = document.querySelectorAll(this.buttonsTarget)
buttons.forEach(button => {
const text = this.scheme == "dark" ? this.change.dark : this.change.light
button.innerHTML = text
button.setAttribute("aria-label", text.replace(/<[^>]*>?/gm, ""))
})
2021-10-24 12:33:20 +07:00
},
// Store scheme to local storage
schemeToLocalStorage() {
2024-01-27 13:53:19 +07:00
if (typeof window.localStorage !== "undefined") {
window.localStorage.setItem(this.localStorageKey, this.scheme)
}
},
2024-01-27 13:53:19 +07:00
}
2021-10-23 12:17:04 +07:00
2024-01-27 13:53:19 +07:00
export default themeSwitcher