fix(tokens): review the generation of utility-classes (#30498)

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 some utility-classes are not as effective as they could be,
because they don't affect, for example, the --background or --color css
API's.

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Changed the way the classes are generated, so that it affects the css
custom properties as well.
- Make the background classes to only be generated for the tokens that
contain 'bg'.
- Update the style for the body background on the ionic theme (this
triggered some snapshots differences).

## 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.
-->

---------

Co-authored-by: ionitron <hi@ionicframework.com>
This commit is contained in:
Bernardo Cardoso
2025-06-24 16:01:32 +01:00
committed by GitHub
parent 5458e06742
commit 33dcf98bd2
14 changed files with 478 additions and 1159 deletions

View File

@ -142,8 +142,12 @@ function generateTypographyOutput(prop, propName, isVariable) {
// Generates a color based css utility-class from a color Design Token structure
function generateColorUtilityClasses(prop, className) {
return `.${variablesPrefix}-${className} {\n color: $${variablesPrefix}-${prop.name};\n}
.${variablesPrefix}-background-${className} {\n background-color: $${variablesPrefix}-${prop.name};\n}`;
const isBg = className.includes('bg');
const cssProp = isBg ? 'background-color' : 'color';
return `.${variablesPrefix}-${className} {
--${cssProp}: $${variablesPrefix}-${prop.name};
${cssProp}: $${variablesPrefix}-${prop.name};
}`;
}
// Generates margin and padding utility classes to match the token-agnostic
@ -264,7 +268,10 @@ function generateSpaceUtilityClasses(prop, className) {
// Generates a valid box-shadow value from a shadow Design Token structure
function generateRadiusUtilityClasses(propName) {
return `.${variablesPrefix}-${propName} {\n border-radius: $${variablesPrefix}-${propName};\n}`;
return `.${variablesPrefix}-${propName} {
--border-radius: $${variablesPrefix}-${propName};
border-radius: $${variablesPrefix}-${propName};
}`;
}
// Generates a border based css utility-class from a font Design Token structure
@ -282,7 +289,10 @@ function generateBorderUtilityClasses(prop, propName) {
default:
attribute = 'border-color';
}
return `.${variablesPrefix}-${propName} {\n ${attribute}: $${variablesPrefix}-${propName};\n}`;
return `.${variablesPrefix}-${propName} {
--${attribute}: $${variablesPrefix}-${propName};
${attribute}: $${variablesPrefix}-${propName};
}`;
}
// Generates a font based css utility-class from a font Design Token structure
@ -292,7 +302,10 @@ function generateFontUtilityClasses(prop, propName) {
// Generates a valid box-shadow value from a shadow Design Token structure
function generateShadowUtilityClasses(propName) {
return `.${variablesPrefix}-${propName} {\n box-shadow: $${variablesPrefix}-${propName};\n}`;
return `.${variablesPrefix}-${propName} {
--box-shadow: $${variablesPrefix}-${propName};
box-shadow: $${variablesPrefix}-${propName};
}`;
}
// Generates a utility class for a given token category and name