Using custom properties with adjusters results in `Expected a color`
I'm seeing an Expected a color error when trying to use custom properties with adjusters.
Example:
:root {
--color: blue;
}
.test {
color: color-mod(var(--color) shade(20%));
}
I'm using postcss-preset-env as well, so it's possible it's misconfigured or need to use transformVars with the color-mod feature.
That reduced test case should work. For instance, when I test that code @ https://jonathantneal.github.io/postcss-preset-env/ I get the following result:
:root {
--color: blue;
}
.test {
color: rgb(0, 0, 204);
color: rgb(0, 0, 204);
}
Do you have a project to share where I could see this bug in action? Then I could look into this further with you.
Closing due to inactivity. I hope my previous response helped you. If not, feel free to respond at your convenience. 😄
Hi, I get a similar error ('Expected a color') when using color-mod:
This works:
/* test.css */
:root {
--misty: #C8C8C8;
}
.test {
color: color-mod(var(--misty) a(0.6));
}
But it does not work when omitting the root declaration, and instead using a postcss-custom-properties variable in postcss.config.js:
/* postcss.config.js */
module.exports = {
plugins: {
'postcss-import': {},
'postcss-preset-env': {
stage: 0
},
'postcss-custom-properties': {
preserve: true,
variables: {
misty: '#C8C8C8'
}
}
}
}
/* test.css */
.test {
color: color-mod(var(--misty) a(0.6)); /* Gives error: 'Expected a color' */
}
:-(
@jonathantneal i'm having the same problem as @erikmoren, since i'm using variables in postcss-custom-properties config to inject custom properties. Is there any way to make it work with this plugin?
EDIT: as a workaround, i'm setting preserve option of postcss-custom-properties to true, this way the variables are already resolved whenpostcss-color-mod-function parses stylesheet
Module build failed: Syntax Error
(14:31) Expected a color
12 |
13 | &:disabled {
> 14 | background-color: color-mod(var(--color-brandGreen) lightness(50%));
| ^
15 | }
16 | }
I'm also seeing this issue.
Still having the same problem guys. This library isn't usable right now.
Yes, the issue is that you have unresolved custom properties. I believe this is covered elsewhere in the issue tracker.
Look at the CSS you are passing to this plugin before it runs — do you see colors like var(--foo) used within color-mod without :root { --foo: value } like declarations for it to read? If so, now you know the problem.
If you’re using custom configurations in postcss-custom-properties to slip in variables, be careful that you are not expecting this plugin to know about those configurations for that plugin.
@JamesTheHacker, @mrmckeb, @erikmoren ^
I'm switching over from Parcel to Webpack due to other issues but I'll retry. So basically this functionality (CSS variables) should work provided my config is correct?
I import my theme.css file right at the top before anything else. This file consists purely of variables contained within a :root block. Which is why I'm confused.
Ok, all setup with Webpack. Problem still exists as expected.
The custom property is resolved. I use it in the lines above:
&:focus {
border: 1px solid var(--electric-blue);
outline: 1px solid var(--electric-blue);
background: color-mod(var(--electric-blue) blend(red 50%)) !important;
}
Both border and outline are --electric-blue and the correct color. But, the color-mod still fails with Expected a color. Here is the full error:
Uncaught Error: Module build failed (from ./node_modules/postcss-loader/src/index.js):
SyntaxError
(46:31) Expected a color
[90m 44 | [39m &[33m:[39mfocus [33m{[39m
[90m 45 | [39m border[33m:[39m 1px solid [36mvar[39m[36m(--electric-blue)[39m[33m;[39m
[31m[1m>[22m[39m[90m 46 | [39m background[33m:[39m [36mcolor-mod[39m[36m([39m[36mvar[39m[36m(--electric-blue)[39m [36mblend[39m[36m(red 50%)[39m[36m)[39m !important[33m;[39m
[90m | [39m [31m[1m^[22m[39m
[90m 47 | [39m outline[33m:[39m 1px solid [36mvar[39m[36m(--electric-blue)[39m[33m;[39m
[90m 48 | [39m [33m}[39m
at eval (base.css?./node_modules/css-loader??ref--4-1!./node_modules/postcss-loader/src:1)
at Object../node_modules/css-loader/index.js?!./node_modules/postcss-loader/src/index.js!./src/css/base.css (index.js:154)
at __webpack_require__ (index.js:20)
at eval (base.css:2)
at Object../src/css/base.css (index.js:691)
at __webpack_require__ (index.js:20)
at eval (index.js:10)
at Module../src/index.js (index.js:736)
at __webpack_require__ (index.js:20)
at eval (webpack:///multi_(:1313/webpack)-dev-server/client?:2:18)
I created a new repo and attempted to reproduce. I couldn't! These are the fun hard to track bugs. At least I know the plugin works fine. Has to be a source order issue because when I created the new repo I copied my configs over and installed the same plugins and the same plugin order.
Got it to work. No idea how. I cleaned the project, refactored, cleared cache, deleted node_modules and reinstalled. Now works :S
Now it's back in a different file lmfao! I don't understand, the new file with the problem is included immediately after the file with the working color-mod.
@jonathantneal
It happens when postcss-preset-env disables custom-properties plugin based on .browserslistrc file.
My browserslist config:
and_chr >= 49
chrome >= 49
edge >= 16
firefox >= 52
ios_saf >= 10.2
safari >= 10
Since each browser from the list supports custom properties, postcss doesn't handle them, and this error occures.
@Rendalf do you have a solution for this problem?
hello every one, how fix this problem?
@jbibla @huangyanxiong01 Postcss handles custom props and color-mod during compilation. So if you need to change custom props during runtime, you shouldn't use them in color-mod function. Also you can fix it by forcing plugin to handle custom css props, but you won't be able to change them in runtime.
Btw color-mod is deprecated spec, so I don't recommend to use it.