Skip to content

Commit 71d9d61

Browse files
joeybakerromainmenke
authored andcommitted
add: ability to set variables after initial load
When using this module as part of a live-reload system, devs might change the variable list after the module has been loaded. This adds a `setVariables` method on the plugin to enable that functionality.
1 parent dbac0cf commit 71d9d61

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed

plugins/postcss-custom-properties/index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,37 @@ function resolveValue(value, variables, result, decl) {
126126
return results
127127
}
128128

129+
function prefixVariables(variables) {
130+
var prefixedVariables = {}
131+
132+
if (!variables) {
133+
return prefixVariables
134+
}
135+
136+
Object.keys(variables).forEach(function(name) {
137+
var val = variables[name]
138+
if (name.slice(0, 2) !== "--") {
139+
name = "--" + name
140+
}
141+
prefixedVariables[name] = String(val)
142+
})
143+
144+
return prefixedVariables
145+
}
146+
129147
/**
130148
* Module export.
131149
*/
132150

133151
module.exports = postcss.plugin("postcss-custom-properties", function(options) {
134-
return function(style, result) {
135-
options = options || {}
136-
var variables = {}
137-
if (options.variables) {
138-
Object.keys(options.variables).forEach(function(name) {
139-
var val = options.variables[name]
140-
if (name.slice(0, 2) !== "--") {
141-
name = "--" + name
142-
}
143-
variables[name] = String(val)
144-
})
145-
}
152+
options = options || {}
153+
154+
function setVariables(variables) {
155+
options.variables = prefixVariables(variables)
156+
}
157+
158+
function plugin(style, result) {
159+
var variables = prefixVariables(options.variables)
146160
var strict = options.strict === undefined ? true : options.strict
147161
var appendVariables = options.appendVariables
148162
var preserve = options.preserve
@@ -268,4 +282,8 @@ module.exports = postcss.plugin("postcss-custom-properties", function(options) {
268282
}
269283
}
270284
}
285+
286+
plugin.setVariables = setVariables
287+
288+
return plugin
271289
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
:root {
2+
--test-one: local;
3+
--test-two: local;
4+
}
5+
6+
div {
7+
p: var(--test-one);
8+
p: var(--test-two);
9+
p: var(--test-three);
10+
p: var(--test-varception);
11+
p: var(--test-jsception);
12+
p: var(--test-num);
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
div {
2+
p: js-one;
3+
p: js-two;
4+
p: js-three;
5+
p: js-one;
6+
p: js-one;
7+
p: 1;
8+
}

plugins/postcss-custom-properties/test/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ test(
110110
}
111111
)
112112

113+
test("allows users to programatically change the variables", function(t) {
114+
var variables = {
115+
"--test-one": "js-one",
116+
"--test-two": "js-two",
117+
"--test-three": "js-three",
118+
"--test-varception": "var(--test-one)",
119+
"--test-jsception": "var(--test-varception)",
120+
"--test-num": 1,
121+
}
122+
var plugin = customProperties()
123+
var name = "js-override"
124+
var expected = fs.readFileSync(fixturePath(name + ".expected"), "utf8").trim()
125+
var actual
126+
127+
plugin.setVariables(variables)
128+
129+
actual = postcss(plugin)
130+
.process(fixture(name), {from: fixturePath(name)}).css.trim()
131+
132+
t.equal(
133+
actual, expected,
134+
"processed fixture '" + name + "' should be equal to expected output"
135+
)
136+
137+
t.end()
138+
})
139+
113140
test("removes variable properties from the output", function(t) {
114141
compareFixtures(t, "remove-properties")
115142
t.end()

0 commit comments

Comments
 (0)