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

Used color-mod() in CSS variable is not working

Open nolimitdev opened this issue 3 years ago • 18 comments

Consider this webpack example

webpack.config.js

module.exports = (env, options) => {
    return {
        module : {
            rules : [
                {
                    test : /\.css$/,
                    use : [
                        { loader : 'postcss-loader' },
                    ],
                },
            ],
        },
        plugins : [
        ]
    };
};

postcss.config.js

module.exports = (api) => {
    return {
        plugins : [
            ['postcss-custom-properties'],
            ['postcss-color-mod-function'],
        ], 
    }
};

file.css

:root {
 --myvar: color-mod(#1BB749 w(+10%) s(+1%));
}

.test1 { color: color-mod(#1BB749 w(+10%) s(+1%)); }
.test2 { color: var(--myvar); }

Output is

.test1 { color: hsl(137.69230769230768, 56.4140127389%, 46.1764705882%); }
.test2 { color: color-mod(#1BB749 w(+10%) s(+1%)); }

Why color-mod() in .test2 is not processed? I have correct plugins order where postcss-custom-properties firstly process variable and secondly postcss-color-mod-function should process color-mod() but it is not processed.

The solution is only to use postcss-color-mod-function in webpack plugins section not in module.rules section but it have negative performance impact to process whole bundle with postcss-color-mod-function during watch or dev server with hot reload. So I would like it works in module.rules section as expected. Thanks.

nolimitdev avatar Dec 02 '21 13:12 nolimitdev

Any update on this? I'm having the same issue.

HolisticPeanut avatar Jan 06 '22 07:01 HolisticPeanut

Not really, this plugin is not going to be continued since the spec has been discontinued and there are other plugins we're maintaining. I'd be keen to review something that'd fix this but I'm not so sure we'll get to do this on our own

Antonio-Laguna avatar Jan 06 '22 19:01 Antonio-Laguna

Hi,

Tried a few things, including postcss-color-function (which by the way, funnily recommends color-mod as the fix for the same problem: https://github.com/postcss/postcss-color-function/issues/49#issuecomment-392915555 => this would lead me to think this issue was not always present in color-mod)

Also noticed postcss-hexrgba has the same issue.

Finally fixed all by installing postcss-functions:

  1. Defined my own functions [using color]
import Color from 'color'

export default {
    darken: (value, frac) => Color(value).darken(frac),
    lighten: (value, frac) => Color(value).lighten(frac),
    alpha:(value, frac) => Color(value).alpha(frac).hsl()
}
  1. PostCSS plugins: loading postcss-functions after postcss-custom-properties

  2. Example in styles:

:root {
 --myvar: #1BB749;
}

.test1 { color: darken(var(--myvar), 0.2); }
.test2 { color: alpha(var(--myvar), 0.75); }

Output:

.test1 {
    color: #16923a;
}

.test2 {
    color: #1bb749bf;
}

Posted this in the hope it helps others find a quick fix to this

HolisticPeanut avatar Jan 07 '22 10:01 HolisticPeanut

@GeneralBaduar a quick question, is the var being set on another (imported) file?

Antonio-Laguna avatar Jan 07 '22 12:01 Antonio-Laguna

@GeneralBaduar a quick question, is the var being set on another (imported) file?

Yes, in my actual project I'm using a shared file imported with postcss-custom-properties:

"postcss-custom-properties": {
            preserve: false,
            importFrom: "styles/definitions/variables.pcss",
        },

HolisticPeanut avatar Jan 07 '22 12:01 HolisticPeanut

Then this might not be the case, I've just found an issue which is rather serious: https://github.com/csstools/postcss-plugins/issues/132

I've found that when combined with postcss-import, variables weren't properly right. This could be your case too but hidden by another cascade of combinations.

Releasing a bugfix asap

Antonio-Laguna avatar Jan 07 '22 12:01 Antonio-Laguna

Interesting find. Thank you for the update!

HolisticPeanut avatar Jan 07 '22 12:01 HolisticPeanut

@GeneralBaduar It has been released as 12.0.4, could you try in the event it solves the issue for you?

Antonio-Laguna avatar Jan 07 '22 17:01 Antonio-Laguna

Got version 12.0.4. I'm not sure if my project is to blame, but now I get an error when using var [* tried imported and also inline]:

:root{
    --color-red: red;
}

.test1{
    color: color-mod(var(--color-red) a(50%));
}

Previously it wouldn't convert, now it throws: postcss-color-mod-function: [...] Expected a color

Using an inline color works:

--brand-red:      color-mod(yellow blend(red 50%));

HolisticPeanut avatar Jan 07 '22 18:01 HolisticPeanut

What version were you on for postcss?

Antonio-Laguna avatar Jan 07 '22 18:01 Antonio-Laguna

Version 8.4.5

HolisticPeanut avatar Jan 07 '22 18:01 HolisticPeanut

So checking this, it could be actually conflicting since the plugin is done in a way that tries to do part of the work that custom properties does and uses a library that we don't use anymore and very old. Are you testing using main branch or are you using 3.0.3?

Antonio-Laguna avatar Jan 07 '22 19:01 Antonio-Laguna

So checking this, it could be actually conflicting since the plugin is done in a way that tries to do part of the work that custom properties does and uses a library that we don't use anymore and very old. Are you testing using main branch or are you using 3.0.3?

Using version 3.0.3

HolisticPeanut avatar Jan 07 '22 19:01 HolisticPeanut

Any progress? Same issue with "postcss-color-mod-function": "^3.0.3" and "postcss": "^8.4.12"

JackieCheung avatar Apr 01 '22 06:04 JackieCheung

@JackieCheung unfortunately no progress, this plugin is discontinued so the effort is put elsewhere.

Antonio-Laguna avatar Apr 01 '22 07:04 Antonio-Laguna

Anyone know what the most up-to-date, not-discontinued approach for shading/modifying a CSS var with PostCSS is?

i.e. in a way that transforms the value for old browsers:

input:

color: darken(var(--myVar), 0.2);

output:

color: #123456;

AlecRust avatar Apr 07 '22 15:04 AlecRust

For devs coming from SCSS and switched over to PostCSS and seeing no progress here anymore, maybe it would good to check out the lighten/darken methods from scss, reuse them and live with that for now. But this seems out of scope of this plugin and not related to any specification.

Chris2011 avatar Aug 23 '22 20:08 Chris2011

@HolisticPeanut You should totally just make a missing color functions plugin from people converting from SASS! I'm coming from SASS, and I definitely miss those color functions like blending and darken etc, etc.

cyraid avatar Jul 22 '24 06:07 cyraid