css-prefers-color-scheme 6.0.2 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/README.md +278 -45
- package/dist/browser-global.js +1 -99
- package/dist/browser-global.js.map +1 -1
- package/dist/browser.cjs +1 -92
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.mjs +1 -92
- package/dist/browser.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +1 -1
- package/package.json +101 -79
- package/browser.js +0 -100
- package/browser.min.js +0 -100
- package/dist/cli.cjs +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,61 @@
|
|
|
1
1
|
# Changes to Prefers Color Scheme
|
|
2
2
|
|
|
3
|
+
### 7.0.1 (August 23, 2022)
|
|
4
|
+
|
|
5
|
+
- Fix: assign global browser polyfill to `window`, `self` or a blank object.
|
|
6
|
+
|
|
7
|
+
### 7.0.0 (July 8, 2022)
|
|
8
|
+
|
|
9
|
+
[Read the full changelog](https://github.com/csstools/postcss-plugins/wiki/PostCSS-Preset-Env-8)
|
|
10
|
+
|
|
11
|
+
- Breaking: removed old CDN urls
|
|
12
|
+
- Breaking: remove `color-depth` queries fallback
|
|
13
|
+
- Breaking: remove 'no-preference' support as this was dropped from the spec
|
|
14
|
+
- Breaking: remove old global object
|
|
15
|
+
- Fix: case insensitive matching.
|
|
16
|
+
|
|
17
|
+
#### How to migrate :
|
|
18
|
+
|
|
19
|
+
##### Re-build your CSS with the new version of the library.
|
|
20
|
+
|
|
21
|
+
##### If you use a CDN url, please update it.
|
|
22
|
+
|
|
23
|
+
```diff
|
|
24
|
+
- <script src="https://unpkg.com/css-prefers-color-scheme/browser"></script>
|
|
25
|
+
+ <script src="https://unpkg.com/css-prefers-color-scheme/dist/browser-global.js"></script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```diff
|
|
29
|
+
- <script src="https://unpkg.com/css-prefers-color-scheme/browser.min"></script>
|
|
30
|
+
+ <script src="https://unpkg.com/css-prefers-color-scheme/dist/browser-global.js"></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
##### Use `prefersColorSchemeInit` to initialize the polyfill in the browser.
|
|
34
|
+
|
|
35
|
+
```diff
|
|
36
|
+
- initPrefersColorScheme()
|
|
37
|
+
+ prefersColorSchemeInit()
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
##### Remove `@media (prefer-color-scheme: no-preference)` from your CSS.
|
|
41
|
+
|
|
42
|
+
`@media (prefers-color-scheme: no-preference)` was removed from the specification and should be equivalent to not having any media query.
|
|
43
|
+
|
|
44
|
+
```diff
|
|
45
|
+
- @media (prefers-color-scheme: no-preference) {
|
|
46
|
+
- .some-selector {
|
|
47
|
+
- /* your styles ... */
|
|
48
|
+
- }
|
|
49
|
+
- }
|
|
50
|
+
+ .some-selector {
|
|
51
|
+
+ /* your styles ... */
|
|
52
|
+
+ }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 6.0.3 (January 31, 2022)
|
|
56
|
+
|
|
57
|
+
- Fix `preserve: false` option.
|
|
58
|
+
|
|
3
59
|
### 6.0.2 (January 2, 2022)
|
|
4
60
|
|
|
5
61
|
- Removed Sourcemaps from package tarball.
|
package/README.md
CHANGED
|
@@ -1,96 +1,329 @@
|
|
|
1
|
-
# Prefers Color Scheme [<img src="https://
|
|
1
|
+
# Prefers Color Scheme [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
[![Build Status][cli-img]][cli-url]
|
|
5
|
-
[![Support Chat][git-img]][git-url]
|
|
3
|
+
[<img alt="npm version" src="https://img.shields.io/npm/v/css-prefers-color-scheme.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/prefers-color-scheme-query.svg" height="20">][css-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|
6
4
|
|
|
7
|
-
[Prefers Color Scheme] lets you use light and dark color schemes in all
|
|
8
|
-
browsers, following the [Media Queries] specification.
|
|
5
|
+
[Prefers Color Scheme] lets you use light and dark color schemes in all browsers, following the [Media Queries] specification.
|
|
9
6
|
|
|
10
|
-
|
|
7
|
+
```pcss
|
|
8
|
+
@media (prefers-color-scheme: dark) {
|
|
9
|
+
:root {
|
|
10
|
+
--site-bgcolor: #1b1b1b;
|
|
11
|
+
--site-color: #fff;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@media (prefers-color-scheme: light) {
|
|
16
|
+
:root {
|
|
17
|
+
--site-bgcolor: #fff;
|
|
18
|
+
--site-color: #222;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* becomes */
|
|
23
|
+
|
|
24
|
+
@media (color: 48842621) {
|
|
25
|
+
:root {
|
|
26
|
+
--site-bgcolor: #1b1b1b;
|
|
27
|
+
--site-color: #fff;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@media (prefers-color-scheme: dark) {
|
|
32
|
+
:root {
|
|
33
|
+
--site-bgcolor: #1b1b1b;
|
|
34
|
+
--site-color: #fff;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@media (color: 70318723) {
|
|
39
|
+
:root {
|
|
40
|
+
--site-bgcolor: #fff;
|
|
41
|
+
--site-color: #222;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@media (prefers-color-scheme: light) {
|
|
46
|
+
:root {
|
|
47
|
+
--site-bgcolor: #fff;
|
|
48
|
+
--site-color: #222;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
11
52
|
|
|
12
53
|
## Usage
|
|
13
54
|
|
|
14
|
-
|
|
15
|
-
media queries:
|
|
55
|
+
Add [Prefers Color Scheme] to your project:
|
|
16
56
|
|
|
17
57
|
```bash
|
|
18
|
-
|
|
58
|
+
npm install postcss css-prefers-color-scheme --save-dev
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Use it as a [PostCSS] plugin:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const postcss = require('postcss');
|
|
65
|
+
const prefersColorScheme = require('css-prefers-color-scheme');
|
|
66
|
+
|
|
67
|
+
postcss([
|
|
68
|
+
prefersColorScheme(/* pluginOptions */)
|
|
69
|
+
]).process(YOUR_CSS /*, processOptions */);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
[Prefers Color Scheme] runs in all Node environments, with special
|
|
73
|
+
instructions for:
|
|
74
|
+
|
|
75
|
+
| [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|
76
|
+
| --- | --- | --- | --- | --- | --- |
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
### preserve
|
|
81
|
+
|
|
82
|
+
The `preserve` option determines whether the original notation
|
|
83
|
+
is preserved. By default, it is preserved.
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
prefersColorScheme({ preserve: false })
|
|
19
87
|
```
|
|
20
88
|
|
|
21
|
-
|
|
89
|
+
```pcss
|
|
90
|
+
@media (prefers-color-scheme: dark) {
|
|
91
|
+
:root {
|
|
92
|
+
--site-bgcolor: #1b1b1b;
|
|
93
|
+
--site-color: #fff;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@media (prefers-color-scheme: light) {
|
|
98
|
+
:root {
|
|
99
|
+
--site-bgcolor: #fff;
|
|
100
|
+
--site-color: #222;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* becomes */
|
|
105
|
+
|
|
106
|
+
@media (color: 48842621) {
|
|
107
|
+
:root {
|
|
108
|
+
--site-bgcolor: #1b1b1b;
|
|
109
|
+
--site-color: #fff;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@media (color: 70318723) {
|
|
114
|
+
:root {
|
|
115
|
+
--site-bgcolor: #fff;
|
|
116
|
+
--site-color: #222;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Browser
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
// initialize prefersColorScheme (applies the current OS color scheme, if available)
|
|
125
|
+
import prefersColorSchemeInit from 'css-prefers-color-scheme/browser';
|
|
126
|
+
const prefersColorScheme = prefersColorSchemeInit();
|
|
127
|
+
|
|
128
|
+
// apply "dark" queries (you can also apply "light")
|
|
129
|
+
prefersColorScheme.scheme = 'dark';
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
or
|
|
22
133
|
|
|
23
134
|
```html
|
|
24
|
-
|
|
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>
|
|
135
|
+
<!-- When using a CDN url you will have to manually update the version number -->
|
|
136
|
+
<script src="https://unpkg.com/css-prefers-color-scheme@7.0.1/dist/browser-global.js"></script>
|
|
137
|
+
<script>prefersColorSchemeInit()</script>
|
|
29
138
|
```
|
|
30
139
|
|
|
31
|
-
⚠️ Please use a versioned url, like this : `https://unpkg.com/css-prefers-color-scheme@
|
|
140
|
+
⚠️ Please use a versioned url, like this : `https://unpkg.com/css-prefers-color-scheme@7.0.1/dist/browser-global.js`
|
|
32
141
|
Without the version, you might unexpectedly get a new major version of the library with breaking changes.
|
|
33
142
|
|
|
34
|
-
|
|
35
|
-
|
|
143
|
+
[Prefers Color Scheme] works in all major browsers, including Safari 6+ and
|
|
144
|
+
Internet Explorer 9+ without any additional polyfills.
|
|
36
145
|
|
|
37
|
-
|
|
146
|
+
To maintain compatibility with browsers supporting `prefers-color-scheme`, the
|
|
147
|
+
library will remove `prefers-color-scheme` media queries in favor of
|
|
148
|
+
cross-browser compatible `color` media queries. This ensures a seamless
|
|
149
|
+
experience, even when JavaScript is unable to run.
|
|
38
150
|
|
|
39
|
-
|
|
40
|
-
[PostCSS plugin](README-POSTCSS.md).
|
|
41
|
-
- Next, apply light and dark color schemes everywhere using this
|
|
42
|
-
[browser script](README-BROWSER.md).
|
|
151
|
+
### Browser Usage
|
|
43
152
|
|
|
44
|
-
|
|
153
|
+
Use [Prefers Color Scheme] to activate your `prefers-color-scheme` queries:
|
|
45
154
|
|
|
46
|
-
|
|
155
|
+
```js
|
|
156
|
+
import prefersColorSchemeInit from 'css-prefers-color-scheme/browser';
|
|
157
|
+
const prefersColorScheme = prefersColorSchemeInit();
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
By default, the current OS color scheme is applied if your browser supports it.
|
|
161
|
+
Otherwise, the light color scheme is applied. You may override this by passing
|
|
162
|
+
in a color scheme.
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
import prefersColorSchemeInit from 'css-prefers-color-scheme/browser';
|
|
166
|
+
const prefersColorScheme = prefersColorSchemeInit('dark');
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The `prefersColorScheme` object returns the following properties — `scheme`,
|
|
170
|
+
`hasNativeSupport`, `onChange`, and `removeListener`.
|
|
171
|
+
|
|
172
|
+
#### scheme
|
|
173
|
+
|
|
174
|
+
The `scheme` property returns the currently preferred color scheme, and it can
|
|
175
|
+
be changed.
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
import prefersColorSchemeInit from 'css-prefers-color-scheme/browser';
|
|
179
|
+
const prefersColorScheme = prefersColorSchemeInit();
|
|
47
180
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
181
|
+
// log the preferred color scheme
|
|
182
|
+
console.log(prefersColorScheme.scheme);
|
|
183
|
+
|
|
184
|
+
// apply "dark" queries
|
|
185
|
+
prefersColorScheme.scheme = 'dark';
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### hasNativeSupport
|
|
189
|
+
|
|
190
|
+
The `hasNativeSupport` boolean represents whether `prefers-color-scheme` is
|
|
191
|
+
supported by the browser.
|
|
192
|
+
|
|
193
|
+
#### onChange
|
|
194
|
+
|
|
195
|
+
The optional `onChange` function is run when the preferred color scheme is
|
|
196
|
+
changed, either from the OS or manually.
|
|
197
|
+
|
|
198
|
+
#### removeListener
|
|
199
|
+
|
|
200
|
+
The `removeListener` function removes the native `prefers-color-scheme`
|
|
201
|
+
listener, which may or may not be applied, depending on your browser support.
|
|
202
|
+
This is provided to give you complete control over plugin cleanup.
|
|
203
|
+
|
|
204
|
+
#### debug
|
|
205
|
+
|
|
206
|
+
If styles are not applied you can enable debug mode to log exceptions.
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
import prefersColorSchemeInit from 'css-prefers-color-scheme/browser';
|
|
210
|
+
const prefersColorScheme = prefersColorSchemeInit('light', { debug: true });
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
```html
|
|
214
|
+
<script src="https://unpkg.com/css-prefers-color-scheme@7.0.1/dist/browser-global.js"></script>
|
|
215
|
+
<script>prefersColorSchemeInit('light', { debug: true })</script>
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
### Browser Dependencies
|
|
220
|
+
|
|
221
|
+
Web API's:
|
|
222
|
+
|
|
223
|
+
- [Window.matchMedia](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia)
|
|
224
|
+
|
|
225
|
+
ECMA Script:
|
|
226
|
+
|
|
227
|
+
- `Object.defineProperty`
|
|
228
|
+
- `Array.prototype.forEach`
|
|
229
|
+
- `Array.prototype.indexOf`
|
|
230
|
+
- `RegExp.prototype.exec`
|
|
231
|
+
- `String.prototype.match`
|
|
232
|
+
- `String.prototype.replace`
|
|
233
|
+
|
|
234
|
+
## CORS
|
|
235
|
+
|
|
236
|
+
⚠️ Applies to you if you load CSS from a different domain than the page.
|
|
237
|
+
|
|
238
|
+
In this case the CSS is treated as untrusted and will not be made available to the JavaScript polyfill.
|
|
239
|
+
The polyfill will not work without applying the correct configuration for CORS.
|
|
240
|
+
|
|
241
|
+
Example :
|
|
242
|
+
|
|
243
|
+
| page | css | CORS applies |
|
|
244
|
+
| --- | --- | --- |
|
|
245
|
+
| https://example.com/ | https://example.com/style.css | no |
|
|
246
|
+
| https://example.com/ | https://other.com/style.css | yes |
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
**You might see one of these error messages :**
|
|
250
|
+
|
|
251
|
+
Chrome :
|
|
252
|
+
|
|
253
|
+
> DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules
|
|
254
|
+
|
|
255
|
+
Safari :
|
|
256
|
+
|
|
257
|
+
> SecurityError: Not allowed to access cross-origin stylesheet
|
|
258
|
+
|
|
259
|
+
Firefox :
|
|
260
|
+
|
|
261
|
+
> DOMException: CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet
|
|
262
|
+
|
|
263
|
+
To resolve CORS errors you need to take two steps :
|
|
264
|
+
|
|
265
|
+
- add an HTTP header `Access-Control-Allow-Origin: <your-value>` when serving your CSS file.
|
|
266
|
+
- add `crossorigin="anonymous"` to the `<link rel="stylesheet">` tag for your CSS file.
|
|
267
|
+
|
|
268
|
+
In a node server setting the HTTP header might look like this :
|
|
269
|
+
|
|
270
|
+
```js
|
|
271
|
+
// http://localhost:8080 is the domain of your page!
|
|
272
|
+
res.setHeader('Access-Control-Allow-Origin', 'https://example.com');
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
You can also configure a wildcard but please be aware that this might be a security risk.
|
|
276
|
+
It is better to only set the header for the domain you want to allow and only on the responses you want to allow.
|
|
277
|
+
|
|
278
|
+
HTML might look like this :
|
|
279
|
+
|
|
280
|
+
```html
|
|
281
|
+
<link rel="stylesheet" href="https://example.com/styles.css" crossorigin="anonymous">
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
## How does it work?
|
|
51
286
|
|
|
52
|
-
[Prefers Color Scheme]
|
|
53
|
-
`prefers-color-scheme`
|
|
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)`.
|
|
287
|
+
[Prefers Color Scheme] is a [PostCSS] plugin that transforms `prefers-color-scheme` queries into `color` queries.
|
|
288
|
+
This changes `prefers-color-scheme: dark` into `(color: 48842621)` and `prefers-color-scheme: light` into `(color: 70318723)`.
|
|
57
289
|
|
|
58
290
|
The frontend receives these `color` queries, which are understood in all
|
|
59
291
|
major browsers going back to Internet Explorer 9.
|
|
60
292
|
However, since browsers can only have a reasonably small number of bits per color,
|
|
61
293
|
our color scheme values are ignored.
|
|
62
294
|
|
|
63
|
-
[Prefers Color Scheme] uses a [browser script](
|
|
295
|
+
[Prefers Color Scheme] uses a [browser script](#browser) to change
|
|
64
296
|
`(color: 48842621)` queries into `(max-color: 48842621)` in order to
|
|
65
297
|
activate “dark mode” specific CSS, and it changes `(color: 70318723)` queries
|
|
66
298
|
into `(max-color: 48842621)` to activate “light mode” specific CSS.
|
|
67
299
|
|
|
68
300
|
```css
|
|
69
301
|
@media (color: 70318723) { /* prefers-color-scheme: light */
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
302
|
+
body {
|
|
303
|
+
background-color: white;
|
|
304
|
+
color: black;
|
|
305
|
+
}
|
|
74
306
|
}
|
|
75
307
|
```
|
|
76
308
|
|
|
77
309
|
Since these media queries are accessible to `document.styleSheet`, no CSS
|
|
78
310
|
parsing is required.
|
|
79
311
|
|
|
80
|
-
|
|
312
|
+
### Why does the fallback work this way?
|
|
81
313
|
|
|
82
314
|
The value of `48` is chosen for dark mode because it is the keycode for `0`,
|
|
83
315
|
the hexidecimal value of black. Likewise, `70` is chosen for light mode because
|
|
84
316
|
it is the keycode for `f`, the hexidecimal value of white.
|
|
85
317
|
These are suffixed with a random large number.
|
|
86
318
|
|
|
87
|
-
[cli-img]: https://github.com/csstools/postcss-plugins/workflows/test/badge.svg
|
|
88
319
|
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|
89
|
-
[
|
|
90
|
-
[
|
|
91
|
-
[npm-img]: https://img.shields.io/npm/v/css-prefers-color-scheme.svg
|
|
320
|
+
[css-url]: https://cssdb.org/#prefers-color-scheme-query
|
|
321
|
+
[discord]: https://discord.gg/bUadyRwkJS
|
|
92
322
|
[npm-url]: https://www.npmjs.com/package/css-prefers-color-scheme
|
|
93
323
|
|
|
324
|
+
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|
325
|
+
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|
94
326
|
[PostCSS]: https://github.com/postcss/postcss
|
|
327
|
+
[PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|
95
328
|
[Prefers Color Scheme]: https://github.com/csstools/postcss-plugins/tree/main/plugins/css-prefers-color-scheme
|
|
96
|
-
[Media Queries]: https://
|
|
329
|
+
[Media Queries]: https://www.w3.org/TR/mediaqueries-5/#prefers-color-scheme
|
package/dist/browser-global.js
CHANGED
|
@@ -1,100 +1,2 @@
|
|
|
1
|
-
(function () {
|
|
2
|
-
|
|
3
|
-
/* global document,window,matchMedia */
|
|
4
|
-
var colorIndexRegExp = /((?:not )?all and )?(\(color-index: *(22|48|70)\))/i;
|
|
5
|
-
var prefersColorSchemeRegExp = /prefers-color-scheme:/i;
|
|
6
|
-
|
|
7
|
-
var prefersColorSchemeInit = function prefersColorSchemeInit(initialColorScheme) {
|
|
8
|
-
var mediaQueryString = '(prefers-color-scheme: dark)';
|
|
9
|
-
var mediaQueryList = window.matchMedia && matchMedia(mediaQueryString);
|
|
10
|
-
var hasNativeSupport = mediaQueryList && mediaQueryList.media === mediaQueryString;
|
|
11
|
-
|
|
12
|
-
var mediaQueryListener = function mediaQueryListener() {
|
|
13
|
-
set(mediaQueryList.matches ? 'dark' : 'light');
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
var removeListener = function removeListener() {
|
|
17
|
-
if (mediaQueryList) {
|
|
18
|
-
mediaQueryList.removeListener(mediaQueryListener);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
var set = function set(colorScheme) {
|
|
23
|
-
if (colorScheme !== currentColorScheme) {
|
|
24
|
-
currentColorScheme = colorScheme;
|
|
25
|
-
|
|
26
|
-
if (typeof result.onChange === 'function') {
|
|
27
|
-
result.onChange();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
[].forEach.call(document.styleSheets || [], function (styleSheet) {
|
|
32
|
-
// cssRules is a live list. Converting to an Array first.
|
|
33
|
-
var rules = [];
|
|
34
|
-
[].forEach.call(styleSheet.cssRules || [], function (cssRule) {
|
|
35
|
-
rules.push(cssRule);
|
|
36
|
-
});
|
|
37
|
-
rules.forEach(function (cssRule) {
|
|
38
|
-
var colorSchemeMatch = prefersColorSchemeRegExp.test(Object(cssRule.media).mediaText);
|
|
39
|
-
|
|
40
|
-
if (colorSchemeMatch) {
|
|
41
|
-
var index = [].indexOf.call(cssRule.parentStyleSheet.cssRules, cssRule);
|
|
42
|
-
cssRule.parentStyleSheet.deleteRule(index);
|
|
43
|
-
} else {
|
|
44
|
-
var colorIndexMatch = (Object(cssRule.media).mediaText || '').match(colorIndexRegExp);
|
|
45
|
-
|
|
46
|
-
if (colorIndexMatch) {
|
|
47
|
-
// Old style which has poor browser support and can't handle complex media queries.
|
|
48
|
-
cssRule.media.mediaText = ((/^dark$/i.test(colorScheme) ? colorIndexMatch[3] === '48' : /^light$/i.test(colorScheme) ? colorIndexMatch[3] === '70' : colorIndexMatch[3] === '22') ? 'not all and ' : '') + cssRule.media.mediaText.replace(colorIndexRegExp, '$2');
|
|
49
|
-
} else {
|
|
50
|
-
// New style which supports complex media queries.
|
|
51
|
-
var colorDepthMatch = (Object(cssRule.media).mediaText || '').match(/\( *(?:color|max-color): *(48842621|70318723|22511989) *\)/i);
|
|
52
|
-
|
|
53
|
-
if (colorDepthMatch && colorDepthMatch.length > 1) {
|
|
54
|
-
if (/^dark$/i.test(colorScheme) && (colorDepthMatch[1] === '48842621' || colorDepthMatch[1] === '22511989')) {
|
|
55
|
-
// No preference or preferred is dark and rule is dark.
|
|
56
|
-
cssRule.media.mediaText = cssRule.media.mediaText.replace(/\( *color: *(?:48842621|70318723) *\)/i, "(max-color: " + colorDepthMatch[1] + ")");
|
|
57
|
-
} else if (/^light$/i.test(colorScheme) && (colorDepthMatch[1] === '70318723' || colorDepthMatch[1] === '22511989')) {
|
|
58
|
-
// No preference or preferred is light and rule is light.
|
|
59
|
-
cssRule.media.mediaText = cssRule.media.mediaText.replace(/\( *color: *(?:48842621|22511989) *\)/i, "(max-color: " + colorDepthMatch[1] + ")");
|
|
60
|
-
} else {
|
|
61
|
-
cssRule.media.mediaText = cssRule.media.mediaText.replace(/\( *max-color: *(?:48842621|70318723|22511989) *\)/i, "(color: " + colorDepthMatch[1] + ")");
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
var result = Object.defineProperty({
|
|
71
|
-
hasNativeSupport: hasNativeSupport,
|
|
72
|
-
removeListener: removeListener
|
|
73
|
-
}, 'scheme', {
|
|
74
|
-
get: function get() {
|
|
75
|
-
return currentColorScheme;
|
|
76
|
-
},
|
|
77
|
-
set: set
|
|
78
|
-
}); // initialize the color scheme using the provided value, the system value, or light
|
|
79
|
-
|
|
80
|
-
var currentColorScheme = initialColorScheme || (mediaQueryList && mediaQueryList.matches ? 'dark' : 'light');
|
|
81
|
-
set(currentColorScheme); // listen for system changes
|
|
82
|
-
|
|
83
|
-
if (mediaQueryList) {
|
|
84
|
-
if ('addEventListener' in mediaQueryList) {
|
|
85
|
-
mediaQueryList.addEventListener('change', mediaQueryListener);
|
|
86
|
-
} else {
|
|
87
|
-
mediaQueryList.addListener(mediaQueryListener);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return result;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/* global self */
|
|
95
|
-
self.prefersColorSchemeInit = prefersColorSchemeInit; // Legacy : there used to be a rollup config that exposed this function under a different name.
|
|
96
|
-
|
|
97
|
-
self.initPrefersColorScheme = prefersColorSchemeInit;
|
|
98
|
-
|
|
99
|
-
})();
|
|
1
|
+
!function(){var e=/prefers-color-scheme:/i,t=function(t,a){a||(a={}),a={debug:!!a.debug||!1};var i="(prefers-color-scheme: dark)",o="matchMedia"in window&&window.matchMedia(i),r=o&&o.media===i,c=function(){n(o&&o.matches?"dark":"light")},n=function(t){"dark"!==t&&"light"!==t&&(t=r&&o.matches?"dark":"light"),t!==l&&(l=t,"function"==typeof d.onChange&&d.onChange()),[].forEach.call(document.styleSheets||[],(function(i){try{var o=[];[].forEach.call(i.cssRules||[],(function(e){o.push(e)})),o.forEach((function(a){if(e.test(Object(a.media).mediaText)){var i=[].indexOf.call(a.parentStyleSheet.cssRules,a);a.parentStyleSheet.deleteRule(i)}else{var o=(Object(a.media).mediaText||"").match(/\( *(?:color|max-color): *(48842621|70318723) *\)/i);o&&o.length>1&&("dark"===t&&"48842621"===o[1]?a.media.mediaText=a.media.mediaText.replace(/\( *color: *(?:48842621) *\)/i,"(max-color: "+o[1]+")"):"light"===t&&"70318723"===o[1]?a.media.mediaText=a.media.mediaText.replace(/\( *color: *(?:70318723) *\)/i,"(max-color: "+o[1]+")"):a.media.mediaText=a.media.mediaText.replace(/\( *max-color: *(?:48842621|70318723) *\)/i,"(color: "+o[1]+")"))}}))}catch(e){a.debug&&console.error(e)}}))},d=Object.defineProperty({hasNativeSupport:r,removeListener:function(){o&&o.removeListener(c)}},"scheme",{get:function(){return l},set:n}),l=t||(o&&o.matches?"dark":"light");return n(l),o&&("addEventListener"in o?o.addEventListener("change",c):o.addListener(c)),d};("object"==typeof window&&window||"object"==typeof self&&self||{}).prefersColorSchemeInit=t}();
|
|
100
2
|
//# sourceMappingURL=browser-global.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser-global.js","sources":["../src/browser.js","../src/browser-global.js"],"sourcesContent":["/* global document,window
|
|
1
|
+
{"version":3,"file":"browser-global.js","sources":["../src/browser.js","../src/browser-global.js"],"sourcesContent":["/* global document,window */\nconst prefersColorSchemeRegExp = /prefers-color-scheme:/i;\n\nconst prefersColorSchemeInit = (initialColorScheme, options) => {\n\t// OPTIONS\n\t{\n\t\tif (!options) {\n\t\t\toptions = {};\n\t\t}\n\n\t\toptions = {\n\t\t\tdebug: (!!options.debug) || false,\n\t\t};\n\t}\n\n\tconst mediaQueryString = '(prefers-color-scheme: dark)';\n\tconst mediaQueryList = ('matchMedia' in window) && window.matchMedia(mediaQueryString);\n\tconst hasNativeSupport = mediaQueryList && mediaQueryList.media === mediaQueryString;\n\tconst mediaQueryListener = () => {\n\t\tset((mediaQueryList && mediaQueryList.matches) ? 'dark' : 'light');\n\t};\n\tconst removeListener = () => {\n\t\tif (mediaQueryList) {\n\t\t\tmediaQueryList.removeListener(mediaQueryListener);\n\t\t}\n\t};\n\tconst set = (colorScheme) => {\n\t\tif (colorScheme !== 'dark' && colorScheme !== 'light') {\n\t\t\tif (hasNativeSupport) {\n\t\t\t\tcolorScheme = mediaQueryList.matches ? 'dark' : 'light';\n\t\t\t} else {\n\t\t\t\tcolorScheme = 'light';\n\t\t\t}\n\t\t}\n\n\t\tif (colorScheme !== currentColorScheme) {\n\t\t\tcurrentColorScheme = colorScheme;\n\n\t\t\tif (typeof result.onChange === 'function') {\n\t\t\t\tresult.onChange();\n\t\t\t}\n\t\t}\n\n\t\t[].forEach.call(document.styleSheets || [], styleSheet => {\n\t\t\ttry {\n\t\t\t\t// cssRules is a live list. Converting to an Array first.\n\t\t\t\tconst rules = [];\n\t\t\t\t[].forEach.call(styleSheet.cssRules || [], cssRule => {\n\t\t\t\t\trules.push(cssRule);\n\t\t\t\t});\n\n\t\t\t\trules.forEach(cssRule => {\n\t\t\t\t\tconst colorSchemeMatch = prefersColorSchemeRegExp.test(Object(cssRule.media).mediaText);\n\n\t\t\t\t\tif (colorSchemeMatch) {\n\t\t\t\t\t\tconst index = [].indexOf.call(cssRule.parentStyleSheet.cssRules, cssRule);\n\t\t\t\t\t\tcssRule.parentStyleSheet.deleteRule(index);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// New style which supports complex media queries.\n\t\t\t\t\t\tconst colorDepthMatch = (Object(cssRule.media).mediaText || '').match(/\\( *(?:color|max-color): *(48842621|70318723) *\\)/i);\n\t\t\t\t\t\tif (colorDepthMatch && colorDepthMatch.length > 1) {\n\t\t\t\t\t\t\tif (colorScheme === 'dark' && (colorDepthMatch[1] === '48842621')) {\n\t\t\t\t\t\t\t\t// preferred is dark and rule is dark.\n\t\t\t\t\t\t\t\tcssRule.media.mediaText = cssRule.media.mediaText.replace(/\\( *color: *(?:48842621) *\\)/i, `(max-color: ${colorDepthMatch[1]})`);\n\t\t\t\t\t\t\t} else if (colorScheme === 'light' && (colorDepthMatch[1] === '70318723')) {\n\t\t\t\t\t\t\t\t// preferred is light and rule is light.\n\t\t\t\t\t\t\t\tcssRule.media.mediaText = cssRule.media.mediaText.replace(/\\( *color: *(?:70318723) *\\)/i, `(max-color: ${colorDepthMatch[1]})`);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcssRule.media.mediaText = cssRule.media.mediaText.replace(/\\( *max-color: *(?:48842621|70318723) *\\)/i, `(color: ${colorDepthMatch[1]})`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} catch (e) {\n\t\t\t\tif (options.debug) {\n\t\t\t\t\tconsole.error(e);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t};\n\tconst result = Object.defineProperty(\n\t\t{ hasNativeSupport, removeListener },\n\t\t'scheme',\n\t\t{ get: () => currentColorScheme, set },\n\t);\n\n\t// initialize the color scheme using the provided value, the system value, or light\n\tlet currentColorScheme = initialColorScheme || (mediaQueryList && mediaQueryList.matches ? 'dark' : 'light');\n\n\tset(currentColorScheme);\n\n\t// listen for system changes\n\tif (mediaQueryList) {\n\t\tif ('addEventListener' in mediaQueryList) {\n\t\t\tmediaQueryList.addEventListener('change', mediaQueryListener);\n\t\t} else {\n\t\t\tmediaQueryList.addListener(mediaQueryListener);\n\t\t}\n\t}\n\n\treturn result;\n};\n\nexport default prefersColorSchemeInit;\n","/* global self,window */\nimport { default as prefersColorSchemeInit } from './browser';\n\n(function (global) {\n\tglobal.prefersColorSchemeInit = prefersColorSchemeInit;\n}('object' === typeof window && window || 'object' === typeof self && self || {}));\n"],"names":["prefersColorSchemeRegExp","prefersColorSchemeInit","initialColorScheme","options","debug","mediaQueryString","mediaQueryList","window","matchMedia","hasNativeSupport","media","mediaQueryListener","set","matches","colorScheme","currentColorScheme","result","onChange","forEach","call","document","styleSheets","styleSheet","rules","cssRules","cssRule","push","test","Object","mediaText","index","indexOf","parentStyleSheet","deleteRule","colorDepthMatch","match","length","replace","e","console","error","defineProperty","removeListener","get","addEventListener","addListener","self"],"mappings":"YACA,IAAMA,EAA2B,yBAE3BC,EAAyB,SAACC,EAAoBC,GAG7CA,IACJA,EAAU,CAAA,GAGXA,EAAU,CACTC,QAAUD,EAAQC,QAAU,GAI9B,IAAMC,EAAmB,+BACnBC,EAAkB,eAAgBC,QAAWA,OAAOC,WAAWH,GAC/DI,EAAmBH,GAAkBA,EAAeI,QAAUL,EAC9DM,EAAqB,WAC1BC,EAAKN,GAAkBA,EAAeO,QAAW,OAAS,UAOrDD,EAAM,SAACE,GACQ,SAAhBA,GAA0C,UAAhBA,IAE5BA,EADGL,GACWH,EAAeO,QAAU,OAEzB,SAIZC,IAAgBC,IACnBA,EAAqBD,EAEU,mBAApBE,EAAOC,UACjBD,EAAOC,YAIT,GAAGC,QAAQC,KAAKC,SAASC,aAAe,IAAI,SAAAC,GAC3C,IAEC,IAAMC,EAAQ,GACd,GAAGL,QAAQC,KAAKG,EAAWE,UAAY,IAAI,SAAAC,GAC1CF,EAAMG,KAAKD,MAGZF,EAAML,SAAQ,SAAAO,GAGb,GAFyBzB,EAAyB2B,KAAKC,OAAOH,EAAQf,OAAOmB,WAEvD,CACrB,IAAMC,EAAQ,GAAGC,QAAQZ,KAAKM,EAAQO,iBAAiBR,SAAUC,GACjEA,EAAQO,iBAAiBC,WAAWH,EACpC,KAAM,CAEN,IAAMI,GAAmBN,OAAOH,EAAQf,OAAOmB,WAAa,IAAIM,MAAM,sDAClED,GAAmBA,EAAgBE,OAAS,IAC3B,SAAhBtB,GAAkD,aAAvBoB,EAAgB,GAE9CT,EAAQf,MAAMmB,UAAYJ,EAAQf,MAAMmB,UAAUQ,QAAQ,gCAAhC,eAAgFH,EAAgB,GAA1H,KAC0B,UAAhBpB,GAAmD,aAAvBoB,EAAgB,GAEtDT,EAAQf,MAAMmB,UAAYJ,EAAQf,MAAMmB,UAAUQ,QAAQ,gCAAhC,eAAgFH,EAAgB,GAA1H,KAEAT,EAAQf,MAAMmB,UAAYJ,EAAQf,MAAMmB,UAAUQ,QAAQ,6CAAhC,WAAyFH,EAAgB,GAAnI,KAGF,IAMF,CAJC,MAAOI,GACJnC,EAAQC,OACXmC,QAAQC,MAAMF,EAEf,MAGGtB,EAASY,OAAOa,eACrB,CAAEhC,iBAAAA,EAAkBiC,eA5DE,WAClBpC,GACHA,EAAeoC,eAAe/B,KA2D/B,SACA,CAAEgC,IAAK,WAAA,OAAM5B,CAAb,EAAiCH,IAAAA,IAI9BG,EAAqBb,IAAuBI,GAAkBA,EAAeO,QAAU,OAAS,SAapG,OAXAD,EAAIG,GAGAT,IACC,qBAAsBA,EACzBA,EAAesC,iBAAiB,SAAUjC,GAE1CL,EAAeuC,YAAYlC,IAItBK,CACP,GChGC,iBAAoBT,QAAUA,QAAU,iBAAoBuC,MAAQA,MAAQ,IADtE7C,uBAAyBA"}
|