-
Notifications
You must be signed in to change notification settings - Fork 1.3k
rgb and rgba functions misinterpret CSS variables as a single value instead of their output #2251
Comments
|
@nschonni I see, thank you. Temporary workaround: Jakob E also noted that wrapping the whole function in #{' '} interpolation will work, although this approach failed in my case. |
Unless I’m mistaken the expected outcome is not possible and will not be possible with what @nschonni mentions. CSS Custom Properties are dynamic, they inherit through the DOM tree and can be set in JS at run-time, can be set on different DOM nodes by selectors, etc. Some Postcss plugins use the CSS Custom Properties syntax with This code should always throw:
And this one (a pure CSS feature) should probably be passed as-is by Sass:
But note it’s syntax from a very incomplete draft of CSS Color Module Level 4, so syntax may change and browser support is far off. (It also introduces a bunch of syntax that Sass will probably choke on, like |
@fvsch is mostly correct. The expected output is impossible because However this input should not throw. Node Sass should pass through values it doesn't understand, but are still valid CSS values. The correct output should be: :root {
--color--text: 18, 18, 26; }
body {
color: rgba(var(--color--text), 0.8); } This output is obviously jibberish, but the behaviour is correct as far as Sass is concerned. |
Fixed in 4.8.0 |
Unfortunately it doesn't seem to be fixed. I've just installed var sass = require('node-sass');
var result = sass.renderSync({
data: `body {
--c: 1,2,3;
background-color: rgb(var(--c));
}`
});
console.log(result); fails for me with the following stack trace: Ruslans-MBP:sass-test RHR$ node index.js
/Users/RHR/workspace/tmp/sass-test/node_modules/node-sass/lib/index.js:439
throw assign(new Error(), JSON.parse(result.error));
^
Error: Function rgb is missing argument $green.
at Object.module.exports.renderSync (/Users/RHR/workspace/tmp/sass-test/node_modules/node-sass/lib/index.js:439:16)
at Object.<anonymous> (/Users/RHR/workspace/tmp/sass-test/index.js:2:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:140:18)
at node.js:1043:3 @xzyfer do I miss something, do I need some special treatment to make it work? |
This only seems to work when setting css variables. In @ro0gr's example, this still causes problems. But then.. I don't understand, was this only fixed when setting css vars? Uppercase RGB seems to be working for now though. |
Can you confirm you're using 4.9.0 with npm ls node-sass? |
Looks like we don't have test for RGB with a single argument. https://github.com/sass/sass-spec/pull/1002/files It's entirely possible this specific case is not handle correctly. |
I am using It only works with:
Never with:
Had to use |
I think I'm having a related issue today. My test was:
Although, as you can test here, both
|
Same problem with latest version getting: |
For what it's worth the same problem exists using the other color functions:
Running node-sass 4.10.0 / libsass 3.5.4. |
I'm also having the issue described above, is there already a solution ? |
@Jordaneisenburger definitely not a solution, but defining custom @function rgb($r, $g: null, $b: null ) {
@if ($g == null) {
@return unquote('rgb(#{$r})');
}
@if ($g and $b) {
@return unquote('rgb(#{$r}, #{$g}, #{$b})');
}
@error "wrong number of arguments";
}
@function rgba($r, $g, $b: null, $a: null ) {
@if ($b == null) {
@return unquote('rgba(#{$r}, #{$g})');
}
@if ($b and $a) {
@return unquote('rgba(#{$r}, #{$g}, #{$b}, #{$a})');
}
@error "wrong number of arguments";
} |
@ro0gr thanks for the response, unfortunately i didn't get it to work because the rgb() functions are used inside a .css file. I've tried to include a fix.scss file with your function in it at the top of the src code but somehow didn't work. Thanks anyway! |
This issue appears to still exist in 4.12 and applies to hsl as well. html {
--hue: 210;
--background: hsl(var(--hue), 20%, 12%);
}
.testClass {
background: var(--background);
} Will always attempt to interpret the variable as SASS and throw an error that hue must be a number. |
I've gotta admit I'm surprised that this is still an issue, in v4.14.1, almost two and a half years later - using |
I'm migrating back to Sass since Stylus is deprecated and stepped into a problem that I think that it's the same as this issue. I'm trying to update an old function that I have to determine if background is dark or not and choose the text color, but I found that this not work with CSS variables.
The error is:
|
If you're out of luck with :root {
--color--text: 18, 18, 26;
}
$color-background: var(--background-color, 237, 237, 237);
body {
color: unquote("rgba(var(--color--text), 0.8)");
// or with sass variable
background: unquote("rbg(#{$color-background})");
} Use a function if you don't want to write unquote every time. :root {
--color--text: 18, 18, 26;
}
$color-background: var(--background-color, 237, 237, 237);
@function safe-rgb($var) {
@return unquote("rgb(#{$var})");
}
@function safe-rgb2($css-var) {
@return unquote("rgb(var(#{$css-var}))");
}
body {
background: safe-rgb($color-background);
color: safe-rgb2(--color-text);
} |
Working within Angular 11.0.2. Can confirm, still a problem. |
Somehow this error doesn't occur locally but then on my Heroku server (running Nuxt) the Nuxt build fails with the same $green error. Can confirm that |
@midblue I cannot remember how I resolved the green issue but I did witness it in Angular 11. You could check that node-sass version. See https://stackoverflow.com/a/11973298/1440240 Are you deploying SASS files or part of your CI or something? |
this is a show stopper for me... |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
simple take rgb value and go to rgb to hex site, then simply convert and put it in your code problem solved! |
It's 2023, this has been a major issue for 5 years. Can this issue please have more attention so it can be fixed, instead of having to specify like 4 separate color variables just to add a little transparency to your website? |
Which color --bs-bg-opacity var ? I couldn't find this variables value ! |
Example:
Expected outcome:
Actual outcome:
When using rgba():
When using rgb(), without the alpha-value:
Notes:
This works if I wrap the CSS variable values in their own rgb() function and just output those as property values, but in order to add different alpha-values on the fly, storing only the R, G, and B values in variables is a lot more flexible.
As I posted this on Twitter and instantly got a backlash: please note that of course I could use $sass-variables instead, but CSS variables are a lot better to work with for obvious reasons (mutability, element scoping).
Versions:
The text was updated successfully, but these errors were encountered: