Skip to content

Commit 19023cb

Browse files
committed
add logic to create alias tokens fallbacks
1 parent 7fec492 commit 19023cb

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

core/scripts/tokens/utils.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ function removeConsecutiveRepeatedWords(str) {
4848
return str.replace(/(\b\w+\b)(-\1)+/g, '$1');
4949
}
5050

51+
// Generates a reference variable for an alias token type
52+
function getAliasReferenceVariable(prop) {
53+
if (typeof prop.$value === 'string' && prop.$value.startsWith('{') && prop.$value.endsWith('}')) {
54+
// Remove curly braces and replace dots with dashes
55+
let ref = prop.$value.slice(1, -1).replace(/\./g, '-');
56+
// Remove consecutive repeated words (e.g., border-border-radius-0 → border-radius-0)
57+
ref = removeConsecutiveRepeatedWords(ref);
58+
return `$${variablesPrefix}-${ref}`;
59+
}
60+
return null;
61+
}
62+
5163
// Generates a valid box-shadow value from a shadow Design Token structure
5264
function generateShadowValue(prop, propName) {
5365
const cssShadow = prop.$value.map(shadow => {
@@ -80,17 +92,25 @@ function generateFontFamilyValue(prop, propName, variableType = 'css') {
8092

8193
// Generates a final value, based if the Design Token is of type color or not
8294
function generateValue(prop, propName) {
83-
const rgb = hexToRgb(prop.$value);
95+
// Use the original value to detect aliases
96+
const aliasVar = getAliasReferenceVariable({ $value: prop.original.$value });
8497

85-
let rgbDeclaration = '';
98+
// Always generate the main variable
99+
let mainValue;
100+
if (aliasVar) {
101+
mainValue = `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${aliasVar});`;
102+
} else {
103+
mainValue = `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${prop.$value});`;
104+
}
86105

106+
// Always generate the -rgb variable if it's a color
107+
const rgb = hexToRgb(prop.$value);
108+
let rgbDeclaration = '';
87109
if (rgb) {
88-
// If the token is color, also add a rgb variable using the same color
89110
rgbDeclaration = `\n$${variablesPrefix}-${propName}-rgb: var(--${variablesPrefix}-${propName}-rgb, ${rgb.r}, ${rgb.g}, ${rgb.b});`;
90-
prop.value = getRgbaValue(prop.$value);
91111
}
92112

93-
return `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${prop.$value});${rgbDeclaration}`;
113+
return `${mainValue}${rgbDeclaration}`;
94114
}
95115

96116
// Generates a typography based css utility-class or scss variable from a typography token structure
@@ -287,6 +307,7 @@ module.exports = {
287307
generateFontFamilyValue,
288308
generateTypographyOutput,
289309
generateValue,
310+
getAliasReferenceVariable,
290311
setPrefixValue,
291312
generateRadiusUtilityClasses,
292313
generateColorUtilityClasses,

0 commit comments

Comments
 (0)