https://github.com/magica11y/prefers-color-scheme
🌗 Detects user’s color scheme preferences using the 'prefers-color-scheme' CSS3 level 5 media query.
https://github.com/magica11y/prefers-color-scheme
a11y accessibility css-mediaquery matchmedia prefers-color-scheme user-preferences web
Last synced: 6 months ago
JSON representation
🌗 Detects user’s color scheme preferences using the 'prefers-color-scheme' CSS3 level 5 media query.
- Host: GitHub
- URL: https://github.com/magica11y/prefers-color-scheme
- Owner: magica11y
- License: mit
- Created: 2019-08-03T14:35:54.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-17T12:05:43.000Z (over 1 year ago)
- Last Synced: 2024-09-25T07:52:43.791Z (8 months ago)
- Topics: a11y, accessibility, css-mediaquery, matchmedia, prefers-color-scheme, user-preferences, web
- Language: JavaScript
- Homepage: https://magica11y.github.io
- Size: 747 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
`prefersColorScheme()`
======================
> Detects user’s color scheme preferences using the `prefers-color-scheme` CSS3 level 5 media query.[](https://app.travis-ci.com/github/magica11y/prefers-color-scheme)
[](https://www.npmjs.com/package/@magica11y/prefers-color-scheme)
[")](https://bundlephobia.com/result?p=@magica11y/prefers-color-scheme)
[")](https://bundlephobia.com/result?p=@magica11y/prefers-color-scheme)
[](https://coveralls.io/github/magica11y/prefers-color-scheme)
[](https://david-dm.org/magica11y/prefers-color-scheme)
[](https://david-dm.org/magica11y/prefers-color-scheme?type=dev)
[](https://www.npmjs.com/package/@magica11y/prefers-color-scheme)
[](LICENSE.md)
[](https://snyk.io/test/github/magica11y/prefers-color-scheme?targetFile=package.json)[](https://magica11y.github.io)
# :sparkles: Introduction
Quoting from the [CSS3](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS3) [level 5](https://www.w3.org/TR/mediaqueries-5/)
[media queries](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries) specfication…> The [`'prefers-color-scheme'`](https://www.w3.org/TR/mediaqueries-5/#prefers-color-scheme) media feature reflects the user’s desire that the page use a light or dark color theme.
:high_brightness: **`prefersColorScheme()`** is part of :crystal_ball: [**Magica11y**](https://magica11y.github.io),
which provides a suite of functions to detect “user-preference” and “environment” media features.[Magica11y](https://magica11y.github.io) functions are awesome because…
* They have **zero** dependencies
* They’re **lightweight**; e.g. `prefersColorScheme()` is just [")](https://bundlephobia.com/result?p=@magica11y/prefers-color-scheme) minified, or [")](https://bundlephobia.com/result?p=@magica11y/prefers-color-scheme) minified & gzipp’d
* They use the **[`window.matchMedia`](https://developer.mozilla.org/docs/Web/API/Window/matchMedia)** API underneath
* They’re optimized for **performance**; all the module functions are designed in such a way that they exit early
* They provide a **clean**, **well-documented** and **semantic** API to work withIn addition to `prefersColorScheme()`, [Magica11y](https://magica11y.github.io) also provides…
* :tv: [`environmentBlending()`](https://github.com/magica11y/environment-blending)
* :art: [`forcedColors()`](https://github.com/magica11y/forced-colors)
* :new_moon: [`invertedColors()`](https://github.com/magica11y/inverted-colors)
* ~:candle: [`lightLevel()`](https://github.com/magica11y/light-level)~
* :high_brightness: [`prefersContrast()`](https://github.com/magica11y/prefers-contrast)
* :roller_coaster: [`prefersReducedMotion()`](https://github.com/magica11y/prefers-reduced-motion)
* :gem: [`prefersReducedTransparency()`](https://github.com/magica11y/prefers-reduced-transparency)# :rocket: Getting started
## :building_construction: Installation
You can install `prefersColorScheme()` using a package manager such as [`yarn`](https://yarnpkg.com/en/package/@magica11y/prefers-color-scheme) or [`npm`](https://www.npmjs.com/package/@magica11y/prefers-color-scheme)…
```sh
$ yarn add "@magica11y/prefers-color-scheme"
# OR
$ npm install --save "@magica11y/prefers-color-scheme"
```You can also include `prefersColorScheme()` from a CDN on your page, such as [jsDelivr](https://www.jsdelivr.com/package/npm/@magica11y/prefers-color-scheme) or [unpkg](https://unpkg.com/@magica11y/prefers-color-scheme)…
```html
```
## :game_die: Usage
`prefersColorScheme()` is distributed as a [UMD](https://github.com/umdjs/umd) module, so you can use it as a browser global…
```js
var preferredColorScheme = window.magica11y.prefersColorScheme.default();
var enableDarkTheme = (preferredColorScheme === window.magica11y.prefersColorScheme.colorSchemes.DARK);
```… or as a CommonJS module…
```js
const prefersColorScheme = require('@magica11y/prefers-color-scheme');
const preferredColorScheme = prefersColorScheme.default();
const enableDarkTheme = (preferredColorScheme === prefersColorScheme.colorSchemes.DARK);
```… or as an ES module…
```js
import prefersColorScheme, { colorSchemes } from '@magica11y/prefersColorScheme';const preferredColorScheme = prefersColorScheme();
const enableDarkTheme = (preferredColorScheme === colorSchemes.DARK);
```The `colorSchemes` object contains all the possible values supported by the `'prefers-color-scheme'` media query…
* `colorSchemes.LIGHT` (spec: [`'light'`](https://www.w3.org/TR/mediaqueries-5/#valdef-media-prefers-color-scheme-light))
> Indicates that user has expressed the preference for a page that has a light theme (dark text on light background), or has not expressed an active preference (and thus should receive the "web default" of a light theme).
* `colorSchemes.DARK` (spec: [`'dark'`](https://www.w3.org/TR/mediaqueries-5/#valdef-media-prefers-color-scheme-dark))
> Indicates that user has expressed the preference for a page that has a dark theme (light text on dark background).
* `null`
> The user’s preference could not be determined.# :checkered_flag: Typechecking
You can import the [Flow](https://flow.org) types from the provided [libdefs](https://flow.org/en/docs/libdefs)
in `node_modules/@magica11y/prefers-color-scheme/lib` by configuring them in your `.flowconfig`…```
[libs]
node_modules/@magica11y/prefers-color-scheme/lib
```Now, you can use the Flow types as follows…
```js
// @flow
import prefersColorScheme, { type ColorScheme } from '@magica11y/prefers-color-scheme';const preferredColorScheme: ?ColorScheme = prefersColorScheme();
```:tophat: **Note**: `prefersColorScheme()` returns a [`nullable`](https://flow.org/en/docs/types/primitives/#toc-null-and-void)
type (i.e. `ColorScheme`). So using the `?` prefix to indicate nullable types is recommended (i.e. `?ColorScheme`).# :scroll: License
[](LICENSE.md)
See [LICENSE.md](LICENSE.md) for more details.
Handcrafted with :heart: by [Rishabh](https://rishabh.ink).
[](https://twitter.com/rishabh_ink)