1

I have a number of SASS variables that reference SVG icons. Below is a simplified example:

I would like to be able to loop through a list of strings and compose the SASS variable name in a way that's presented using the background style of the icon-checkbox class. My approach so far does not work. Can I anyone show me how/if this can be done in SASS.

$icon-trash-grey: "data:image/svg+xml;charset=UTF-8,<svg data>";
$icon-save-grey: "data:image/svg+xml;charset=UTF-8,<svg data>";

$icon-list: trash, save; // list of icon names
    
.icon-checkbox {
    each $key in $icon-list {
        background: url($icon-#{$key}-grey);
        // other styles
    }
}

Thanks in advance

2 Answers 2

1

The following SASS

$icon-trash-grey: "data:image/svg+xml;charset=UTF-8,<svg data>";
$icon-save-grey: "data:image/svg+xml;charset=UTF-8,<svg data>";  

$icon-map: (
  trash: $icon-trash-grey, 
  save:  $icon-save-grey,
);

.icon{
  @each $key, $value in $icon-map {
      &.#{$key} {
          background: url($value);
      }
  }
}

renders this CSS

.icon.trash {
  background: url("data:image/svg+xml;charset=UTF-8,<svg data>");
}
.icon.save {
  background: url("data:image/svg+xml;charset=UTF-8,<svg data>");
}

You can have a look, make edits or fork this Sassmeister

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, however the returned String is not a SASS variable name...it's just a string. My question's goal is to compose a SASS variable name.
I've updated the answer with map that references the desired variables.
1

Try this :

$icon-list : (
  "icon-trash-grey": "data:image/svg+xml;charset=UTF-8,<svg data>",
  "icon-save-grey": "data:image/svg+xml;charset=UTF-8,<svg data>"
); 

@each $key, $val in $icon-list {
  .#{$key} {
    background: url($val);
  }
}

1 Comment

Hi that works for the context you've provided but my goal is specific to composing a SASS variable name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.