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

Map keys inconsistent when using css color names #94

Open
crgeary opened this issue Nov 25, 2020 · 7 comments
Open

Map keys inconsistent when using css color names #94

crgeary opened this issue Nov 25, 2020 · 7 comments

Comments

@crgeary
Copy link

crgeary commented Nov 25, 2020

I've been having an issue for a while where some of the keys in maps are imported into Sass as their respective colour names, rather than a literal string. This is causing issues when trying to access them later with map-get() as you have to know if it was a colour name, or a string to do that.

Suppose that I had the following JSON:

{
    "colors": {
        "primary": "#043876",
        "red": "#B43E36"
    }
}

It would setup a map like this:

$colors: ('primary': #043876, red: #B43E36);

Where primary is a string, but red is not. This adds all sorts of confusion due to the inconsistency in how the keys of the map are setup. Is it possible to force all keys to be strings? This might be a rather drastic breaking change, so perhaps it could exist behind an option?

Thanks 👍

@crgeary
Copy link
Author

crgeary commented Nov 25, 2020

Actually, this might be my mistake. Just tested the latest version in isolation and it works as expected. This might be an issue from an old version, or an issue I introduced somewhere.

Sorry. Will close.

@crgeary crgeary closed this as completed Nov 25, 2020
@reeseovine
Copy link

Hey, I think I'm also experiencing this issue, but I'm using the latest version (4.3.0). Were you able to fix it in your own implementation, and if so what version did you use?

@crgeary
Copy link
Author

crgeary commented Dec 25, 2020

Actually, I think you're right.

I just tried this again on my codebase and I still have the original issue. My current solution is to re-create the variable in SASS and force keys to be of a particular type. Although it would be a lot nicer not to have to do this of course.

I'm also using Dart Sass instead of Node Sass, as the latter is deprecated. However they should be fairly interchangeable.

@import "./config.json";

$colors: convert-map-keys-to-strings($colors);
@use 'sass:map';

@function convert-map-keys-to-strings($map) {
    $return: ();
    @each $k, $v in $map {
        $return: map.set($return, "#{$k}", $v);
    }
    @return $return;
}

@crgeary crgeary reopened this Dec 25, 2020
@reeseovine
Copy link

Your solution works for me! Another thing that I had to make sure of was that I was using double quotes instead of single when calling map-get. Those in conjunction do the trick.

But of course it would be nice for this to be fixed in the importer (if possible).

@reeseovine
Copy link

Another thing that I had to make sure of was that I was using double quotes instead of single

Rather, they have to match what's used in map-set in your function.

@crgeary
Copy link
Author

crgeary commented Dec 31, 2020

I made a helper function for colors as well, which I think solves the issue of needing to use the correct quote types. Although I haven't tested that as I use prettier to force double quotes.

@function get-color($color) {
    @return map-get($colors, "#{$color}");
}

@reeseovine
Copy link

I have a similar helper so I just added the quotes. Not sure exactly what it does but it works!

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

No branches or pull requests

2 participants