Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass breaking due to CSS custom properties?

Say you have the CSS custom property for a color:

--color: 255,0,0

And you want to mix it specially every time with rgb or rgba as the need requires. This is valid CSS:

rgba(var(--color), .3)

...but Sass explodes. I've been trying to see if I could write a mixin or something but I can't figure out how to get around Sass's insistence on using it's own color functions even when they are not necessary.

like image 725
dougoftheabaci Avatar asked Apr 27 '17 21:04

dougoftheabaci


2 Answers

Got it! This is a bit hacky but this allows you to create a custom function that utilizes the rgba() function with CSS custom properties (as allowed in the spec):

@function swatch($swatch-color, $swatch-alpha:1) {
  @return unquote("rgba(var(--#{$swatch-color}), #{$swatch-alpha})");
}
:root {
  --green: 0,255,0;
}
.green { color: swatch(green, .1); }

Found the key to the solution in a Sass bug report. This only works because the unquoting and interpolation bypass the default rgba() function.

like image 192
dougoftheabaci Avatar answered Oct 13 '22 02:10

dougoftheabaci


Perhaps using interpolation rgba(#{--color}, .3)?

like image 42
Brice Avatar answered Oct 13 '22 03:10

Brice