font-weight
The use-design-tokens rule checks that CSS font-weight declarations use
design token variables instead of hardcoded values. This ensures consistent
font-weight usage across the application and makes it easier to maintain design
system consistency.
This rule is autofixable and can automatically replace some font-weight values with appropriate design tokens where possible.
Examples of incorrect code for this rule
.bold-text {
font-weight: bold;
}
.bolder-text {
font-weight: bolder;
}
.lighter-text {
font-weight: lighter;
}
.custom-text {
font-weight: 400;
}
.heading {
font-weight: 600;
}
.bold-text {
font-weight: 700;
}
.light-text {
font-weight: 300;
}
.heavy-text {
font-weight: 900;
}
Examples of correct token usage for this rule
.normal-text {
font-weight: var(--font-weight);
}
.semibold-text {
font-weight: var(--font-weight-semibold);
}
.bold-text {
font-weight: var(--font-weight-bold);
}
.button-text {
font-weight: var(--button-font-weight);
}
.heading-text {
font-weight: var(--font-weight-heading);
}
/* Local CSS variables that reference valid font-weight keywords are allowed */
:root {
--custom-font-weight: normal;
}
.custom-text {
font-weight: var(--custom-font-weight);
}
/* Local CSS variables that reference valid font-weight tokens are allowed */
:root {
--custom-font-weight: var(--font-weight-semibold);
}
.custom-text {
font-weight: var(--custom-font-weight);
}
.custom-text {
font-weight: var(--custom-font-weight, var(--font-weight-semibold));
}
The rule also allows the non-token values listed for Font weight in the checked properties.
Autofix functionality
This rule can automatically fix some violations by replacing values with appropriate design tokens. Font weight in the checked properties lists every value it fixes.
Examples of autofixable violations:
/* Before */
.normal-text {
font-weight: 400;
}
/* After autofix */
.normal-text {
font-weight: normal;
}
/* Before */
.semibold-text {
font-weight: 600;
}
/* After autofix */
.semibold-text {
font-weight: var(--font-weight-semibold);
}
/* Before */
.bold-text {
font-weight: 700;
}
/* After autofix */
.bold-text {
font-weight: var(--font-weight-bold);
}