Skip to content

Commit 1222a32

Browse files
committed
chore: 🔥 Remove autogenerated typography classes (#2429)
resolves #2392 - Removed `typography` in name from typography CSS variables (This touched a lot of files). - Removed generated CSS classes. - Replaced with deconstructed CSS variable for each prop. - Replicated current classes with new CSS variables to make sure everything was still working. - ⚠️ Paragraph typography variants, `short` & `long` needs to be separate classes to support `@composes` as it colides with other components `variant`. - For example `Button` `variant`. - I would think this is a minor tradeoff. - Saving rewrite of typography classes to new CSS convention for separate PR as this is big enough. - Concatenated `base` layer with `utilities` and `typography` Look at "old" changes in commit 2e0c899 on how `Button` and `Paragraph` can be updated to follow the new convention.
1 parent 69cf96a commit 1222a32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4511
-4900
lines changed

apps/storefront/tokens/altinn/dark.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/altinn/light.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/digdir/dark.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/digdir/light.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/portal/dark.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/portal/light.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/uutilsynet/dark.ts

+248-62
Large diffs are not rendered by default.

apps/storefront/tokens/uutilsynet/light.ts

+248-62
Large diffs are not rendered by default.

packages/cli/src/tokens/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function buildTokens(options: Options): Promise<void> {
4646

4747
const colorModeConfigs = getConfigs(configs.colorModeVariables, outPath, tokensDir, colormodeThemes);
4848
const semanticConfigs = getConfigs(configs.semanticVariables, outPath, tokensDir, semanticThemes);
49-
const typographyConfigs = getConfigs(configs.typographyCSS, outPath, tokensDir, typographyThemes);
49+
const typographyConfigs = getConfigs(configs.typographyVariables, outPath, tokensDir, typographyThemes);
5050
const storefrontConfigs = getConfigs(configs.typescriptTokens, storefrontOutDir, tokensDir, colormodeThemes);
5151

5252
if (typographyConfigs.length > 0) {

packages/cli/src/tokens/build/configs.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { register } from '@tokens-studio/sd-transforms';
1+
import { expandTypesMap, register } from '@tokens-studio/sd-transforms';
22
import type { ThemeObject } from '@tokens-studio/types';
33
import * as R from 'ramda';
44
import StyleDictionary from 'style-dictionary';
@@ -7,7 +7,7 @@ import { outputReferencesFilter } from 'style-dictionary/utils';
77

88
import * as formats from './formats/css.js';
99
import { jsTokens } from './formats/js-tokens.js';
10-
import { nameKebab, sizeRem, typographyShorthand } from './transformers.js';
10+
import { nameKebab, sizeRem, typographyName } from './transformers.js';
1111
import { permutateThemes as permutateThemes_ } from './utils/permutateThemes.js';
1212
import type { PermutatedThemes } from './utils/permutateThemes.js';
1313
import { pathStartsWithOneOf, typeEquals } from './utils/utils.js';
@@ -25,7 +25,7 @@ const fileHeader = () => [`These files are generated from design tokens defind u
2525

2626
StyleDictionary.registerTransform(sizeRem);
2727
StyleDictionary.registerTransform(nameKebab);
28-
StyleDictionary.registerTransform(typographyShorthand);
28+
StyleDictionary.registerTransform(typographyName);
2929

3030
StyleDictionary.registerFormat(jsTokens);
3131
StyleDictionary.registerFormat(formats.colormode);
@@ -38,7 +38,7 @@ const dsTransformers = [
3838
'ts/size/px',
3939
sizeRem.name,
4040
'ts/typography/fontWeight',
41-
typographyShorthand.name,
41+
typographyName.name,
4242
'ts/color/modifiers',
4343
'ts/color/css/hexrgba',
4444
'ts/size/lineheight',
@@ -149,7 +149,7 @@ export const semanticVariables: GetConfig = ({ outPath, theme }) => {
149149
format: formats.semantic.name,
150150
filter: (token) =>
151151
(!token.isSource || isCalculatedToken(token)) &&
152-
!typeEquals(['color', 'fontWeight', 'fontFamily'], token),
152+
!typeEquals(['color', 'fontWeight', 'fontFamily', 'typography'], token),
153153
},
154154
],
155155
options: {
@@ -200,14 +200,18 @@ export const typescriptTokens: GetConfig = ({ mode = 'unknown', outPath, theme }
200200
};
201201
};
202202

203-
export const typographyCSS: GetConfig = ({ outPath, theme, typography }) => {
203+
export const typographyVariables: GetConfig = ({ outPath, theme, typography }) => {
204204
const selector = `${typography === 'primary' ? ':root, ' : ''}[data-ds-typography="${typography}"]`;
205205
const layer = `ds.theme.typography.${typography}`;
206206

207207
return {
208208
usesDtcg: true,
209209
log: { verbosity: 'silent' },
210210
preprocessors: ['tokens-studio'],
211+
expand: {
212+
include: ['typography'],
213+
typesMap: { ...expandTypesMap, typography: { ...expandTypesMap.typography, letterSpacing: 'dimension' } },
214+
},
211215
platforms: {
212216
css: {
213217
prefix,
@@ -216,7 +220,14 @@ export const typographyCSS: GetConfig = ({ outPath, theme, typography }) => {
216220
layer,
217221
buildPath: `${outPath}/${theme}/`,
218222
basePxFontSize,
219-
transforms: [nameKebab.name, 'ts/size/px', sizeRem.name, 'ts/size/lineheight', 'ts/typography/fontWeight'],
223+
transforms: [
224+
nameKebab.name,
225+
'ts/size/px',
226+
sizeRem.name,
227+
'ts/size/lineheight',
228+
'ts/typography/fontWeight',
229+
typographyName.name,
230+
],
220231
files: [
221232
{
222233
destination: `typography/${typography}.css`,

packages/cli/src/tokens/build/formats/css.ts

+17-109
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ export const colormode: Format = {
3131
usesDtcg,
3232
});
3333

34-
const colorSchemeProperty = mode_ === 'dark' || mode_ === 'light' ? `color-scheme: ${mode_};\n` : '';
34+
const colorSchemeProperty = mode_ === 'dark' || mode_ === 'light' ? `\n color-scheme: ${mode_};\n` : '';
3535

36-
const content = `{\n${allTokens.map(format).join('\n')}\n${colorSchemeProperty}}\n`;
36+
const formattedTokens = dictionary.allTokens.map(format).join('\n');
37+
const content = `{\n${formattedTokens}\n${colorSchemeProperty}}\n`;
3738
const autoSelectorContent = ['light', 'dark'].includes(mode_) ? prefersColorScheme(mode_, content) : '';
3839
const body = R.isNotNil(layer)
3940
? `@layer ${layer} {\n${selector} ${content} ${autoSelectorContent}\n}\n`
@@ -48,7 +49,6 @@ const calculatedVariable = R.pipe(R.split(/:(.*?);/g), (split) => `${split[0]}:
4849
export const semantic: Format = {
4950
name: 'ds/css-semantic',
5051
format: async ({ dictionary, file, options, platform }) => {
51-
const { allTokens } = dictionary;
5252
const { outputReferences, usesDtcg } = options;
5353
const { selector, isCalculatedToken, layer } = platform;
5454

@@ -61,7 +61,7 @@ export const semantic: Format = {
6161
usesDtcg,
6262
});
6363

64-
const formatTokens = R.map((token: TransformedToken) => {
64+
const formattedTokens = R.map((token: TransformedToken) => {
6565
const originalValue = getValue<string>(token.original);
6666

6767
if (
@@ -75,50 +75,19 @@ export const semantic: Format = {
7575
}
7676

7777
return format(token);
78-
});
78+
}, dictionary.allTokens);
7979

80-
const formattedVariables = formatTokens(allTokens);
81-
const content = `{\n${formattedVariables.join('\n')}\n}\n`;
80+
const content = `{\n${formattedTokens.join('\n')}\n}\n`;
8281
const body = R.isNotNil(layer) ? `@layer ${layer} {\n${selector} ${content}\n}\n` : `${selector} ${content}\n`;
8382

8483
return header + body;
8584
},
8685
};
8786

88-
type Typgraphy = {
89-
fontWeight: string;
90-
fontSize: string;
91-
lineHeight: number;
92-
fontFamily: string;
93-
};
94-
95-
type ProcessedTokens = { variables: string[]; classes: string[] };
96-
97-
const sortByType = R.sortBy<TransformedToken>((token) => token?.$type === 'typography');
98-
const getVariableName = R.pipe<string[], string[], string, string, string, string>(
99-
R.split(':'),
100-
R.head,
101-
R.defaultTo(''),
102-
R.trim,
103-
(name) => `var(${name})`,
104-
);
105-
106-
const bemify = R.pipe(
107-
(path: string[]) => {
108-
const filteredPath = path.filter((p) => p !== 'typography');
109-
const withPrefix = R.concat([prefix], R.remove(0, 0, filteredPath));
110-
const [rest, last] = R.splitAt(-1, withPrefix);
111-
112-
const className = `${rest.join('-')}--${R.head(last)}`;
113-
return className;
114-
},
115-
R.trim,
116-
R.toLower,
117-
);
118-
119-
const classSelector = R.pipe(R.prop('path'), bemify);
120-
const sortTypographyLast = R.sortWith<TransformedToken>([
121-
R.ascend((token) => (typeEquals('typography')(token) ? 1 : 0)),
87+
// Predicate to filter tokens with .path array that includes both typography and fontFamily
88+
const typographyFontFamilyPredicate = R.allPass([
89+
R.pathSatisfies(R.includes('typography'), ['path']),
90+
R.pathSatisfies(R.includes('fontFamily'), ['path']),
12291
]);
12392

12493
export const typography: Format = {
@@ -136,74 +105,13 @@ export const typography: Format = {
136105
usesDtcg,
137106
});
138107

139-
const sortedTokens = sortTypographyLast(dictionary.allTokens);
140-
141-
const formattedTokens = R.pipe(
142-
sortByType,
143-
R.reduce<TransformedToken, ProcessedTokens>(
144-
(acc, token) => {
145-
if (typeEquals('fontweight', token)) {
146-
const className = `
147-
.${classSelector(token)} {
148-
font-weight: ${getValue<string>(token)};
149-
}`;
150-
151-
return Object.assign(acc, {
152-
variables: [...acc.variables, format(token)],
153-
classes: [...acc.classes, className],
154-
});
155-
}
156-
157-
if (typeEquals('lineheight', token)) {
158-
const className = `
159-
.${classSelector(token)} {
160-
line-height: ${getValue<string>(token)};
161-
}`;
162-
163-
return Object.assign(acc, {
164-
variables: [...acc.variables, format(token)],
165-
classes: [...acc.classes, className],
166-
});
167-
}
168-
169-
if (typeEquals('typography', token)) {
170-
let references: TransformedToken[] = [];
171-
try {
172-
references = getReferences(getValue<Typgraphy>(token.original), dictionary.tokens);
173-
} catch (error) {
174-
console.error('Error getting references', error);
175-
throw new Error(JSON.stringify(token, null, 2));
176-
}
177-
const fontweight = R.find<TransformedToken>(typeEquals(['fontweight']))(references);
178-
const lineheight = R.find<TransformedToken>(typeEquals(['lineheight']))(references);
179-
const fontsize = R.find<TransformedToken>(typeEquals(['fontsize']))(references);
180-
const letterSpacing = R.find<TransformedToken>(typeEquals(['dimension']))(references);
181-
182-
const fontSizeVar = fontsize ? getVariableName(format(fontsize)) : null;
183-
const fontWeightVar = fontweight ? getVariableName(format(fontweight)) : null;
184-
const lineheightVar = lineheight ? getVariableName(format(lineheight)) : null;
185-
const letterSpacingVar = letterSpacing ? getVariableName(format(letterSpacing)) : null;
186-
187-
const className = `
188-
.${classSelector(token)} {
189-
${fontSizeVar ? `font-size: ${fontSizeVar};` : ''}
190-
${lineheightVar ? `line-height: ${lineheightVar};` : ''}
191-
${fontWeightVar ? `font-weight: ${fontWeightVar};` : ''}
192-
${letterSpacingVar ? `letter-spacing: ${letterSpacingVar};` : ''}
193-
}`;
194-
195-
return Object.assign(acc, { classes: [className, ...acc.classes] });
196-
}
197-
198-
return Object.assign(acc, { variables: [...acc.variables, format(token)] });
199-
},
200-
{ variables: [], classes: [] },
201-
),
202-
)(sortedTokens);
203-
204-
const classes = formattedTokens.classes.join('\n');
205-
const variables = formattedTokens.variables.join('\n');
206-
const content = selector ? `${selector} {\n${variables}\n${classes}\n}` : classes;
108+
console.log('dictionary.allTokens', dictionary.allTokens);
109+
110+
const filteredTokens = R.reject(typographyFontFamilyPredicate, dictionary.allTokens);
111+
112+
const formattedTokens = R.pipe(R.map(format), R.join('\n'))(filteredTokens);
113+
114+
const content = selector ? `${selector} {\n${formattedTokens}\n}` : formattedTokens;
207115
const body = R.isNotNil(layer) ? `@layer ${layer} {\n${content}\n}` : content;
208116

209117
return header + body;

packages/cli/src/tokens/build/transformers.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,13 @@ export const nameKebab: Transform = {
4444
},
4545
};
4646

47-
type Typgraphy = {
48-
fontWeight: string;
49-
fontSize: string;
50-
lineHeight: number;
51-
fontFamily: string;
52-
};
53-
54-
export const typographyShorthand: Transform = {
55-
name: 'typography/shorthand',
56-
type: 'value',
47+
export const typographyName: Transform = {
48+
name: 'name/typography',
49+
type: 'name',
5750
transitive: true,
58-
filter: (token) => typeEquals('typography', token),
51+
// expanded tokens have different type so we match on path instead
52+
filter: (token) => pathStartsWithOneOf(['typography'], token),
5953
transform: (token) => {
60-
const typography = getValue<Typgraphy>(token);
61-
return `${typography.fontWeight} ${typography.fontSize}/${typography.lineHeight} '${typography.fontFamily}'`;
54+
return token.name.replace('-typography', '');
6255
},
6356
};

0 commit comments

Comments
 (0)