source: frontend/node_modules/css-prefers-color-scheme/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 KB
Line 
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
8browsers, following the [Media Queries] specification.
9
10[!['Can I use' table](https://caniuse.bitsofco.de/image/prefers-color-scheme.png)](https://caniuse.com/#feat=prefers-color-scheme)
11
12## Usage
13
14From the command line, transform CSS files that use `prefers-color-scheme`
15media queries:
16
17```bash
18npx css-prefers-color-scheme SOURCE.css TRANSFORMED.css
19```
20
21Next, 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>
27colorScheme = 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`
32Without 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.
35The 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
58The frontend receives these `color` queries, which are understood in all
59major browsers going back to Internet Explorer 9.
60However, since browsers can only have a reasonably small number of bits per color,
61our 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
65activate “dark mode” specific CSS, and it changes `(color: 70318723)` queries
66into `(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
77Since these media queries are accessible to `document.styleSheet`, no CSS
78parsing is required.
79
80## Why does the fallback work this way?
81
82The value of `48` is chosen for dark mode because it is the keycode for `0`,
83the hexidecimal value of black. Likewise, `70` is chosen for light mode because
84it is the keycode for `f`, the hexidecimal value of white.
85These 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
Note: See TracBrowser for help on using the repository browser.