2021-07-09 17:42:10 +07:00
|
|
|
/*
|
|
|
|
* Theme switcher
|
|
|
|
*
|
|
|
|
* Pico.css - https://picocss.com
|
|
|
|
* Copyright 2019-2021 - Licensed under MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
export const themeSwitcher = {
|
|
|
|
|
|
|
|
// Config
|
2021-07-09 18:18:39 +07:00
|
|
|
_scheme: 'auto',
|
|
|
|
change: {
|
2021-07-09 17:42:10 +07:00
|
|
|
light: '<i>Turn on dark mode</i>',
|
|
|
|
dark: '<i>Turn off dark mode</i>'
|
|
|
|
},
|
2021-07-09 18:18:39 +07:00
|
|
|
buttonsTarget: '.theme-switcher',
|
|
|
|
|
2021-07-09 17:42:10 +07:00
|
|
|
|
|
|
|
|
|
|
|
// Init
|
|
|
|
init() {
|
|
|
|
this.scheme = this._scheme;
|
|
|
|
this.initSwitchers();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Prefered color scheme
|
|
|
|
get preferedColorScheme() {
|
|
|
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
|
|
return 'dark';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'light';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Init switchers
|
|
|
|
initSwitchers() {
|
2021-07-09 18:18:39 +07:00
|
|
|
const buttons = document.querySelectorAll(this.buttonsTarget);
|
2021-07-09 17:42:10 +07:00
|
|
|
buttons.forEach(function(button) {
|
|
|
|
button.addEventListener('click', function(event) {
|
2021-07-09 18:18:39 +07:00
|
|
|
if (this.scheme == 'dark') {
|
2021-07-09 17:42:10 +07:00
|
|
|
this.scheme = 'light';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.scheme = 'dark';
|
|
|
|
}
|
|
|
|
}.bind(this), false);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Add new button
|
|
|
|
addButton(config) {
|
|
|
|
|
|
|
|
// Insert Switcher
|
|
|
|
let button = document.createElement(config.tag);
|
|
|
|
button.className = config.class;
|
|
|
|
document.querySelector(config.target).appendChild(button);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Set scheme
|
|
|
|
set scheme(scheme) {
|
|
|
|
|
|
|
|
if (scheme == 'auto') {
|
|
|
|
if (this.preferedColorScheme == 'dark') {
|
|
|
|
this._scheme = 'dark';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._scheme = 'light';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set to Dark
|
|
|
|
else if (scheme == 'dark' || scheme == 'light') {
|
|
|
|
this._scheme = scheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set to Apply theme
|
|
|
|
this.applyScheme();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2021-07-09 18:18:39 +07:00
|
|
|
// Get scheme
|
|
|
|
get scheme() {
|
|
|
|
return this._scheme;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2021-07-09 17:42:10 +07:00
|
|
|
// Apply scheme
|
|
|
|
applyScheme() {
|
|
|
|
|
|
|
|
// Root attribute
|
2021-07-09 18:18:39 +07:00
|
|
|
document.querySelector('html').setAttribute('data-theme', this.scheme);
|
2021-07-09 17:42:10 +07:00
|
|
|
|
|
|
|
// Buttons text
|
2021-07-09 18:18:39 +07:00
|
|
|
const buttons = document.querySelectorAll(this.buttonsTarget);
|
2021-07-09 17:42:10 +07:00
|
|
|
let text;
|
|
|
|
buttons.forEach(function(button) {
|
2021-07-09 18:18:39 +07:00
|
|
|
if (this.scheme == 'dark') {
|
|
|
|
text = this.change.dark;
|
2021-07-09 17:42:10 +07:00
|
|
|
}
|
|
|
|
else {
|
2021-07-09 18:18:39 +07:00
|
|
|
text = this.change.light;
|
2021-07-09 17:42:10 +07:00
|
|
|
}
|
|
|
|
button.innerHTML = text;
|
|
|
|
button.setAttribute('aria-label', text.replace(/<[^>]*>?/gm, ''));
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 12:17:04 +07:00
|
|
|
|
|
|
|
export default themeSwitcher;
|