|
1 | | -type Theme = 'auto' | 'light' | 'dark'; |
| 1 | +/** |
| 2 | + * Theme toggle. |
| 3 | + * |
| 4 | + * This script should be executed before DOM is loaded. |
| 5 | + * Otherwise, the screen will flash in the dark mode. |
| 6 | + */ |
2 | 7 |
|
3 | | -const themeToggle = document.getElementById("theme-toggle") as HTMLElement; |
4 | | -const themeSelections: HTMLDivElement[] = Array.from(themeToggle.querySelectorAll('div.theme-icon')); |
| 8 | +type Theme = "auto" | "light" | "dark"; |
5 | 9 |
|
6 | | -// Load saved theme preference or default to auto (sync system) |
7 | | -if (typeof localStorage !== "undefined") { |
8 | | - const theme = localStorage.getItem("theme") ?? 'auto'; |
9 | | - applyTheme(theme as Theme); |
10 | | -} |
11 | | - |
12 | | -// Theme toggle functionality |
13 | | -themeToggle.addEventListener("click", () => { |
14 | | - const current = themeSelections.findIndex(icon => !icon.hidden); |
15 | | - const next = themeSelections[(current + 1) % themeSelections.length].classList; |
16 | | - const theme = next.contains("auto") ? "auto" : next.contains("light") ? "light" : "dark"; |
17 | | - applyTheme(theme); |
18 | | -}); |
19 | | - |
20 | | -function applyTheme(theme: Theme): void { |
| 10 | +/** Same as `applyTheme`, but works before DOM is loaded */ |
| 11 | +function applyThemeWithoutDOM(theme: Theme): void { |
21 | 12 | // Change the page’s theme |
22 | | - const resolved = theme === 'auto' ? getSystemTheme() : theme; |
| 13 | + const resolved = theme === "auto" ? getSystemTheme() : theme; |
23 | 14 | if (resolved === "dark") { |
24 | 15 | document.documentElement.classList.add("dark"); |
25 | 16 | } else { |
26 | 17 | document.documentElement.classList.remove("dark"); |
27 | 18 | } |
| 19 | +} |
28 | 20 |
|
29 | | - // Update theme selections |
30 | | - themeSelections.forEach(icon => { |
31 | | - icon.hidden = !icon.classList.contains(theme); |
| 21 | +// Load saved theme preference or default to auto (sync system) |
| 22 | +if (typeof localStorage !== "undefined") { |
| 23 | + const theme = localStorage.getItem("theme") ?? "auto"; |
| 24 | + applyThemeWithoutDOM(theme as Theme); |
| 25 | +} |
| 26 | + |
| 27 | +document.addEventListener("DOMContentLoaded", () => { |
| 28 | + const themeToggle = document.getElementById("theme-toggle") as HTMLElement; |
| 29 | + const themeSelections: HTMLDivElement[] = Array.from( |
| 30 | + themeToggle.querySelectorAll("div.theme-icon"), |
| 31 | + ); |
| 32 | + |
| 33 | + // Load saved theme preference or default to auto (sync system) |
| 34 | + if (typeof localStorage !== "undefined") { |
| 35 | + const theme = localStorage.getItem("theme") ?? "auto"; |
| 36 | + applyTheme(theme as Theme); |
| 37 | + } |
| 38 | + |
| 39 | + // Theme toggle functionality |
| 40 | + themeToggle.addEventListener("click", () => { |
| 41 | + const current = themeSelections.findIndex((icon) => !icon.hidden); |
| 42 | + const next = |
| 43 | + themeSelections[(current + 1) % themeSelections.length].classList; |
| 44 | + const theme = next.contains("auto") |
| 45 | + ? "auto" |
| 46 | + : next.contains("light") |
| 47 | + ? "light" |
| 48 | + : "dark"; |
| 49 | + applyTheme(theme); |
32 | 50 | }); |
33 | 51 |
|
34 | | - // Save for future loads |
35 | | - localStorage.setItem("theme", theme); |
36 | | -} |
| 52 | + function applyTheme(theme: Theme): void { |
| 53 | + applyThemeWithoutDOM(theme); |
| 54 | + |
| 55 | + // Update theme selections |
| 56 | + themeSelections.forEach((icon) => { |
| 57 | + icon.hidden = !icon.classList.contains(theme); |
| 58 | + }); |
| 59 | + |
| 60 | + // Save for future loads |
| 61 | + localStorage.setItem("theme", theme); |
| 62 | + } |
| 63 | +}); |
37 | 64 |
|
38 | | -function getSystemTheme(): 'dark' | 'light' { |
| 65 | +function getSystemTheme(): "dark" | "light" { |
39 | 66 | const match = window.matchMedia && |
40 | 67 | window.matchMedia("(prefers-color-scheme: dark)").matches; |
41 | 68 | return match ? "dark" : "light"; |
|
0 commit comments