fix(web): set theme before app loads (#1017)

* chore(web): toggle classList instead of add and remove

* fix(web): set theme before the app loads
This commit is contained in:
Fabricio Silva 2023-07-21 17:00:34 +01:00 committed by GitHub
parent daa4bf9a0f
commit 6c2f3a4b2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View file

@ -21,6 +21,10 @@
<script> <script>
window.APP = {}; window.APP = {};
window.APP.baseUrl = "{{.BaseUrl}}"; window.APP.baseUrl = "{{.BaseUrl}}";
const browserPrefers = !(window.matchMedia !== undefined && window.matchMedia("(prefers-color-scheme: light)").matches);
const { darkTheme = browserPrefers } = JSON.parse(localStorage.getItem("settings")) || {};
document.documentElement.classList.toggle("dark", darkTheme);
</script> </script>
</head> </head>
<body class="bg-color"> <body class="bg-color">

View file

@ -72,12 +72,7 @@ export const SettingsContext = newRidgeState<SettingsType>(
{ {
onSet: (new_state) => { onSet: (new_state) => {
try { try {
if (new_state.darkTheme) { document.documentElement.classList.toggle("dark", new_state.darkTheme);
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
localStorage.setItem("settings", JSON.stringify(new_state)); localStorage.setItem("settings", JSON.stringify(new_state));
} catch (e) { } catch (e) {
console.log("An error occurred while trying to modify the local settings context state."); console.log("An error occurred while trying to modify the local settings context state.");