Fixed dark theme not applying on the first time #273

Merged
bema merged 1 commits from bema/fix/default_dark_mode into master 2023-12-05 10:47:46 +01:00
2 changed files with 36 additions and 19 deletions

View File

@@ -3,29 +3,43 @@ import { RouterView } from 'vue-router';
import SidebarComponent from '@components/sidebar/SidebarComponent.vue'; import SidebarComponent from '@components/sidebar/SidebarComponent.vue';
import {onMounted, provide, ref } from 'vue'; import {onMounted, provide, ref } from 'vue';
const theme = ref( getTheme() );
provide('theme', theme );
onMounted( ()=> { onMounted( ()=> {
switch( localStorage.theme ) { if (localStorage.theme)
case "dark":{ selectThemeFromLocalStorage();
document.documentElement.classList.add('dark'); else if (browserPrefersDarkMode()) {
break; setDarkTheme();
} }
case "light":{
document.documentElement.classList.remove('dark');
}}
theme.value = localStorage.theme;
}) })
const theme = ref( getTheme() ); function setDarkTheme() {
provide('theme', theme ); document.documentElement.classList.add('dark');
theme.value = "dark";
localStorage.setItem("theme", "dark");
}
function setTheme(newTheme : string){ function selectThemeFromLocalStorage() {
theme.value = newTheme; if (localStorage.theme == "dark")
} document.documentElement.classList.add('dark');
else
document.documentElement.classList.remove('dark');
function getTheme(){ theme.value = localStorage.theme;
return localStorage.theme; }
}
function browserPrefersDarkMode(){
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
}
function setTheme(newTheme : string){
theme.value = newTheme;
}
function getTheme(){
return localStorage.theme;
}
</script> </script>

View File

@@ -18,7 +18,10 @@ function changeLogoForTheme(){
} }
function isDarkModeSet(){ function isDarkModeSet(){
return localStorage.theme == "dark"; if (localStorage.theme)
return localStorage.theme == "dark";
else
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
} }
function changeTheme(theme:string){ function changeTheme(theme:string){