Most operating systems have a setting to set the apps background to dark colors. This is done to reduce the eye strain. Web apps can detect the OS setting and adjust the application colors accordingly. This is an UX enhancement
which we can showcase.
/**
* Should return true when dark mode enabled
* Should return false when dark mode disabled
* @returns {boolean}
*/
export const checkIfDarkModeEnabledOnClientDevice = () => {
if (window && window.matchMedia) {
return window.matchMedia('(prefers-color-scheme: dark)').matches
}
return false
}
@mixin dark-mode {
@media (prefers-color-scheme: dark) {
@content;
}
}
To use it
@include dark-mode {
background-color: $darkModeBackgroundColor;
color: $textColorDarkMode;
}