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

fix: ignore variable mode value if it is a variable alias #20

Closed

Conversation

torleifhalseth
Copy link

If the variable mode value is referencing a remote variable we should ignore it when generating the post variables payload.

@torleifhalseth
Copy link
Author

Appreciate the example project you put together @james04321! 🎖️

In our variables setup we have two libraries (Primitives, Themes). Variables in the themes library reference variables in the primitives library.

When I tested this in the context of our project the script sync-tokens-to-figma added the referenced primitive color values to the payload (variableModeValues) and then generated these variables in the themes. I modified the generatePostVariablesPayload to check if the value is of type VARIABLE_ALIAS.

Please correct me if the code suggested for ignoring these variables does not make sense or if we should fix this some other way.

Cheers 🍻

@james04321
Copy link
Collaborator

Thanks for bringing this up @torleifhalseth! I think we forgot to test for aliases to remote variables.

I'll look at this later this week when I get the chance. Does this pass the existing tests?

@torleifhalseth
Copy link
Author

Yes, existing tests are passing ✅

@torleifhalseth torleifhalseth marked this pull request as draft February 28, 2024 06:51
…exclude these from the post variables payload
@torleifhalseth

This comment has been minimized.

@torleifhalseth torleifhalseth marked this pull request as ready for review February 28, 2024 07:23
@torleifhalseth

This comment has been minimized.

@torleifhalseth torleifhalseth marked this pull request as draft February 28, 2024 07:45
@torleifhalseth
Copy link
Author

When should we add a variable value under a specific mode value to the POST payload?

Scenario 1:
The tokens reference a value alias located in a remote file (library) and we don't want to duplicate/create this variable in the current library.

Scenario 2:
The token references a value alias located in the same file (library) and the token value is different from the variable value under a specific mode in Figma. We want to update this specific value so that it is in sync with the token value.

How would we know if a token in file should create a new Figma Variable or if it is an external reference?

For now, i created the shouldGenerateVariableModeValuesThatAreMissingInFigma parameter to the generatePostVariablesPayload function. I am not sure if this is the way to go, but for now, this is the best I could come up with.

@james04321
Copy link
Collaborator

How would we know if a token in file should create a new Figma Variable or if it is an external reference?

My immediate intuition (that I haven't thought through fully yet) is that we should include remote variable aliases in variableModeValues whenever possible. That is, if a remote variable is already materialized in the current file (is referenced by another local variable), then we should allow setting aliases to this variable in the script.

However, we are limited by the API in that we can't bring in remote variables that don't already live in the current file.

When I have time later (probably next week), I want to think through how the export/import scripts work with remote variables to make sure we're handling them consistently.

In the meantime, feel free to make whatever changes you need in your own fork if you're blocked by this. We might not end up using the changes in this PR depending on where we land, so don't feel like you have to polish everything right now.

@torleifhalseth
Copy link
Author

Sounds like a good idea @james04321 . I will leave this for now and wait for your feedback/solution for working with remote variables.

@james04321
Copy link
Collaborator

Sounds like a good idea @james04321 . I will leave this for now and wait for your feedback/solution for working with remote variables.

@torleifhalseth Made a separate PR to fix this issue, which allows us to update aliases to remote variables. Let me know if this works for you: #23

james04321 added a commit that referenced this pull request Mar 19, 2024
…yncing tokens to Figma (#23)

For the `sync_tokens_to_figma.ts` script, if a tokens file has aliases to variables in remote collections, e.g. `"$value": "{name_of_variable}"`, update those alias references when needed. This issue was brought up in #20.

The previous behavior was that if a tokens file has aliases to variables in a remote collection, those aliases would cause a 400 response from the `POST /v1/files/:file_key/variables` endpoint, as the script was sending up invalid variable ids inside the alias objects.

Now, the script will match up token values with remote variable names and update aliases when needed.

Note: we are limited by the variables API in that we cannot alias variables that aren't already consumed by current file.

Demo:

Here is a demo of the `sync_tokens_to_figma.ts` script updating a Figma file that has a Semantic variables collection with aliases to variables published from another file.

https://github.com/figma/variables-github-action-example/assets/250513/592fddbc-38fe-485c-89ca-688b1777b64f

## Test Plan

- In one file, create and publish a variable collection
- In another file, create a variable collection with variables that alias to variables from the file above
- Run `npm run sync-figma-to-tokens` to export the variables from the second file into a tokens file
- Run `npm run sync-tokens-to-figma` on the exported tokens file to sync the tokens back to Figma. The result is that we should noop instead of getting a 400 error from the REST API.
@torleifhalseth
Copy link
Author

Great work @james04321 🎖️ The solution works great for us. Thank you for taking the time to follow up and fixing this.

For now, we are duplicating this code in our project, but it would be great if this was packaged at some point. Is that something you guys would consider @james04321? The same way you did with the @figma/rest-api-spec. This was a great improvement. Makes it much easier for developers to start working with the API 👍

@torleifhalseth torleifhalseth deleted the fix/ignore-variable-mode-alias branch March 21, 2024 06:37
@james04321
Copy link
Collaborator

The same way you did with the @figma/rest-api-spec. This was a great improvement. Makes it much easier for developers to start working with the API 👍

😍 Glad you are finding the spec useful!

For now, we are duplicating this code in our project, but it would be great if this was packaged at some point.

We don't have plans to do this primarily because there are a bunch of opinionated design decisions in the code such as the format of the tokens json, intentionally conservative behavior (not removing variables in the Figma file that have been removed in the tokens file), etc... and we want organizations using this code to be intentional about how they're setting up their Figma-GitHub sync to avoid messing up their Figma files.

@torleifhalseth
Copy link
Author

Ok. I understand. I guess the opinionated design decisions could be configurable, but then the code complexity increases 🤷‍♂️

This example is a great starting point for developers, and I agree that we should be intentional about how we are setting up the sync. I also think the majority of designers and developers appreciate opinionated decisions when they are described and well thought over 🚀

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

Successfully merging this pull request may close these issues.

2 participants