Add color scheme matcher snippets for browser

This commit is contained in:
Angelos Chalaris
2020-05-04 12:50:35 +03:00
parent 8eed72694a
commit 081e65eed2
2 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,17 @@
---
title: prefersDarkColorScheme
tags: browser,intermediate
---
Returns `true` if the user color scheme preference is `dark`, `false` otherwise.
Use `window.matchMedia()` with the appropriate media query to check the user color scheme preference.
```js
const prefersDarkColorScheme = () =>
window && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
```
```js
prefersDarkColorScheme(); // true
```

View File

@ -0,0 +1,17 @@
---
title: prefersLightColorScheme
tags: browser,intermediate
---
Returns `true` if the user color scheme preference is `light`, `false` otherwise.
Use `window.matchMedia()` with the appropriate media query to check the user color scheme preference.
```js
const prefersLightColorScheme = () =>
window && window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches;
```
```js
prefersLightColorScheme(); // true
```