Fixed dark theme not applying on the first time (#273)

Reviewed-on: #273
Reviewed-by: Mikolaj Widla <widlam@noreply.example.com>
Co-authored-by: Adam Bem <adam.bem@zoho.eu>
Co-committed-by: Adam Bem <adam.bem@zoho.eu>
This commit is contained in:
2023-12-05 10:47:44 +01:00
committed by Adam Bem
parent ebb6730a1b
commit aad0d068d3
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 {onMounted, provide, ref } from 'vue';
const theme = ref( getTheme() );
provide('theme', theme );
onMounted( ()=> {
switch( localStorage.theme ) {
case "dark":{
document.documentElement.classList.add('dark');
break;
}
case "light":{
document.documentElement.classList.remove('dark');
}}
theme.value = localStorage.theme;
onMounted( ()=> {
if (localStorage.theme)
selectThemeFromLocalStorage();
else if (browserPrefersDarkMode()) {
setDarkTheme();
}
})
const theme = ref( getTheme() );
provide('theme', theme );
function setDarkTheme() {
document.documentElement.classList.add('dark');
theme.value = "dark";
localStorage.setItem("theme", "dark");
}
function setTheme(newTheme : string){
theme.value = newTheme;
}
function selectThemeFromLocalStorage() {
if (localStorage.theme == "dark")
document.documentElement.classList.add('dark');
else
document.documentElement.classList.remove('dark');
function getTheme(){
return localStorage.theme;
}
theme.value = 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>

View File

@@ -18,7 +18,10 @@ function changeLogoForTheme(){
}
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){