|
1 | 1 | var _a;
|
2 | 2 | var themeQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
3 | 3 | var localStorageKey = "picked-theme";
|
4 |
| -var autoTheme = false; // hello |
| 4 | +var autoTheme = false; |
5 | 5 | // load initial theme before load event to prevent flashing of background color on navigation
|
6 | 6 | var initialTheme = (_a = window.localStorage.getItem(localStorageKey)) !== null && _a !== void 0 ? _a : "auto";
|
7 | 7 | setPickedTheme(initialTheme);
|
8 | 8 | window.addEventListener("load", function () {
|
| 9 | + var _a; |
9 | 10 | var themeSwitcher = window.document.getElementById("theme-switcher");
|
10 | 11 | themeSwitcher.addEventListener("change", function (themePickedEvent) {
|
11 | 12 | var newValue = themePickedEvent.target.value;
|
12 | 13 | setPickedTheme(newValue);
|
13 | 14 | });
|
14 |
| - themeQuery.addEventListener("change", function (systemThemeChangeEvent) { |
| 15 | + (_a = themeQuery.addEventListener) === null || _a === void 0 ? void 0 : _a.call(themeQuery, "change", function (systemThemeChangeEvent) { |
15 | 16 | var systemTheme = systemThemeChangeEvent.matches ? "dark" : "light";
|
16 | 17 | updateDropdownLabel(systemTheme);
|
17 | 18 | if (autoTheme) {
|
18 | 19 | applyTheme(systemTheme);
|
19 | 20 | }
|
20 | 21 | });
|
21 | 22 | setInitialTheme(themeSwitcher);
|
| 23 | + checkThemingSupport(); |
22 | 24 | });
|
23 | 25 | /**
|
24 | 26 | * Sets the correct theme based on what's in storage, and updates the theme switcher dropdown with the correct initial value.
|
@@ -60,3 +62,18 @@ function setPickedTheme(newTheme) {
|
60 | 62 | function updateDropdownLabel(systemTheme) {
|
61 | 63 | window.document.getElementById('theme-option-auto').innerText = "Auto (" + systemTheme + ")";
|
62 | 64 | }
|
| 65 | + |
| 66 | +/** |
| 67 | + * Checks whether color-scheme is a supported feature of the browser. |
| 68 | + * If it is not, removes the auto option from the dropdown. |
| 69 | + */ |
| 70 | +function checkThemingSupport() { |
| 71 | + var darkQuery = window.matchMedia('(prefers-color-scheme: dark)'); |
| 72 | + var lightQuery = window.matchMedia('(prefers-color-scheme: light)'); |
| 73 | + // If neither query matches, we know that the browser doesn't support theming |
| 74 | + if (!darkQuery.matches && !lightQuery.matches) { |
| 75 | + var themeOptionAuto = window.document.getElementById('theme-option-auto'); |
| 76 | + // IE doesn't support element.remove() |
| 77 | + themeOptionAuto.parentNode.removeChild(themeOptionAuto); |
| 78 | + } |
| 79 | +} |
0 commit comments