π A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!
Vue awesomedashboarddockerhomelabhomepagemitnodejsorganizationproductivitypwaself-hostedstartpagevue
Add auto dark/light theme switch based on OS preference
Vishwanath Marturc2bad0b0a921
diff --git a/src/App.vue b/src/App.vueindex a7529776..3ec5a054 100644--- a/src/App.vue+++ b/src/App.vue@@ -155,11 +155,21 @@ export default {e.preventDefault();return 'You may have unsaved edits. Are you sure you want to exit the page?';},+ /* Detect and apply theme based on OS preference */+ applyThemeBasedOnOSPreference() {+ const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;+ const osTheme = prefersDark ? this.appConfig.nightTheme : this.appConfig.dayTheme;+ if (osTheme) {+ this.$store.commit(Keys.SET_THEME, osTheme);+ this.updateTheme(osTheme);+ }+ },},/* Basic initialization tasks on app load */async mounted() {await this.$store.dispatch(Keys.INITIALIZE_CONFIG); // Initialize config before moving onthis.applyLanguage(); // Apply users local language+ this.applyThemeBasedOnOSPreference(); // Apply theme based on OS preferencethis.hideSplash(); // Hide the splash screen, if visibleif (this.appConfig.customCss) { // Inject users custom CSS, if presentconst cleanedCss = this.appConfig.customCss.replace(/<\/?[^>]+(>|$)/g, '');diff --git a/src/mixins/ThemingMixin.js b/src/mixins/ThemingMixin.jsindex 64e21e7d..7659f351 100644--- a/src/mixins/ThemingMixin.js+++ b/src/mixins/ThemingMixin.js@@ -136,6 +136,14 @@ const ThemingMixin = {} else if (hasExternal) {this.applyRemoteTheme(this.externalThemes[initialTheme]);}++ // Detect OS theme preference and apply the corresponding theme+ const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;+ const osTheme = prefersDark ? this.appConfig.nightTheme : this.appConfig.dayTheme;+ if (osTheme) {+ this.$store.commit(Keys.SET_THEME, osTheme);+ this.updateTheme(osTheme);+ }},},};