Skip to content

Commit d2117a7

Browse files
committed
fix: split fontWeight tokens font style properly into parent group
1 parent 01b5df5 commit d2117a7

File tree

8 files changed

+64
-61
lines changed

8 files changed

+64
-61
lines changed

.changeset/two-ads-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tokens-studio/sd-transforms': minor
3+
---
4+
5+
Properly split fontWeight tokens that contain fontStyle into the parent group. For typography tokens this was correct but for fontWeight tokens, collision could occur between the token and its sibling tokens.

package-lock.json

Lines changed: 12 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"is-mergeable-object": "^1.1.1"
4242
},
4343
"peerDependencies": {
44-
"style-dictionary": "^4.0.0"
44+
"style-dictionary": "^4.0.1"
4545
},
4646
"devDependencies": {
4747
"@changesets/cli": "^2.27.6",

src/preprocessors/add-font-styles.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,35 @@ function recurse(
6666
if (tokenType === 'typography') {
6767
const tokenTypographyValue = tokenValue as TokenTypographyValue & { fontStyle: string };
6868
if (tokenTypographyValue.fontWeight === undefined) return;
69+
6970
const fontWeight = resolveFontWeight(
7071
`${tokenTypographyValue.fontWeight}`,
7172
refCopy,
7273
usesDtcg,
7374
);
7475
const { weight, style } = splitWeightStyle(fontWeight, alwaysAddFontStyle);
75-
76-
tokenTypographyValue.fontWeight = weight;
7776
if (style) {
77+
tokenTypographyValue.fontWeight = weight;
7878
tokenTypographyValue.fontStyle = style;
7979
}
8080
} else if (tokenType === 'fontWeight') {
8181
const tokenFontWeightsValue = tokenValue as SingleFontWeightsToken['value'];
8282
const fontWeight = resolveFontWeight(`${tokenFontWeightsValue}`, refCopy, usesDtcg);
8383
const { weight, style } = splitWeightStyle(fontWeight, alwaysAddFontStyle);
8484

85-
// since tokenFontWeightsValue is a primitive (string), we have to permutate the change directly
86-
token[`${usesDtcg ? '$' : ''}value`] = weight;
8785
if (style) {
88-
(slice as DeepKeyTokenMap<false>)[`fontStyle`] = {
89-
...token,
90-
[`${usesDtcg ? '$' : ''}type`]: 'fontStyle',
91-
[`${usesDtcg ? '$' : ''}value`]: style,
86+
// since tokenFontWeightsValue is a primitive (string), we have to permutate the change directly
87+
(slice[key] as DeepKeyTokenMap<false>) = {
88+
weight: {
89+
...token,
90+
[`${usesDtcg ? '$' : ''}type`]: 'fontWeight',
91+
[`${usesDtcg ? '$' : ''}value`]: weight,
92+
},
93+
style: {
94+
...token,
95+
[`${usesDtcg ? '$' : ''}type`]: 'fontStyle',
96+
[`${usesDtcg ? '$' : ''}value`]: style,
97+
},
9298
};
9399
}
94100
}

test/integration/cross-file-refs.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ describe('cross file references', () => {
4242
it('supports cross file references e.g. expanding typography', async () => {
4343
const file = await promises.readFile(outputFilePath, 'utf-8');
4444
expect(file).to.include(`
45-
--sdTypoFontWeight: 400;
46-
--sdTypoFontStyle: italic;
45+
--sdTypoFontWeightWeight: 400;
46+
--sdTypoFontWeightStyle: italic;
4747
--sdPrimaryFont: Inter;
4848
--sdFontWeight: 800;
4949
--sdLineHeight: 1.5;
@@ -67,14 +67,14 @@ describe('cross file references', () => {
6767
--sdTestTypographyTextFontSize: 25px;
6868
--sdTestTypographyTextLineHeight: 32px;
6969
--sdTestTypographyTextFontWeight: 700;
70-
--sdWeight: 400;
71-
--sdTypoAliasFontWeight: 400;
72-
--sdTypoAliasFontStyle: italic;
70+
--sdWeightWeight: 400;
71+
--sdWeightStyle: italic;
72+
--sdTypoAliasFontWeightWeight: 400;
73+
--sdTypoAliasFontWeightStyle: italic;
7374
--sdTypo3FontFamily: Inter;
7475
--sdTypo3FontWeight: 800;
7576
--sdTypo3LineHeight: 1.5;
7677
--sdTypo3FontSize: 8px;
77-
--sdFontStyle: italic;
7878
`);
7979
});
8080
});

test/integration/expand-composition.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe('expand composition tokens', () => {
7474
--sdCompositionHeaderFontSizes: 96px;
7575
--sdCompositionHeaderFontWeights: 700;
7676
--sdTypography: italic 800 26px/1.25 Arial;
77-
--sdFontWeightRef: 800;
77+
--sdFontWeightRefWeight: 800;
78+
--sdFontWeightRefStyle: italic;
7879
--sdBorder: 4px solid #FFFF00;
7980
--sdShadowSingle: inset 0 4px 10px 0 rgba(0,0,0,0.4);
8081
--sdShadowDouble: inset 0 4px 10px 0 rgba(0,0,0,0.4), 0 8px 12px 5px rgba(0,0,0,0.4);
@@ -95,7 +96,8 @@ describe('expand composition tokens', () => {
9596
--sdCompositionHeaderFontSizes: 96px;
9697
--sdCompositionHeaderFontWeights: 700;
9798
--sdTypographyFontFamily: Arial;
98-
--sdTypographyFontWeight: 800;
99+
--sdTypographyFontWeightWeight: 800;
100+
--sdTypographyFontWeightStyle: italic;
99101
--sdTypographyLineHeight: 1.25;
100102
--sdTypographyFontSize: 26px;
101103
--sdTypographyLetterSpacing: 0rem;
@@ -104,7 +106,8 @@ describe('expand composition tokens', () => {
104106
--sdTypographyTextDecoration: none;
105107
--sdTypographyTextCase: none;
106108
--sdTypographyFontStyle: italic;
107-
--sdFontWeightRef: 800;
109+
--sdFontWeightRefWeight: 800;
110+
--sdFontWeightRefStyle: italic;
108111
--sdBorderColor: #ffff00;
109112
--sdBorderWidth: 4px;
110113
--sdBorderStyle: solid;

test/spec/preprocessors/add-font-styles.spec.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ describe('add font style', () => {
106106
fw: { value: 'SemiBold Italic', type: 'fontWeight' },
107107
}),
108108
).to.eql({
109-
fw: { value: 'SemiBold', type: 'fontWeight' },
110-
fontStyle: { value: 'italic', type: 'fontStyle' },
109+
fw: {
110+
weight: {
111+
value: 'SemiBold',
112+
type: 'fontWeight',
113+
},
114+
style: { value: 'italic', type: 'fontStyle' },
115+
},
111116
});
112117
});
113118

@@ -122,8 +127,10 @@ describe('add font style', () => {
122127
}),
123128
).to.eql({
124129
fw: {
125-
weight: { $value: 'SemiBold', $type: 'fontWeight' },
126-
fontStyle: { $value: 'italic', $type: 'fontStyle' },
130+
weight: {
131+
weight: { $value: 'SemiBold', $type: 'fontWeight' },
132+
style: { $value: 'italic', $type: 'fontStyle' },
133+
},
127134
},
128135
});
129136
});
@@ -244,7 +251,7 @@ describe('add font style', () => {
244251
});
245252
});
246253

247-
it('handlestinvalid fontweight structures e.g. mixing token group / token, also handles DTCG format', () => {
254+
it('handles invalid fontweight structures e.g. mixing token group / token, also handles DTCG format', () => {
248255
// @ts-expect-error aligned types already here
249256
const tokens = {
250257
foo: {
@@ -277,7 +284,7 @@ describe('add font style', () => {
277284
thing: {
278285
$type: 'typography',
279286
$value: {
280-
fontWeight: '700',
287+
fontWeight: '{foo}',
281288
},
282289
},
283290
});

test/spec/register.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const tokens = {
178178
},
179179
type: 'typography',
180180
},
181-
'shadow-blur': {
181+
blur: {
182182
value: '10',
183183
type: 'sizing',
184184
},
@@ -187,7 +187,7 @@ const tokens = {
187187
{
188188
x: '0',
189189
y: '4',
190-
blur: '{shadow-blur}',
190+
blur: '{blur}',
191191
spread: '0',
192192
color: 'rgba(0,0,0,0.4)',
193193
type: 'innerShadow',
@@ -337,7 +337,7 @@ describe('register', () => {
337337
--fontSizesH6: 16px;
338338
--fontSizesBody: 16px;
339339
--heading6: 700 16px/1 'Arial Black', 'Suisse Int\\'l', sans-serif;
340-
--shadowBlur: 10px;
340+
--blur: 10px;
341341
--shadow: inset 0 4px 10px 0 rgba(0,0,0,0.4);
342342
--borderWidth: 5px;
343343
--border: 5px solid #000000;
@@ -352,7 +352,7 @@ describe('register', () => {
352352
tokens,
353353
preprocessors: ['tokens-studio'],
354354
expand: {
355-
include: ['border', 'boxShadow'],
355+
include: ['border', 'shadow'],
356356
},
357357
platforms: {
358358
compose: {
@@ -386,6 +386,7 @@ import androidx.compose.ui.graphics.Color
386386
import androidx.compose.ui.unit.*
387387
388388
object bar {
389+
val blur = 10px
389390
val borderColor = #000000
390391
val borderStyle = solid
391392
val borderWidth = 5px
@@ -423,12 +424,11 @@ Arial Black, Suisse Int'l, sans-serif
423424
val lineHeightsHeading = 1.1
424425
val opacity = 0.25
425426
val shadowBlur = 10px
426-
val shadowBlur = 10px
427427
val shadowColor = rgba(0,0,0,0.4)
428+
val shadowOffsetX = 0
429+
val shadowOffsetY = 4px
428430
val shadowSpread = 0
429431
val shadowType = innerShadow
430-
val shadowX = 0
431-
val shadowY = 4
432432
/** You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
433433
val spacingMultiValue = 8px 64px
434434
val spacingSm = 8px

0 commit comments

Comments
 (0)