|
9 | 9 | }
|
10 | 10 | })();
|
11 | 11 |
|
| 12 | + const settingKey = "use-dark-theme"; |
| 13 | + |
12 | 14 | function toggleDarkTheme(isDark) {
|
13 | 15 | currentlyDark = isDark
|
14 | 16 | // this triggers the `:root.theme-dark` rule from scalastyle.css,
|
15 | 17 | // which changes the values of a bunch of CSS color variables
|
16 | 18 | document.documentElement.classList.toggle("theme-dark", isDark);
|
17 |
| - supportsLocalStorage && localStorage.setItem("use-dark-theme", isDark); |
| 19 | + supportsLocalStorage && localStorage.setItem(settingKey, isDark); |
18 | 20 | }
|
19 | 21 |
|
| 22 | + /* Infer a dark/light theme preference from the user's system */ |
| 23 | + const colorSchemePrefMql = window.matchMedia("(prefers-color-scheme: dark)"); |
| 24 | + |
20 | 25 | /* This needs to happen ASAP so we don't get a FOUC of bright colors before the dark theme is applied */
|
21 |
| - const initiallyDark = supportsLocalStorage && (localStorage.getItem("use-dark-theme") === "true"); |
| 26 | + const initiallyDark = (() => { |
| 27 | + const storedSetting = supportsLocalStorage && localStorage.getItem(settingKey); |
| 28 | + return (storedSetting === null) ? colorSchemePrefMql.matches : storedSetting === "true"; |
| 29 | + })(); |
22 | 30 | let currentlyDark = initiallyDark;
|
23 | 31 | toggleDarkTheme(initiallyDark);
|
24 | 32 |
|
|
29 | 37 | themeToggler.addEventListener("change", e => {
|
30 | 38 | toggleDarkTheme(!e.target.checked);
|
31 | 39 | });
|
| 40 | + |
| 41 | + /* Auto-swap the dark/light theme if the user changes it in their system */ |
| 42 | + colorSchemePrefMql.addEventListener('change', e => { |
| 43 | + const preferDark = e.matches |
| 44 | + themeToggler.checked = !preferDark |
| 45 | + toggleDarkTheme(preferDark) |
| 46 | + }) |
32 | 47 | });
|
33 | 48 | })();
|
0 commit comments