postcss-color-mod-function icon indicating copy to clipboard operation
postcss-color-mod-function copied to clipboard

Using custom properties with adjusters results in `Expected a color`

Open n00b2pr0 opened this issue 8 years ago • 16 comments

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.

n00b2pr0 avatar Apr 13 '18 01:04 n00b2pr0

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.

jonathantneal avatar Apr 14 '18 04:04 jonathantneal

Closing due to inactivity. I hope my previous response helped you. If not, feel free to respond at your convenience. 😄

jonathantneal avatar Apr 23 '18 17:04 jonathantneal

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' */
}

:-(

erikmoren avatar Jun 01 '18 08:06 erikmoren

@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

fmal avatar Jun 11 '18 08:06 fmal

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.

mrmckeb avatar Jul 26 '18 19:07 mrmckeb

Still having the same problem guys. This library isn't usable right now.

JamesTheHacker avatar Aug 13 '18 10:08 JamesTheHacker

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 ^

jonathantneal avatar Aug 13 '18 11:08 jonathantneal

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.

JamesTheHacker avatar Aug 13 '18 12:08 JamesTheHacker

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

  44 |     &:focus {
  45 |         border: 1px solid var(--electric-blue);
> 46 |         background: color-mod(var(--electric-blue) blend(red 50%)) !important;
     |                               ^
  47 |         outline: 1px solid var(--electric-blue);
  48 |     }


    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)

JamesTheHacker avatar Aug 13 '18 16:08 JamesTheHacker

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.

JamesTheHacker avatar Aug 13 '18 16:08 JamesTheHacker

Got it to work. No idea how. I cleaned the project, refactored, cleared cache, deleted node_modules and reinstalled. Now works :S

JamesTheHacker avatar Aug 13 '18 17:08 JamesTheHacker

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.

JamesTheHacker avatar Aug 13 '18 20:08 JamesTheHacker

@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 avatar Aug 31 '18 08:08 Rendalf

@Rendalf do you have a solution for this problem?

jbibla avatar Apr 19 '20 16:04 jbibla

hello every one, how fix this problem?

myfreax avatar Dec 28 '20 14:12 myfreax

@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.

Rendalf avatar Dec 28 '20 20:12 Rendalf