css-prefers-color-scheme 4.0.0 → 6.0.2
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 +46 -0
- package/README.md +28 -16
- package/browser.js +34 -6
- package/browser.min.js +100 -1
- package/dist/browser-global.js +100 -0
- package/dist/browser-global.js.map +1 -0
- package/dist/browser.cjs +93 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.mjs +93 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/cli.cjs +3 -0
- package/dist/index.cjs +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +49 -59
- package/browser.js.map +0 -1
- package/cli.js +0 -116
- package/index.js +0 -67
- package/index.js.map +0 -1
- package/index.mjs +0 -65
- package/index.mjs.map +0 -1
- package/postcss.js +0 -38
- package/postcss.mjs +0 -36
package/index.mjs
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
const colorIndexRegExp = /((?:not )?all and )?(\(color-index: *(22|48|70)\))/i;
|
|
2
|
-
const prefersColorSchemeRegExp = /prefers-color-scheme:/i;
|
|
3
|
-
|
|
4
|
-
const prefersColorSchemeInit = initialColorScheme => {
|
|
5
|
-
const mediaQueryString = '(prefers-color-scheme: dark)';
|
|
6
|
-
const mediaQueryList = window.matchMedia && matchMedia(mediaQueryString);
|
|
7
|
-
const hasNativeSupport = mediaQueryList && mediaQueryList.media === mediaQueryString;
|
|
8
|
-
|
|
9
|
-
const mediaQueryListener = () => {
|
|
10
|
-
set(mediaQueryList.matches ? 'dark' : 'light');
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const removeListener = () => {
|
|
14
|
-
if (mediaQueryList) {
|
|
15
|
-
mediaQueryList.removeListener(mediaQueryListener);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const set = colorScheme => {
|
|
20
|
-
if (colorScheme !== currentColorScheme) {
|
|
21
|
-
currentColorScheme = colorScheme;
|
|
22
|
-
|
|
23
|
-
if (typeof result.onChange === 'function') {
|
|
24
|
-
result.onChange();
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
[].forEach.call(document.styleSheets || [], styleSheet => {
|
|
29
|
-
[].forEach.call(styleSheet.cssRules || [], cssRule => {
|
|
30
|
-
const colorSchemeMatch = prefersColorSchemeRegExp.test(Object(cssRule.media).mediaText);
|
|
31
|
-
|
|
32
|
-
if (colorSchemeMatch) {
|
|
33
|
-
const index = [].indexOf.call(cssRule.parentStyleSheet.cssRules, cssRule);
|
|
34
|
-
cssRule.parentStyleSheet.deleteRule(index);
|
|
35
|
-
} else {
|
|
36
|
-
const colorIndexMatch = (Object(cssRule.media).mediaText || '').match(colorIndexRegExp);
|
|
37
|
-
|
|
38
|
-
if (colorIndexMatch) {
|
|
39
|
-
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');
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const result = Object.defineProperty({
|
|
47
|
-
hasNativeSupport,
|
|
48
|
-
removeListener
|
|
49
|
-
}, 'scheme', {
|
|
50
|
-
get: () => currentColorScheme,
|
|
51
|
-
set
|
|
52
|
-
}); // initialize the color scheme using the provided value, the system value, or light
|
|
53
|
-
|
|
54
|
-
let currentColorScheme = initialColorScheme || (mediaQueryList && mediaQueryList.matches ? 'dark' : 'light');
|
|
55
|
-
set(currentColorScheme); // listen for system changes
|
|
56
|
-
|
|
57
|
-
if (mediaQueryList) {
|
|
58
|
-
mediaQueryList.addListener(mediaQueryListener);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export default prefersColorSchemeInit;
|
|
65
|
-
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["src/browser.js"],"sourcesContent":["const colorIndexRegExp = /((?:not )?all and )?(\\(color-index: *(22|48|70)\\))/i;\nconst prefersColorSchemeRegExp = /prefers-color-scheme:/i;\n\nconst prefersColorSchemeInit = initialColorScheme => {\n\tconst mediaQueryString = '(prefers-color-scheme: dark)';\n\tconst mediaQueryList = window.matchMedia && matchMedia(mediaQueryString);\n\tconst hasNativeSupport = mediaQueryList && mediaQueryList.media === mediaQueryString;\n\tconst mediaQueryListener = () => {\n\t\tset(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 !== 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\t[].forEach.call(styleSheet.cssRules || [], cssRule => {\n\t\t\t\tconst colorSchemeMatch = prefersColorSchemeRegExp.test(Object(cssRule.media).mediaText);\n\n\t\t\t\tif (colorSchemeMatch) {\n\t\t\t\t\tconst index = [].indexOf.call(cssRule.parentStyleSheet.cssRules, cssRule);\n\n\t\t\t\t\tcssRule.parentStyleSheet.deleteRule(index);\n\t\t\t\t} else {\n\t\t\t\t\tconst colorIndexMatch = (Object(cssRule.media).mediaText || '').match(colorIndexRegExp);\n\n\t\t\t\t\tif (colorIndexMatch) {\n\t\t\t\t\t\tcssRule.media.mediaText = (\n\t\t\t\t\t\t\t(/^dark$/i.test(colorScheme)\n\t\t\t\t\t\t\t\t? colorIndexMatch[3] === '48'\n\t\t\t\t\t\t\t: /^light$/i.test(colorScheme)\n\t\t\t\t\t\t\t\t? colorIndexMatch[3] === '70'\n\t\t\t\t\t\t\t: colorIndexMatch[3] === '22')\n\t\t\t\t\t\t\t\t? 'not all and '\n\t\t\t\t\t\t\t: ''\n\t\t\t\t\t\t) + cssRule.media.mediaText.replace(colorIndexRegExp, '$2');\n\t\t\t\t\t}\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\tmediaQueryList.addListener(mediaQueryListener);\n\t}\n\n\treturn result;\n};\n\nexport default prefersColorSchemeInit;\n"],"names":["colorIndexRegExp","prefersColorSchemeRegExp","prefersColorSchemeInit","initialColorScheme","mediaQueryString","mediaQueryList","window","matchMedia","hasNativeSupport","media","mediaQueryListener","set","matches","removeListener","colorScheme","currentColorScheme","result","onChange","forEach","call","document","styleSheets","styleSheet","cssRules","cssRule","colorSchemeMatch","test","Object","mediaText","index","indexOf","parentStyleSheet","deleteRule","colorIndexMatch","match","replace","defineProperty","get","addListener"],"mappings":"AAAA,MAAMA,gBAAgB,GAAG,qDAAzB;AACA,MAAMC,wBAAwB,GAAG,wBAAjC;;AAEA,MAAMC,sBAAsB,GAAGC,kBAAkB,IAAI;QAC9CC,gBAAgB,GAAG,8BAAzB;QACMC,cAAc,GAAGC,MAAM,CAACC,UAAP,IAAqBA,UAAU,CAACH,gBAAD,CAAtD;QACMI,gBAAgB,GAAGH,cAAc,IAAIA,cAAc,CAACI,KAAf,KAAyBL,gBAApE;;QACMM,kBAAkB,GAAG,MAAM;IAChCC,GAAG,CAACN,cAAc,CAACO,OAAf,GAAyB,MAAzB,GAAkC,OAAnC,CAAH;GADD;;QAGM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,CAAcmB,SAAd,CAAwBO,OAAxB,CAAgCnC,gBAAhC,EAAkD,IAAlD,CARJ;;;OAXH;KADD;GATD;;QAmCMgB,MAAM,GAAGW,MAAM,CAACS,cAAP,CACd;IAAE5B,gBAAF;IAAoBK;GADN,EAEd,QAFc,EAGd;IAAEwB,GAAG,EAAE,MAAMtB,kBAAb;IAAiCJ;GAHnB,CAAf,CA/CoD;;MAsDhDI,kBAAkB,GAAGZ,kBAAkB,KAAKE,cAAc,IAAIA,cAAc,CAACO,OAAjC,GAA2C,MAA3C,GAAoD,OAAzD,CAA3C;EAEAD,GAAG,CAACI,kBAAD,CAAH,CAxDoD;;MA2DhDV,cAAJ,EAAoB;IACnBA,cAAc,CAACiC,WAAf,CAA2B5B,kBAA3B;;;SAGMM,MAAP;CA/DD;;;;"}
|
package/postcss.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
2
|
-
|
|
3
|
-
var postcss$1 = _interopDefault(require('postcss'));
|
|
4
|
-
|
|
5
|
-
const mediaRegExp = /^media$/i;
|
|
6
|
-
const prefersInterfaceRegExp = /\(\s*prefers-color-scheme\s*:\s*(dark|light|no-preference)\s*\)/i;
|
|
7
|
-
const colorIndexByStyle = {
|
|
8
|
-
dark: 48,
|
|
9
|
-
light: 70,
|
|
10
|
-
'no-preference': 22
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const prefersInterfaceReplacer = ($0, style) => `(color-index: ${colorIndexByStyle[style.toLowerCase()]})`;
|
|
14
|
-
|
|
15
|
-
var postcss = postcss$1.plugin('postcss-prefers-color-scheme', opts => {
|
|
16
|
-
const preserve = 'preserve' in Object(opts) ? opts.preserve : true;
|
|
17
|
-
return root => {
|
|
18
|
-
root.walkAtRules(mediaRegExp, atRule => {
|
|
19
|
-
const {
|
|
20
|
-
params
|
|
21
|
-
} = atRule;
|
|
22
|
-
const altParams = params.replace(prefersInterfaceRegExp, prefersInterfaceReplacer);
|
|
23
|
-
|
|
24
|
-
if (params !== altParams) {
|
|
25
|
-
if (preserve) {
|
|
26
|
-
atRule.cloneBefore({
|
|
27
|
-
params: altParams
|
|
28
|
-
});
|
|
29
|
-
} else {
|
|
30
|
-
atRule.params = altParams;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
module.exports = postcss;
|
|
38
|
-
//# sourceMappingURL=postcss.js.map
|
package/postcss.mjs
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import postcss$1 from 'postcss';
|
|
2
|
-
|
|
3
|
-
const mediaRegExp = /^media$/i;
|
|
4
|
-
const prefersInterfaceRegExp = /\(\s*prefers-color-scheme\s*:\s*(dark|light|no-preference)\s*\)/i;
|
|
5
|
-
const colorIndexByStyle = {
|
|
6
|
-
dark: 48,
|
|
7
|
-
light: 70,
|
|
8
|
-
'no-preference': 22
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const prefersInterfaceReplacer = ($0, style) => `(color-index: ${colorIndexByStyle[style.toLowerCase()]})`;
|
|
12
|
-
|
|
13
|
-
var postcss = postcss$1.plugin('postcss-prefers-color-scheme', opts => {
|
|
14
|
-
const preserve = 'preserve' in Object(opts) ? opts.preserve : true;
|
|
15
|
-
return root => {
|
|
16
|
-
root.walkAtRules(mediaRegExp, atRule => {
|
|
17
|
-
const {
|
|
18
|
-
params
|
|
19
|
-
} = atRule;
|
|
20
|
-
const altParams = params.replace(prefersInterfaceRegExp, prefersInterfaceReplacer);
|
|
21
|
-
|
|
22
|
-
if (params !== altParams) {
|
|
23
|
-
if (preserve) {
|
|
24
|
-
atRule.cloneBefore({
|
|
25
|
-
params: altParams
|
|
26
|
-
});
|
|
27
|
-
} else {
|
|
28
|
-
atRule.params = altParams;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
export default postcss;
|
|
36
|
-
//# sourceMappingURL=postcss.mjs.map
|