Skip to content

Commit f86f3fb

Browse files
committed
refactor(themetoggle): Refactor init logic to prioritise localStorage over browser preferences
1 parent 60c8e9c commit f86f3fb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

frontend/src/lib/components/ThemeToggle.svelte

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Button } from 'bits-ui';
44
import { MoonIcon, SunIcon } from 'lucide-svelte';
55
6-
let theme = $state((browser && localStorage.getItem('color-scheme')) || 'light');
6+
let theme = $state('light');
77
88
if (browser) {
99
const preference = window.matchMedia('(prefers-color-scheme: dark)');
@@ -12,6 +12,10 @@
1212
theme = 'dark';
1313
}
1414
15+
console.log(localStorage.getItem('color-scheme'));
16+
17+
theme = localStorage.getItem('color-scheme') || theme;
18+
1519
setTheme();
1620
1721
preference.addEventListener('change', (mediaQuery) => {
@@ -39,13 +43,17 @@
3943

4044
<svelte:head>
4145
<script type="module">
42-
let theme = localStorage.getItem('color-scheme') || 'light';
46+
let theme = 'light';
47+
4348
const preference = window.matchMedia('(prefers-color-scheme: dark)');
4449
4550
if (preference.matches) {
4651
theme = 'dark';
4752
}
4853
54+
theme = localStorage.getItem('color-scheme') || theme;
55+
56+
console.log('Theme', theme);
4957
document.documentElement.setAttribute('data-color-scheme', theme);
5058
localStorage.setItem('color-scheme', theme);
5159
</script>

0 commit comments

Comments
 (0)