Skip to content

Commit 9a0fd38

Browse files
authored
Bugfix/theme initial state (#214)
* fix: initial state of theme is undefined * style: formatting semicolons also removed a space after a meta tag lol?
1 parent eb4a793 commit 9a0fd38

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/components/Navbar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ function Links() {
3636
}
3737

3838
export const ThemeToggleBtn = (props) => {
39-
const [theme, setTheme] = createSignal("light");
39+
const [theme, setTheme] = createSignal("");
4040

4141
onMount(() => {
42-
const localTheme = localStorage.theme
43-
setTheme(localTheme)
44-
})
42+
const localTheme = localStorage.theme;
43+
setTheme(localTheme);
44+
});
4545

4646
return (
4747
<button

src/entry-server.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default createHandler(() => (
66
<html lang="en">
77
<head>
88
<meta charset="utf-8" />
9-
<meta name="viewport" content="width=device-width, initial-scale=1" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1" />
1010

1111
{/* favicon & font */}
1212
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
@@ -24,9 +24,11 @@ export default createHandler(() => (
2424

2525
{assets}
2626

27-
{/* set saved theme */}
27+
{/* Check if a theme is saved in localStorage, if not, set it based on the system's theme preference */}
2828
<script>
29-
document.querySelector("html").className = localStorage.theme
29+
if (!localStorage.theme)
30+
localStorage.theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
31+
document.querySelector("html").className = localStorage.theme;
3032
</script>
3133
</head>
3234
<body>

0 commit comments

Comments
 (0)