uptime-kuma/src/mixins/theme.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

export default {
data() {
return {
2021-08-12 14:17:20 +00:00
system: (window.matchMedia("(prefers-color-scheme: dark)").matches) ? "dark" : "light",
userTheme: localStorage.theme,
userHeartbeatBar: localStorage.heartbeatBarTheme,
2021-09-23 05:57:24 +00:00
statusPageTheme: "light",
forceStatusPageTheme: false,
2021-09-11 11:40:03 +00:00
path: "",
};
},
mounted() {
// Default Light
if (! this.userTheme) {
this.userTheme = "auto";
}
// Default Heartbeat Bar
if (!this.userHeartbeatBar) {
2021-08-15 18:46:21 +00:00
this.userHeartbeatBar = "normal";
}
document.body.classList.add(this.theme);
2021-08-12 16:23:40 +00:00
this.updateThemeColorMeta();
},
computed: {
theme() {
// As entry can be status page now, set forceStatusPageTheme to true to use status page theme
if (this.forceStatusPageTheme) {
return this.statusPageTheme;
}
2021-09-23 05:57:24 +00:00
// Entry no need dark
if (this.path === "") {
return "light";
}
2022-03-18 04:57:37 +00:00
if (this.path.startsWith("/status-page") || this.path.startsWith("/status")) {
2021-09-11 11:40:03 +00:00
return this.statusPageTheme;
} else {
if (this.userTheme === "auto") {
return this.system;
}
return this.userTheme;
}
}
},
watch: {
2021-09-11 11:40:03 +00:00
"$route.fullPath"(path) {
this.path = path;
},
userTheme(to, from) {
localStorage.theme = to;
},
theme(to, from) {
document.body.classList.remove(from);
document.body.classList.add(this.theme);
2021-08-12 16:23:40 +00:00
this.updateThemeColorMeta();
2021-08-15 18:46:21 +00:00
},
userHeartbeatBar(to, from) {
localStorage.heartbeatBarTheme = to;
},
heartbeatBarTheme(to, from) {
document.body.classList.remove(from);
document.body.classList.add(this.heartbeatBarTheme);
2021-08-12 16:23:40 +00:00
}
},
methods: {
/** Update the theme color meta tag */
2021-08-12 16:23:40 +00:00
updateThemeColorMeta() {
if (this.theme === "dark") {
document.querySelector("#theme-color").setAttribute("content", "#161B22");
} else {
document.querySelector("#theme-color").setAttribute("content", "#5cdd8b");
}
}
}
2021-09-23 05:57:24 +00:00
};