Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefers-color-scheme #2540

Open
cylott opened this issue Apr 24, 2020 · 1 comment
Open

prefers-color-scheme #2540

cylott opened this issue Apr 24, 2020 · 1 comment

Comments

@cylott
Copy link

cylott commented Apr 24, 2020

The prefers-color-scheme CSS media query is used to detect if the user has requested the system use a light or dark color theme.

Does Modernizr have a method to detect whether the browser is "Dark Mode" capable? I'm not talking about whether or not dark mode is "on," but whether or not the device/os supports some sort of dark mode. I'd imagine this would be a laundry list of User Agents.

@patrickkettner
Copy link
Member

@cylott it does not. Browsers have built in support for checking if a media query is supported, matchMedia

specifically, you would want

matchMedia('(prefers-color-scheme: dark)')

you can easily make that see if it is "on" with

matchMedia('(prefers-color-scheme: dark)').matches

or more bulletproof

'matchMedia' in window && matchMedia('(prefers-color-scheme: dark)').matches

Modernizr has a helper for media query testing, in mq. Like a lot of things here, its built to support browsers you shouldn't have to think about anymore, so I would not recommend using it unless you have a lot of legacy browsers. It is just a abstraction on top of matchMedia and its ilk

Modernizr.addTest('darkmode', Modernizr.mq("prefers-color-scheme: dark"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants