Skip to content

Commit

Permalink
Moved initial theme setting to head script to prevent white flash on …
Browse files Browse the repository at this point in the history
…page change
  • Loading branch information
marekdedic committed Nov 25, 2023
1 parent 423e704 commit a975fa6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/templates/@layout.latte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">

<script n:syntax="off">
if ("theme" in localStorage) {
document.documentElement.setAttribute("data-bs-theme", localStorage.theme);
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
document.documentElement.setAttribute("data-bs-theme", "dark");
} else {
document.documentElement.setAttribute("data-bs-theme", "light");
}
</script>

{if $productionMode}
<script n:syntax="off">
ga = function (){ga.q.push(arguments)};
Expand Down
8 changes: 1 addition & 7 deletions frontend/DarkModeToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ export class DarkModeToggle {
this.setTheme(document.documentElement.dataset.bsTheme === "dark" ? "light" : "dark");
};

if ("theme" in localStorage) {
this.setTheme(localStorage.theme);
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
this.setTheme("dark");
} else {
this.setTheme("light");
}
this.setTheme(document.documentElement.dataset.bsTheme as "dark"|"light"|undefined ?? "light");
}

private setTheme(theme: "dark"|"light"): void {
Expand Down

0 comments on commit a975fa6

Please sign in to comment.