2023-03-22 20:29:17 +03:00
|
|
|
<!-- Switch the mode between dark and light. -->
|
2020-02-11 19:04:59 +03:00
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
class ModeToggle {
|
2023-03-22 20:29:17 +03:00
|
|
|
static get MODE_KEY() {
|
|
|
|
return 'mode';
|
|
|
|
}
|
|
|
|
static get MODE_ATTR() {
|
|
|
|
return 'data-mode';
|
|
|
|
}
|
|
|
|
static get DARK_MODE() {
|
|
|
|
return 'dark';
|
|
|
|
}
|
|
|
|
static get LIGHT_MODE() {
|
|
|
|
return 'light';
|
|
|
|
}
|
|
|
|
static get ID() {
|
|
|
|
return 'mode-toggle';
|
|
|
|
}
|
2020-02-11 19:04:59 +03:00
|
|
|
|
|
|
|
constructor() {
|
2020-12-10 21:20:18 +03:00
|
|
|
if (this.hasMode) {
|
|
|
|
if (this.isDarkMode) {
|
2020-04-01 19:30:00 +03:00
|
|
|
if (!this.isSysDarkPrefer) {
|
2020-02-14 18:36:38 +03:00
|
|
|
this.setDark();
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-01 19:30:00 +03:00
|
|
|
if (this.isSysDarkPrefer) {
|
2020-02-14 18:36:38 +03:00
|
|
|
this.setLight();
|
|
|
|
}
|
2020-02-11 19:04:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 14:51:25 +03:00
|
|
|
let self = this;
|
2020-02-11 19:04:59 +03:00
|
|
|
|
2020-02-14 18:36:38 +03:00
|
|
|
/* always follow the system prefers */
|
2023-03-13 15:20:59 +03:00
|
|
|
this.sysDarkPrefers.addEventListener('change', () => {
|
2020-12-09 21:42:46 +03:00
|
|
|
if (self.hasMode) {
|
|
|
|
if (self.isDarkMode) {
|
2020-04-01 19:30:00 +03:00
|
|
|
if (!self.isSysDarkPrefer) {
|
2020-02-14 18:36:38 +03:00
|
|
|
self.setDark();
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-01 19:30:00 +03:00
|
|
|
if (self.isSysDarkPrefer) {
|
2020-02-14 18:36:38 +03:00
|
|
|
self.setLight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.clearMode();
|
2020-02-11 19:04:59 +03:00
|
|
|
}
|
2020-12-09 21:42:46 +03:00
|
|
|
|
2021-12-12 21:37:10 +03:00
|
|
|
self.notify();
|
2020-02-11 19:04:59 +03:00
|
|
|
});
|
|
|
|
} /* constructor() */
|
|
|
|
|
2023-03-22 20:29:17 +03:00
|
|
|
get sysDarkPrefers() {
|
|
|
|
return window.matchMedia('(prefers-color-scheme: dark)');
|
|
|
|
}
|
2020-02-11 19:04:59 +03:00
|
|
|
|
2023-03-22 20:29:17 +03:00
|
|
|
get isSysDarkPrefer() {
|
|
|
|
return this.sysDarkPrefers.matches;
|
|
|
|
}
|
2020-02-11 19:04:59 +03:00
|
|
|
|
2023-03-22 20:29:17 +03:00
|
|
|
get isDarkMode() {
|
|
|
|
return this.mode === ModeToggle.DARK_MODE;
|
|
|
|
}
|
2020-04-01 19:30:00 +03:00
|
|
|
|
2023-03-22 20:29:17 +03:00
|
|
|
get isLightMode() {
|
|
|
|
return this.mode === ModeToggle.LIGHT_MODE;
|
|
|
|
}
|
2020-04-01 19:30:00 +03:00
|
|
|
|
2023-03-22 20:29:17 +03:00
|
|
|
get hasMode() {
|
|
|
|
return this.mode != null;
|
|
|
|
}
|
2020-04-01 19:30:00 +03:00
|
|
|
|
2023-03-22 20:29:17 +03:00
|
|
|
get mode() {
|
|
|
|
return sessionStorage.getItem(ModeToggle.MODE_KEY);
|
|
|
|
}
|
2020-02-11 19:04:59 +03:00
|
|
|
|
2020-12-09 21:42:46 +03:00
|
|
|
/* get the current mode on screen */
|
|
|
|
get modeStatus() {
|
2023-03-13 15:20:59 +03:00
|
|
|
if (this.isDarkMode || (!this.hasMode && this.isSysDarkPrefer)) {
|
2020-12-09 21:42:46 +03:00
|
|
|
return ModeToggle.DARK_MODE;
|
|
|
|
} else {
|
|
|
|
return ModeToggle.LIGHT_MODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 14:51:25 +03:00
|
|
|
setDark() {
|
2023-03-22 20:29:17 +03:00
|
|
|
document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.DARK_MODE);
|
2021-12-01 14:51:25 +03:00
|
|
|
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
setLight() {
|
2023-03-22 20:29:17 +03:00
|
|
|
document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.LIGHT_MODE);
|
2021-12-01 14:51:25 +03:00
|
|
|
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearMode() {
|
2023-03-22 20:29:17 +03:00
|
|
|
document.documentElement.removeAttribute(ModeToggle.MODE_ATTR);
|
2021-12-01 14:51:25 +03:00
|
|
|
sessionStorage.removeItem(ModeToggle.MODE_KEY);
|
|
|
|
}
|
|
|
|
|
2021-12-12 21:37:10 +03:00
|
|
|
/* Notify another plugins that the theme mode has changed */
|
|
|
|
notify() {
|
2023-03-22 20:29:17 +03:00
|
|
|
window.postMessage(
|
|
|
|
{
|
|
|
|
direction: ModeToggle.ID,
|
|
|
|
message: this.modeStatus
|
|
|
|
},
|
|
|
|
'*'
|
|
|
|
);
|
2020-12-09 21:42:46 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
flipMode() {
|
|
|
|
if (this.hasMode) {
|
|
|
|
if (this.isSysDarkPrefer) {
|
|
|
|
if (this.isLightMode) {
|
|
|
|
this.clearMode();
|
|
|
|
} else {
|
|
|
|
this.setLight();
|
|
|
|
}
|
2020-04-01 19:30:00 +03:00
|
|
|
} else {
|
2023-03-13 15:20:59 +03:00
|
|
|
if (this.isDarkMode) {
|
|
|
|
this.clearMode();
|
|
|
|
} else {
|
|
|
|
this.setDark();
|
|
|
|
}
|
2020-04-01 19:30:00 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-13 15:20:59 +03:00
|
|
|
if (this.isSysDarkPrefer) {
|
|
|
|
this.setLight();
|
2020-04-01 19:30:00 +03:00
|
|
|
} else {
|
2023-03-13 15:20:59 +03:00
|
|
|
this.setDark();
|
2020-04-01 19:30:00 +03:00
|
|
|
}
|
2020-02-11 19:04:59 +03:00
|
|
|
}
|
2020-04-01 19:30:00 +03:00
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
this.notify();
|
|
|
|
} /* flipMode() */
|
|
|
|
} /* ModeToggle */
|
2021-11-30 19:23:22 +03:00
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
const modeToggle = new ModeToggle();
|
2020-10-23 10:42:43 +03:00
|
|
|
</script>
|