Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color maps break with different names #3132

Open
saivan opened this issue Oct 22, 2020 · 6 comments
Open

Color maps break with different names #3132

saivan opened this issue Oct 22, 2020 · 6 comments

Comments

@saivan
Copy link

saivan commented Oct 22, 2020

So I'm using a map with two different names, and my choice of name is causing the compiler to fail somehow.

input.scss

$colors: (
  blue-1: #f34f32,
  yellow-1: #f34f32,
);

@function color($color, $i, $alpha:1) {
  $chosen-color: map-get($colors, #{$color}-#{$i});
  $with-alpha: rgba($chosen-color, $alpha);
  @return $with-alpha;
}

// Uncomment this to unbreak it :D
body {
    background-color: color(blue, 1);
}

// Uncomment this to break it
// body {
//     background-color: color(yellow, 1);
// }

Actual results

So when I uncomment the "break it section" and compile it with libsass v3.5.5, I get:

argument $color of rgba($color, $alpha) must be a color

But when it's commented I get

body{background-color:#f34f32}

I can only pin it down to the variable name 🤷‍♂️

version info:

$ node-sass --version
libsass         3.5.5   (Sass Compiler) [C/C++]
@xzyfer
Copy link
Contributor

xzyfer commented Oct 22, 2020 via email

@saivan
Copy link
Author

saivan commented Oct 22, 2020

I can do that, but it's honestly a little ugly :P Would you not expect it to work without the quotes?

edit: Just for the record, quotes do fix it :)
edit: Also, I forgot to mention that this only happens with the compressed setting

@saivan
Copy link
Author

saivan commented Oct 22, 2020

I mentioned this in an edit, but this only happens with the compressed setting

@xzyfer
Copy link
Contributor

xzyfer commented Oct 22, 2020

Would you not expect it to work without the quotes?

No. This doesn't work because Sass has types. blue is of type Color, where "blue" is of type String. Doing #{$color}-#{$i} does not produce the String "blue-1" as you might expect.

You can play around with the inspect and @debug functions to find alternative ways to produce a string that matches the match look up you want.

The reason this breaks in compressed mode is that the output of Color types changes in compressed mode. The compiler produces the shortest representation of the colour i.e. "red" vs #f00, or "green" vs #0f0.

@xzyfer xzyfer closed this as completed Oct 22, 2020
@saivan
Copy link
Author

saivan commented Oct 22, 2020

I see, is there some way to coerce the color function to use a string?

@mgreter
Copy link
Contributor

mgreter commented Oct 22, 2020

IMO the following should have worked, but seems to fail anyway, therefore reopening:

$colors: (
  #{blue}-#{1}: #111,
  #{yellow}-#{1}: #333,
);

@function color($color, $i, $alpha:1) {
  $key: #{$color}-#{$i};
  $chosen-color: map-get($colors, $key);
  $with-alpha: rgba($chosen-color, $alpha);
  @return $with-alpha;
}

// Uncomment this to unbreak it :D
body {
    background-color1: color(blue, 1);
}

// Uncomment this to break it
body {
    background-color2: color(yellow, 1);
}

Alternatively you probably/might want to use a list with a real color as key:

$colors: (
  blue: (#111),
  yellow: (#333),
);

@function color($color, $i, $alpha:1) {
  $color-list: map-get($colors, $color);
  $chosen-color: nth($color-list, $i);
  $with-alpha: rgba($chosen-color, $alpha);
  @return $with-alpha;
}

// Uncomment this to unbreak it :D
body {
    background-color1: color(blue, 1);
}

// Uncomment this to break it
body {
    background-color2: color(yellow, 1);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants