MadLittleMods / postcss-css-variables

PostCSS plugin to transform CSS Custom Properties(CSS variables) syntax into a static representation
https://madlittlemods.github.io/postcss-css-variables/playground/
Other
536 stars 62 forks source link

Priority of js defined variables over css defined variables #86

Closed 2beers closed 5 years ago

2beers commented 5 years ago

Is there an option that will enable priority for js defined variables over css ones? For example in this css

:root {
    --first-color: red;
}
#box {
    background-color: var(--first-color);
}
cssvariables({
    variables: {
        "--first-color": "green"
    }
})

will output #box{background-color:red} and I expected to change the color to green as provided from js options

Thanks

MadLittleMods commented 5 years ago

@2beers JS-defined variables just get put in a :root at the top

You can add the isImportant option the variable and I think it should take priority, https://github.com/MadLittleMods/postcss-css-variables#variables-default-

cssvariables({
    variables: {
        "--first-color": {
            value: "green",
            isImportant: true
        }
    }
})
2beers commented 5 years ago

Thanks @MadLittleMods . I've tried using isImportant but it returns an error. I tried with both postcss 7.0.5 and 6.0.23 and is the same error. I'm using node v8.11.1

(node:8256) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'parent' of undefined
    at findNodeAncestorWithSelector (D:\work\htdocs\project\node_modules\postcss-css-variables\lib\find-node-ancestor-with-selector.js:11:20)
    at D:\work\htdocs\project\node_modules\postcss-css-variables\lib\resolve-value.js:86:33
    at String.replace (<anonymous>)
    at resolveValue (D:\work\htdocs\project\node_modules\postcss-css-variables\lib\resolve-value.js:44:34)
    at resolveDecl (D:\work\htdocs\project\node_modules\postcss-css-variables\lib\resolve-decl.js:90:44)
    at D:\work\htdocs\project\node_modules\postcss-css-variables\index.js:251:7
    at Array.forEach (<anonymous>)
    at D:\work\htdocs\project\node_modules\postcss-css-variables\index.js:248:33
    at Array.forEach (<anonymous>)
    at D:\work\htdocs\project\node_modules\postcss-css-variables\index.js:247:18
MadLittleMods commented 5 years ago

@2beers Thanks for the heads-up 👍. Working on a fix now which will probably ship today in a bit, https://github.com/MadLittleMods/postcss-css-variables/pull/87

MadLittleMods commented 5 years ago

@2beers

postcss-css-variables@0.11.0 published to npm 🚀, https://www.npmjs.com/package/postcss-css-variables

2beers commented 5 years ago

@MadLittleMods Thanks Eric for your help. Just tested and works great now. Great job