Skip to content

Commit 4eb96a0

Browse files
crisbetoadolgachev
authored andcommitted
build: generate more believable token examples (#31807)
Currently we generate the token examples by taking the first two tokens and using a static value for them. This looks weird when the first tokens aren't color-based. These changes try to infer a more believable value from the token's name. Fixes #31796.
1 parent ec33bf8 commit 4eb96a0

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

tools/extract-tokens/extract-tokens.mts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ExtractedToken {
1111
/** Full prefix of the token. */
1212
prefix: string;
1313
/** Type of the token (color, typography etc.) */
14-
type: string;
14+
type: 'color' | 'typography' | 'density' | 'base';
1515
/** Value of the token. */
1616
value: string | number;
1717
/** Name under which the token can be referred to inside the `overrides` mixin. */
@@ -25,7 +25,7 @@ interface Token {
2525
/** Full prefix of the token. */
2626
prefix: string;
2727
/** Type of the token (color, typography etc.) */
28-
type: string;
28+
type: 'color' | 'typography' | 'density' | 'base';
2929
/** Name under which the token can be referred to inside the `overrides` mixin. */
3030
overridesName: string;
3131
/** Name of the system-level token that this token was derived from. */
@@ -172,8 +172,8 @@ function getUsageExample(themes: ThemeData[]): string | null {
172172
`// Customize the entire app. Change :root to your selector if you want to scope the styles.`,
173173
`:root {`,
174174
` @include mat.${mixin.overridesMixin}((`,
175-
` ${firstToken.overridesName}: orange,`,
176-
...(secondToken ? [` ${secondToken.overridesName}: red,`] : []),
175+
` ${getTokenExample(firstToken, 'first')},`,
176+
...(secondToken ? [` ${getTokenExample(secondToken, 'second')},`] : []),
177177
` ));`,
178178
`}`,
179179
];
@@ -350,6 +350,30 @@ function jsonStringifyImplementation(
350350
`;
351351
}
352352

353+
/** Generates an example string for a token. */
354+
function getTokenExample(token: Token, position: 'first' | 'second'): string {
355+
const name = token.overridesName;
356+
let value: string;
357+
358+
// Attempt to come up with a real-looking example based on the token's shape.
359+
if (name.includes('shape')) {
360+
value = position === 'first' ? '12px' : '16px';
361+
} else if (name.includes('opacity')) {
362+
value = position === 'first' ? '0.8' : '0.2';
363+
} else if (name.includes('size')) {
364+
value = position === 'first' ? '16px' : '10px';
365+
} else if (name.includes('shadow')) {
366+
value =
367+
position === 'first'
368+
? '10px 10px 5px 0px rgba(0, 0, 0, 0.75)'
369+
: '-4px -4px 5px 0px rgba(0, 0, 0, 0.5)';
370+
} else {
371+
value = position === 'first' ? 'orange' : 'red';
372+
}
373+
374+
return `${name}: ${value}`;
375+
}
376+
353377
/**
354378
* Gets the substring between two strings.
355379
* @param text String from which to extract the substring.

0 commit comments

Comments
 (0)