Skip to main content

I am using Figma variables API to access the value for a variable which is aliased to variable from another collection.


E.g.

variable_b is aliased to variable_a and I want to resolve it to the final value, how do I know which value should I pick out of the two values that are available under “valuesByMode” for variable_a?


variable_a: {
"id": "VariableID:302:562"
"valuesByMode": {
"302:0": {
"r": 0.20000000298023224,
"g": 0.20000000298023224,
"b": 0.20000000298023224,
"a": 1
},
"365:2": {
"r": 0.20000000298023224,
"g": 0.20000000298023224,
"b": 0.20000000298023224,
"a": 1
}
}
}

variable_b: {
"id": "VariableID:302:700"
valuesByMode: {
"302:2": {
"type": "VARIABLE_ALIAS",
"id": "VariableID:302:562"
},
"310:3": {
"type": "VARIABLE_ALIAS",
"id": "VariableID:302:562"
}
}

The resolved value depends on the node that consumes the variable, so you’ll need to use variable.resolveForConsumer(node): Variable | Plugin API


Here’s an example usage: Working with Variables | Plugin API


Got it. My use case is to get the list of all the variables and their corresponding values irrespective of the active mode. I was just curious if there is an association b/w a variable alias and the mode of the referred variable and it seems like there is not, it will depend on the context in which they are being used.

For my use case, I will resolve the alias by mapping it to the variable value for each available mode separately.


Thanks for your reply!


Yeah, variable aliases point to variables, not modes within variables.


It’s actually impossible to statically determine the resolved value for a variable when there are multiple modes involved (either in the variable itself or in any variables in the alias chain). The reason is that the consuming node can have any combination of explicit or inherited variable modes per collection assigned to it (Shared Node Properties | Plugin API), so the resolved value will change depending on if the consuming node has:



  • CollectionA, Mode A1; Collection B, Mode B1

  • CollectionA, Mode A2; Collection B, Mode B1

  • CollectionA, Mode A1; Collection B, Mode B2

  • CollectionA, Mode A2; Collection B, Mode B2


if you have two collections with two modes each.


Totally, it is crystal clear now. Thanks again!


Reply