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

Is it possible to dynamically retrieve scss variable value? #2765

Closed
DavidSvetlik opened this issue Oct 27, 2019 · 2 comments
Closed

Is it possible to dynamically retrieve scss variable value? #2765

DavidSvetlik opened this issue Oct 27, 2019 · 2 comments

Comments

@DavidSvetlik
Copy link

I spent some time looking, but couldn't find any help, so I hope this issue is not duplicity and also has solution. :-)

Not sure how to properly explain this, so I will go with an example. Let's say I want to do this:

$margin: (10px, 5px);
$padding: (10px, 5px);

@mixin custom-properties($props, $index) {
	@each $prop in $props {

		#{$prop}: #{nth("$" + str-slice($prop, 3), $index)};
	}
}

:root {
	@include custom-properties((
		--margin,
		--padding
	), 1);
}

I think it's pretty obvious what I am trying to achieve here - to retrieve specific scss variable value which name is constructed "dynamically" by joining $ and string. Unfortunatelly this doesn't work. Since there is a function "global-variable-exists" that accepts argument as a string, I figure it should be somehow possible - at least to have some built in function "get-variable-value".

Thank you for any help!

@mirisuzanne
Copy link
Contributor

It's not possible to dynamically call a variable, but it is possible to dynamically call map keys. Sass maps were added to handle this use-case. Something like this:

$properties: (
  margin: (10px, 5px),
  padding: (10px, 5px),
);

@mixin custom-properties($props, $index) {
  @each $prop in $props {
    $name: str-slice($prop, 3);
    $value: map-get($properties, $name);
    #{$prop}: #{nth($value, $index)};
  }
}
@DavidSvetlik
Copy link
Author

@mirisuzanne Alright, thank you!

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