As I strive to incorporate a Dark Mode feature based on the user's system preferences, I utilize the @media query prefers-color-scheme: dark
. While this approach is effective, I also find it necessary to conduct additional checks using JavaScript.
detectColorScheme() {
if (!window.matchMedia) {
return false;
} else if (window.matchMedia('(prefers-color-scheme: dark').matches) {
this.isDarkMode = true;
}
}
Although this code functions correctly in Chrome, it encounters an issue with Safari. Specifically, for Safari users, the function still returns false even when the system dark mode setting is active.