Skip to content

Commit 883b2ec

Browse files
committed
Custom scope selector
1 parent 7272e95 commit 883b2ec

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
# 0.1.0
1+
## v0.2.0
2+
#### Added
23

3-
* Initial release.
4+
* Add support several colors in one property
5+
* Fix regexp for different color types
6+
* Add support color keywords
7+
* Add opportunity to set custom selector, which will contain variables
8+
9+
## v0.1.0
10+
Initial release

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,13 @@ Default: `false`
6565

6666
If you set true, only colors (hex, rgb, hsl, color keywords) will be extracted from values.
6767

68+
### Scope
69+
70+
Type: `string`<br>
71+
Required: `false`<br>
72+
Default: `:root`
73+
74+
You can set custom selector, which will contain variables.
75+
6876

6977
See [PostCSS] docs for examples for your environment.

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var postcss = require('postcss'),
2-
colorName = Object.keys(require('color-name'));
2+
colorNameList = Object.keys(require('color-name'));
33

44
module.exports = postcss.plugin('postcss-extract-value', function (opts) {
55
opts = opts || {};
66

77
// Cache RegExp
8-
var reColorKeywords = new RegExp(colorName.join('|'));
9-
var reCheck = new RegExp(/#\w+|rgba?|hsla?/.source + '|' + reColorKeywords.source, 'g');
8+
var reColorKeywords = new RegExp(colorNameList.join('|'));
9+
var reCheck = new RegExp(/#\w+|rgba?|hsla?/.source +
10+
'|' + reColorKeywords.source, 'g');
1011
var reCSSVariable = /var\(-{2}\w{1}[\w+-]*/;
1112
var reHex = /#(\w{6}|\w{3})/;
1213
var reRgb = /rgba?\([\d,.\s]+\)/;
@@ -17,6 +18,7 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
1718
// Options
1819
var filterByProps = opts.filterByProps;
1920
var onlyColor = opts.onlyColor;
21+
var scope = opts.scope || ':root';
2022

2123
function checkColor(value) {
2224
return reCheck.test(value);
@@ -40,8 +42,8 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
4042
return '--' + prop + '-' + num;
4143
}
4244

43-
function addCSSVariable(root, prop, value, num) {
44-
root.append(makeCSSVariable(prop, ++num) + ': ' + value);
45+
function addCSSVariable(currentScope, prop, value, num) {
46+
currentScope.append(makeCSSVariable(prop, ++num) + ': ' + value);
4547
}
4648

4749
function extractValue(decl, storePropsLink, valueFiltered) {
@@ -63,7 +65,7 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
6365

6466
css.walkRules(function (rule) {
6567

66-
if (rule.selector === ':root') {
68+
if (rule.selector === scope) {
6769
rootSel = rule;
6870
} else {
6971
rule.walkDecls(function (decl) {
@@ -103,7 +105,7 @@ module.exports = postcss.plugin('postcss-extract-value', function (opts) {
103105
});
104106

105107
if (!rootSel) {
106-
rootSel = postcss.rule({ selector: ':root' });
108+
rootSel = postcss.rule({ selector: scope });
107109
root.prepend(rootSel);
108110
}
109111
for (var prop in storeProps) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-extract-value",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "PostCSS plugin to extract values from css properties and put them into variables.",
55
"keywords": [
66
"postcss",

test.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ test('option "onlyColor"', t => {
6868
return run(t, input, output, { onlyColor: true });
6969
});
7070

71+
test('color keyword', t => {
72+
let input = `.foo {
73+
border: 1px solid black;
74+
}`;
75+
let output = `:root {
76+
--border-1: black;\n}\n.foo {
77+
border: 1px solid var(--border-1);
78+
}`;
79+
return run(t, input, output, { onlyColor: true });
80+
});
81+
7182
test('exist root element', t => {
7283
let input = `:root {
7384
--base-font-size: 16px;
@@ -99,13 +110,32 @@ test('several colors in one property', t => {
99110
return run(t, input, output, { onlyColor: true });
100111
});
101112

102-
test('color keyword', t => {
113+
test('custom element for css variables', t => {
103114
let input = `.foo {
104-
border: 1px solid black;
115+
--color-1: black;
116+
}
117+
.bar {
118+
border-color: red;
119+
}`;
120+
let output = `.foo {
121+
--color-1: black;
122+
--border-color-1: red;
123+
}
124+
.bar {
125+
border-color: var(--border-color-1);
126+
}`;
127+
return run(t, input, output, { scope: '.foo' });
128+
});
129+
130+
test('filter by color and props', t => {
131+
let input = `.foo {
132+
border: 1px solid #000;
133+
background-color: red;
105134
}`;
106135
let output = `:root {
107-
--border-1: black;\n}\n.foo {
136+
--border-1: #000;\n}\n.foo {
108137
border: 1px solid var(--border-1);
138+
background-color: red;
109139
}`;
110-
return run(t, input, output, { onlyColor: true });
140+
return run(t, input, output, { onlyColor: true, filterByProps: ['border'] });
111141
});

0 commit comments

Comments
 (0)