| 1 | # Prefers Color Scheme [<img src="https://jonathantneal.github.io/js-logo.svg" alt="" width="90" height="90" align="right">][Prefers Color Scheme]
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-img]][npm-url]
|
|---|
| 4 | [![Build Status][cli-img]][cli-url]
|
|---|
| 5 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 6 |
|
|---|
| 7 | [Prefers Color Scheme] lets you use light and dark color schemes in all
|
|---|
| 8 | browsers, following the [Media Queries] specification.
|
|---|
| 9 |
|
|---|
| 10 | [](https://caniuse.com/#feat=prefers-color-scheme)
|
|---|
| 11 |
|
|---|
| 12 | ## Usage
|
|---|
| 13 |
|
|---|
| 14 | From the command line, transform CSS files that use `prefers-color-scheme`
|
|---|
| 15 | media queries:
|
|---|
| 16 |
|
|---|
| 17 | ```bash
|
|---|
| 18 | npx css-prefers-color-scheme SOURCE.css TRANSFORMED.css
|
|---|
| 19 | ```
|
|---|
| 20 |
|
|---|
| 21 | Next, use that transformed CSS with this script:
|
|---|
| 22 |
|
|---|
| 23 | ```html
|
|---|
| 24 | <link rel="stylesheet" href="TRANSFORMED.css">
|
|---|
| 25 | <script src="https://unpkg.com/css-prefers-color-scheme/dist/browser-global.js"></script>
|
|---|
| 26 | <script>
|
|---|
| 27 | colorScheme = initPrefersColorScheme('dark') // apply "dark" queries (you can change it afterward, too)
|
|---|
| 28 | </script>
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | ⚠️ Please use a versioned url, like this : `https://unpkg.com/css-prefers-color-scheme@6.0.0/dist/browser-global.js`
|
|---|
| 32 | Without the version, you might unexpectedly get a new major version of the library with breaking changes.
|
|---|
| 33 |
|
|---|
| 34 | ⚠️ If you were using an older version via a CDN, please update the entire url.
|
|---|
| 35 | The old URL will no longer work in a future release.
|
|---|
| 36 |
|
|---|
| 37 | ## Usage
|
|---|
| 38 |
|
|---|
| 39 | - First, transform `prefers-color-scheme` queries using this
|
|---|
| 40 | [PostCSS plugin](README-POSTCSS.md).
|
|---|
| 41 | - Next, apply light and dark color schemes everywhere using this
|
|---|
| 42 | [browser script](README-BROWSER.md).
|
|---|
| 43 |
|
|---|
| 44 | ---
|
|---|
| 45 |
|
|---|
| 46 | ## How does it work?
|
|---|
| 47 |
|
|---|
| 48 | _This plugin used to work with `color-index` as detailed here : [color-index](https://github.com/csstools/css-prefers-color-scheme#how-does-it-work)._
|
|---|
| 49 | _This is deprecated but will continue to work for now._
|
|---|
| 50 | _`color` has better browser support and enables complex media queries._
|
|---|
| 51 |
|
|---|
| 52 | [Prefers Color Scheme] uses a [PostCSS plugin](README-POSTCSS.md) to transform
|
|---|
| 53 | `prefers-color-scheme` queries into `color` queries. This changes
|
|---|
| 54 | `prefers-color-scheme: dark` into `(color: 48842621)`,
|
|---|
| 55 | `prefers-color-scheme: light` into `(color: 70318723)`, and
|
|---|
| 56 | `prefers-color-scheme: no-preference` into `(color: 22511989)`.
|
|---|
| 57 |
|
|---|
| 58 | The frontend receives these `color` queries, which are understood in all
|
|---|
| 59 | major browsers going back to Internet Explorer 9.
|
|---|
| 60 | However, since browsers can only have a reasonably small number of bits per color,
|
|---|
| 61 | our color scheme values are ignored.
|
|---|
| 62 |
|
|---|
| 63 | [Prefers Color Scheme] uses a [browser script](README-BROWSER.md) to change
|
|---|
| 64 | `(color: 48842621)` queries into `(max-color: 48842621)` in order to
|
|---|
| 65 | activate “dark mode” specific CSS, and it changes `(color: 70318723)` queries
|
|---|
| 66 | into `(max-color: 48842621)` to activate “light mode” specific CSS.
|
|---|
| 67 |
|
|---|
| 68 | ```css
|
|---|
| 69 | @media (color: 70318723) { /* prefers-color-scheme: light */
|
|---|
| 70 | body {
|
|---|
| 71 | background-color: white;
|
|---|
| 72 | color: black;
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 | ```
|
|---|
| 76 |
|
|---|
| 77 | Since these media queries are accessible to `document.styleSheet`, no CSS
|
|---|
| 78 | parsing is required.
|
|---|
| 79 |
|
|---|
| 80 | ## Why does the fallback work this way?
|
|---|
| 81 |
|
|---|
| 82 | The value of `48` is chosen for dark mode because it is the keycode for `0`,
|
|---|
| 83 | the hexidecimal value of black. Likewise, `70` is chosen for light mode because
|
|---|
| 84 | it is the keycode for `f`, the hexidecimal value of white.
|
|---|
| 85 | These are suffixed with a random large number.
|
|---|
| 86 |
|
|---|
| 87 | [cli-img]: https://github.com/csstools/postcss-plugins/workflows/test/badge.svg
|
|---|
| 88 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 89 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 90 | [npm-img]: https://img.shields.io/npm/v/css-prefers-color-scheme.svg
|
|---|
| 91 | [npm-url]: https://www.npmjs.com/package/css-prefers-color-scheme
|
|---|
| 92 |
|
|---|
| 93 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 94 | [Prefers Color Scheme]: https://github.com/csstools/postcss-plugins/tree/main/plugins/css-prefers-color-scheme
|
|---|
| 95 | [Media Queries]: https://drafts.csswg.org/mediaqueries-5/#descdef-media-prefers-color-scheme
|
|---|