Skip to content

Commit

Permalink
Fix export sets (#3116)
Browse files Browse the repository at this point in the history
* initial commit to fix export token sets

* optimize getAdjustedTokenName to incorporate ignoreFirstPartForStyles logic

* remove nested ternary expression

* remove debug statement

* update tests on setValuesonNode

* update text style test

* add changeset
  • Loading branch information
akshay-gupta7 authored Sep 6, 2024
1 parent 417df53 commit e1838a3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-donkeys-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/figma-plugin": minor
---

Fixes issue where styles are not applied in Figma, when user exports Token Sets as Styles
23 changes: 17 additions & 6 deletions packages/tokens-studio-for-figma/src/plugin/TokenValueRetriever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ export class TokenValueRetriever {

public createStylesWithVariableReferences;

private getAdjustedTokenName(tokenName: string): string {
const withPrefix = [this.stylePathPrefix, tokenName].filter((n) => n).join('.');
private getAdjustedTokenName(tokenName: string): [string, string] {
const withIgnoredFirstPart = this.ignoreFirstPartForStyles && tokenName.split('.').length > 1
? tokenName.split('.').slice(1).join('.')
: tokenName;

return withPrefix;
const adjustedTokenName = [this.stylePathPrefix, tokenName].filter((n) => n).join('.');
const adjustedTokenNameWithIgnoreFirstPart = [this.stylePathPrefix, withIgnoredFirstPart].filter((n) => n).join('.');

return [adjustedTokenName, adjustedTokenNameWithIgnoreFirstPart];
}

public initiate({
Expand Down Expand Up @@ -53,10 +58,16 @@ export class TokenValueRetriever {
this.tokens = new Map<string, any>(tokens.map((token) => {
const variableId = variableReferences?.get(token.name);
// For styles, we need to ignore the first part of the token name as well as consider theme prefix
const adjustedTokenName = this.getAdjustedTokenName(token.name);
const styleId = styleReferences?.get(token.name);
const [adjustedTokenName, adjustedTokenNameWithIgnoreFirstPart] = this.getAdjustedTokenName(token.name);

const styleId = styleReferences?.get(adjustedTokenName) || styleReferences?.get(adjustedTokenNameWithIgnoreFirstPart);
const finalAdjustedTokenName = styleReferences?.has(adjustedTokenName) ? adjustedTokenName : adjustedTokenNameWithIgnoreFirstPart;

return [token.name, {
...token, variableId, styleId, adjustedTokenName,
...token,
variableId,
styleId,
adjustedTokenName: styleId ? finalAdjustedTokenName : adjustedTokenName,
}];
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ describe('Can set values on node', () => {
},
},
);
expect(setTextValuesOnTargetSpy).toHaveBeenCalled();
expect(textNodeMock).toEqual({ ...textNodeMock });
expect(setTextValuesOnTargetSpy).not.toHaveBeenCalled();
expect(textNodeMock).toEqual({ ...textNodeMock, textStyleId: '456' });
});

it('sets effectStyle if matching Style is found', async () => {
Expand Down Expand Up @@ -453,8 +453,8 @@ describe('Can set values on node', () => {
},
},
);
expect(setEffectValuesOnTargetSpy).toHaveBeenCalled();
expect(solidNodeMock).toEqual({ ...solidNodeMock, effectStyleId: '' });
expect(setEffectValuesOnTargetSpy).not.toHaveBeenCalled();
expect(solidNodeMock).toEqual({ ...solidNodeMock, effectStyleId: '123' });
});

it('sets fillStyle if matching Style', async () => {
Expand Down

0 comments on commit e1838a3

Please sign in to comment.