web/_includes/mode-toggle.html

130 lines
2.9 KiB
HTML
Raw Normal View History

<!--
Switch the mode between dark and light.
-->
<script type="text/javascript">
class ModeToggle {
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"; }
2021-12-12 21:37:10 +03:00
static get ID() { return "mode-toggle"; }
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();
}
}
}
let self = this;
2020-02-14 18:36:38 +03:00
/* always follow the system prefers */
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-12-09 21:42:46 +03:00
2021-12-12 21:37:10 +03:00
self.notify();
});
} /* constructor() */
2020-04-01 19:30:00 +03:00
get sysDarkPrefers() { return window.matchMedia("(prefers-color-scheme: dark)"); }
2020-04-01 19:30:00 +03:00
get isSysDarkPrefer() { return this.sysDarkPrefers.matches; }
get isDarkMode() { return this.mode === ModeToggle.DARK_MODE; }
2020-04-01 19:30:00 +03:00
get isLightMode() { return this.mode === ModeToggle.LIGHT_MODE; }
2020-04-01 19:30:00 +03:00
get hasMode() { return this.mode != null; }
get mode() { return sessionStorage.getItem(ModeToggle.MODE_KEY); }
2020-12-09 21:42:46 +03:00
/* get the current mode on screen */
get modeStatus() {
if (this.isDarkMode
|| (!this.hasMode && this.isSysDarkPrefer)) {
2020-12-09 21:42:46 +03:00
return ModeToggle.DARK_MODE;
} else {
return ModeToggle.LIGHT_MODE;
}
}
setDark() {
$('html').attr(ModeToggle.MODE_ATTR, ModeToggle.DARK_MODE);
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
}
setLight() {
$('html').attr(ModeToggle.MODE_ATTR, ModeToggle.LIGHT_MODE);
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
}
clearMode() {
$('html').removeAttr(ModeToggle.MODE_ATTR);
sessionStorage.removeItem(ModeToggle.MODE_KEY);
}
2021-12-12 21:37:10 +03:00
/* Notify another plugins that the theme mode has changed */
notify() {
window.postMessage({
direction: ModeToggle.ID,
message: this.modeStatus
}, "*");
2020-12-09 21:42:46 +03:00
}
} /* ModeToggle */
const toggle = new ModeToggle();
2020-02-14 18:36:38 +03:00
function flipMode() {
if (toggle.hasMode) {
if (toggle.isSysDarkPrefer) {
if (toggle.isLightMode) {
toggle.clearMode();
2020-04-01 19:30:00 +03:00
} else {
toggle.setLight();
2020-04-01 19:30:00 +03:00
}
} else {
if (toggle.isDarkMode) {
toggle.clearMode();
2020-04-01 19:30:00 +03:00
} else {
toggle.setDark();
2020-04-01 19:30:00 +03:00
}
}
2020-04-01 19:30:00 +03:00
} else {
if (toggle.isSysDarkPrefer) {
toggle.setLight();
} else {
toggle.setDark();
}
}
2020-12-09 21:42:46 +03:00
2021-12-12 21:37:10 +03:00
toggle.notify();
} /* flipMode() */
2020-10-23 10:42:43 +03:00
</script>