Skip to content

Commit f7a2d34

Browse files
authored
feat(tokens): create variables fallbacks based on its alias token (#30404)
Issue number: internal --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> Currently, even if a token used an alias token as its value, when we're generating the variables we wouldn't respect this connection and were simply adding the fixed final value. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Now when a token uses an alias token, we keep that relationship by making sure the alias variable is used as fallback. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. -->
1 parent 6a1382f commit f7a2d34

File tree

3 files changed

+618
-583
lines changed

3 files changed

+618
-583
lines changed

core/scripts/tokens/index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,25 @@
4343

4444
console.log('Generating SCSS variables...');
4545

46-
// Separate typography tokens from the rest
47-
const typographyProperties = dictionary.allTokens.filter((prop) => prop.$type === 'typography');
48-
const otherProperties = dictionary.allTokens.filter((prop) => prop.$type !== 'typography');
49-
50-
// Make sure the reused scss variables are defined first, to avoid compilation errors
51-
const sortedProperties = [...otherProperties, ...typographyProperties];
46+
const primitiveProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'primitives');
47+
const scaleProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'scale');
48+
const borderProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'border');
49+
const semanticsProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'semantics');
50+
const nonPrimitiveScaleBorderSemanticsProperties = dictionary.allTokens.filter(
51+
(prop) => !['primitives', 'scale', 'border', 'semantics'].includes(prop.path[0])
52+
);
53+
const typographyProperties = nonPrimitiveScaleBorderSemanticsProperties.filter((prop) => prop.$type === 'typography');
54+
const otherProperties = nonPrimitiveScaleBorderSemanticsProperties.filter((prop) => prop.$type !== 'typography');
55+
56+
// Order: primitives → semantics → scale → border → other → typography
57+
const sortedProperties = [
58+
...primitiveProperties,
59+
...semanticsProperties,
60+
...scaleProperties,
61+
...borderProperties,
62+
...otherProperties,
63+
...typographyProperties
64+
];
5265

5366
const prefixedVariables = sortedProperties.map((prop) => {
5467
// Remove consecutive repeated words from the token name, like border-border-color

core/scripts/tokens/utils.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ 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+
// (e.g., $ion-border-default: var(--ion-border-default, #d5d5d5) → $ion-border-default: var(--ion-border-default, $ion-primitives-neutral-400))
53+
function getAliasReferenceVariable(prop) {
54+
if (typeof prop.$value === 'string' && prop.$value.startsWith('{') && prop.$value.endsWith('}')) {
55+
// Remove curly braces and replace dots with dashes
56+
let ref = prop.$value.slice(1, -1).replace(/\./g, '-');
57+
// Remove consecutive repeated words (e.g., border-border-radius-0 → border-radius-0)
58+
ref = removeConsecutiveRepeatedWords(ref);
59+
return `$${variablesPrefix}-${ref}`;
60+
}
61+
return null;
62+
}
63+
5164
// Generates a valid box-shadow value from a shadow Design Token structure
5265
function generateShadowValue(prop, propName) {
5366
const cssShadow = prop.$value.map(shadow => {
@@ -80,17 +93,25 @@ function generateFontFamilyValue(prop, propName, variableType = 'css') {
8093

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

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

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

93-
return `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${prop.$value});${rgbDeclaration}`;
114+
return `${mainValue}${rgbDeclaration}`;
94115
}
95116

96117
// Generates a typography based css utility-class or scss variable from a typography token structure
@@ -287,6 +308,7 @@ module.exports = {
287308
generateFontFamilyValue,
288309
generateTypographyOutput,
289310
generateValue,
311+
getAliasReferenceVariable,
290312
setPrefixValue,
291313
generateRadiusUtilityClasses,
292314
generateColorUtilityClasses,

0 commit comments

Comments
 (0)