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/token to figma alias #4697

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions design-tokens/src/token_import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ function isAlias(value: string) {
return value.toString().trim().charAt(0) === '{';
}

/**
* When pushing to Figma, we should strip off "DEFAULT" and "REST" to match simplified token outputs.
* Also, format nested roles, prominence, or interaction to hyphenated ("-") approach
* e.g. color/background/critical/weak/DEFAULT/REST --> color/background/critical-weak
* @param alias
*/
const tokenAliasToFigmaAlias = (alias: string): string => {
const isColor = /^color/.test(alias);
let adjustedName = alias;
if (isColor) {
let parts = adjustedName.split('/');
parts = parts.filter(part => !excludedNameParts.includes(part));
const section = parts.slice(0, 2).join('/');
const name = parts.slice(2).join('-');
adjustedName = `${section}${name ? `/${name}` : ''}`;
}
return adjustedName;
};

function variableValueFromToken(
token: Token,
localVariablesByCollectionAndName: {
Expand All @@ -267,11 +286,12 @@ function variableValueFromToken(
if (typeof token.$value === 'string' && isAlias(token.$value)) {
// Assume aliases are in the format {group.subgroup.token} with any number of optional groups/subgroups
// The Figma syntax for variable names is: group/subgroup/token
const value = token.$value
let value = token.$value
.trim()
.replace(/\./g, '/')
.replace(/[\{\}]/g, '');

value = tokenAliasToFigmaAlias(value);
// When mapping aliases to existing local variables, we assume that variable names
// are unique *across all collections* in the Figma file
// TO DO how will this work with our density token concept is there are repeated
Expand Down Expand Up @@ -499,19 +519,7 @@ export function generatePostVariablesPayload(
localVariablesByCollectionAndName[variableCollection?.id] || {};

Object.entries(tokens).forEach(([tokenName, token]) => {
const isColor = /^color/.test(tokenName);
// When pushing to Figma, we should strip off "DEFAULT" and "REST"
// to match simplified token outputs
// we should also format nested roles, prominence, or interaction to hyphenated ("-") approach
// e.g. color/background/critical/weak/DEFAULT/REST --> color/background/critical-weak
let adjustedName = tokenName;
if (isColor) {
let parts = tokenName.split('/');
parts = parts.filter(part => !excludedNameParts.includes(part));
const section = parts.slice(0, 2).join('/');
const name = parts.slice(2).join('-');
adjustedName = `${section}${name ? `/${name}` : ''}`;
}
const adjustedName = tokenAliasToFigmaAlias(tokenName);

const variable = localVariablesByName[adjustedName];
const variableId = variable ? variable.id : adjustedName;
Expand Down
Loading