Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 969f8f1

Browse files
committed
Hide auto theming option in unsupported browsers
1 parent 7be5e24 commit 969f8f1

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

Assets/ts/theme-switching.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ window.addEventListener("load", () => {
1515
setPickedTheme(newValue as ThemeChoice);
1616
});
1717

18-
themeQuery.addEventListener("change", (systemThemeChangeEvent) => {
18+
themeQuery.addEventListener?.("change", (systemThemeChangeEvent) => {
1919
const systemTheme = systemThemeChangeEvent.matches ? "dark" : "light";
2020
updateDropdownLabel(systemTheme);
2121
if (autoTheme) {
@@ -24,6 +24,7 @@ window.addEventListener("load", () => {
2424
})
2525

2626
setInitialTheme(themeSwitcher);
27+
checkThemingSupport();
2728
})
2829

2930
/**
@@ -67,3 +68,18 @@ function setPickedTheme(newTheme: ThemeChoice) {
6768
function updateDropdownLabel(systemTheme: "light" | "dark") {
6869
window.document.getElementById('theme-option-auto').innerText = `Auto (${systemTheme})`;
6970
}
71+
72+
/**
73+
* Checks whether color-scheme is a supported feature of the browser.
74+
* If it is not, removes the auto option from the dropdown.
75+
*/
76+
function checkThemingSupport() {
77+
const darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
78+
const lightQuery = window.matchMedia('(prefers-color-scheme: light)');
79+
// If neither query matches, we know that the browser doesn't support theming
80+
if (!darkQuery.matches && !lightQuery.matches) {
81+
const themeOptionAuto = window.document.getElementById('theme-option-auto');
82+
// IE doesn't support element.remove()
83+
themeOptionAuto.parentNode.removeChild(themeOptionAuto);
84+
}
85+
}

Resources/all.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
var _a;
22
var themeQuery = window.matchMedia('(prefers-color-scheme: dark)');
33
var localStorageKey = "picked-theme";
4-
var autoTheme = false; // hello
4+
var autoTheme = false;
55
// load initial theme before load event to prevent flashing of background color on navigation
66
var initialTheme = (_a = window.localStorage.getItem(localStorageKey)) !== null && _a !== void 0 ? _a : "auto";
77
setPickedTheme(initialTheme);
88
window.addEventListener("load", function () {
9+
var _a;
910
var themeSwitcher = window.document.getElementById("theme-switcher");
1011
themeSwitcher.addEventListener("change", function (themePickedEvent) {
1112
var newValue = themePickedEvent.target.value;
1213
setPickedTheme(newValue);
1314
});
14-
themeQuery.addEventListener("change", function (systemThemeChangeEvent) {
15+
(_a = themeQuery.addEventListener) === null || _a === void 0 ? void 0 : _a.call(themeQuery, "change", function (systemThemeChangeEvent) {
1516
var systemTheme = systemThemeChangeEvent.matches ? "dark" : "light";
1617
updateDropdownLabel(systemTheme);
1718
if (autoTheme) {
1819
applyTheme(systemTheme);
1920
}
2021
});
2122
setInitialTheme(themeSwitcher);
23+
checkThemingSupport();
2224
});
2325
/**
2426
* 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) {
6062
function updateDropdownLabel(systemTheme) {
6163
window.document.getElementById('theme-option-auto').innerText = "Auto (" + systemTheme + ")";
6264
}
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

Comments
 (0)