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. -->
This commit is contained in:
Bernardo Cardoso
2025-05-14 15:21:18 +01:00
committed by GitHub
parent 6a1382fc2c
commit f7a2d34305
3 changed files with 621 additions and 586 deletions

View File

@ -43,12 +43,25 @@
console.log('Generating SCSS variables...');
// Separate typography tokens from the rest
const typographyProperties = dictionary.allTokens.filter((prop) => prop.$type === 'typography');
const otherProperties = dictionary.allTokens.filter((prop) => prop.$type !== 'typography');
// Make sure the reused scss variables are defined first, to avoid compilation errors
const sortedProperties = [...otherProperties, ...typographyProperties];
const primitiveProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'primitives');
const scaleProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'scale');
const borderProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'border');
const semanticsProperties = dictionary.allTokens.filter((prop) => prop.path[0] === 'semantics');
const nonPrimitiveScaleBorderSemanticsProperties = dictionary.allTokens.filter(
(prop) => !['primitives', 'scale', 'border', 'semantics'].includes(prop.path[0])
);
const typographyProperties = nonPrimitiveScaleBorderSemanticsProperties.filter((prop) => prop.$type === 'typography');
const otherProperties = nonPrimitiveScaleBorderSemanticsProperties.filter((prop) => prop.$type !== 'typography');
// Order: primitives → semantics → scale → border → other → typography
const sortedProperties = [
...primitiveProperties,
...semanticsProperties,
...scaleProperties,
...borderProperties,
...otherProperties,
...typographyProperties
];
const prefixedVariables = sortedProperties.map((prop) => {
// Remove consecutive repeated words from the token name, like border-border-color

View File

@ -48,6 +48,19 @@ function removeConsecutiveRepeatedWords(str) {
return str.replace(/(\b\w+\b)(-\1)+/g, '$1');
}
// Generates a reference variable for an alias token type
// (e.g., $ion-border-default: var(--ion-border-default, #d5d5d5) → $ion-border-default: var(--ion-border-default, $ion-primitives-neutral-400))
function getAliasReferenceVariable(prop) {
if (typeof prop.$value === 'string' && prop.$value.startsWith('{') && prop.$value.endsWith('}')) {
// Remove curly braces and replace dots with dashes
let ref = prop.$value.slice(1, -1).replace(/\./g, '-');
// Remove consecutive repeated words (e.g., border-border-radius-0 → border-radius-0)
ref = removeConsecutiveRepeatedWords(ref);
return `$${variablesPrefix}-${ref}`;
}
return null;
}
// Generates a valid box-shadow value from a shadow Design Token structure
function generateShadowValue(prop, propName) {
const cssShadow = prop.$value.map(shadow => {
@ -80,17 +93,25 @@ function generateFontFamilyValue(prop, propName, variableType = 'css') {
// Generates a final value, based if the Design Token is of type color or not
function generateValue(prop, propName) {
const rgb = hexToRgb(prop.$value);
// Use the original value to detect aliases
const aliasVar = getAliasReferenceVariable({ $value: prop.original.$value });
let rgbDeclaration = '';
if (rgb) {
// If the token is color, also add a rgb variable using the same color
rgbDeclaration = `\n$${variablesPrefix}-${propName}-rgb: var(--${variablesPrefix}-${propName}-rgb, ${rgb.r}, ${rgb.g}, ${rgb.b});`;
prop.value = getRgbaValue(prop.$value);
// Always generate the main variable
let mainValue;
if (aliasVar) {
mainValue = `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${aliasVar});`;
} else {
mainValue = `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${prop.$value});`;
}
return `$${variablesPrefix}-${propName}: var(--${variablesPrefix}-${propName}, ${prop.$value});${rgbDeclaration}`;
// Always generate the -rgb variable if it's a color
const rgb = hexToRgb(prop.$value);
let rgbDeclaration = '';
if (rgb) {
rgbDeclaration = `\n$${variablesPrefix}-${propName}-rgb: var(--${variablesPrefix}-${propName}-rgb, ${rgb.r}, ${rgb.g}, ${rgb.b});`;
}
return `${mainValue}${rgbDeclaration}`;
}
// Generates a typography based css utility-class or scss variable from a typography token structure
@ -287,6 +308,7 @@ module.exports = {
generateFontFamilyValue,
generateTypographyOutput,
generateValue,
getAliasReferenceVariable,
setPrefixValue,
generateRadiusUtilityClasses,
generateColorUtilityClasses,

File diff suppressed because it is too large Load Diff