Skip to content

Commit 80c25a5

Browse files
make border only use references for included tokens
1 parent f19507b commit 80c25a5

File tree

5 files changed

+76
-37
lines changed

5 files changed

+76
-37
lines changed

scripts/buildTokens.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ const getStyleDictionaryConfig: StyleDictionaryConfigGenerator = (
4242
Object.entries({
4343
css: css(`css/${filename}.css`, options.prefix, options.buildPath, {
4444
themed: options.themed,
45-
theme: options.theme,
45+
theme: [options.theme, getFallbackTheme(options.theme)],
4646
}),
4747
docJson: docJson(`docs/${filename}.json`, options.prefix, options.buildPath, {
48-
theme: options.theme,
48+
theme: [options.theme, getFallbackTheme(options.theme)],
4949
}),
5050
styleLint: styleLint(`styleLint/${filename}.json`, options.prefix, options.buildPath, {
5151
theme: options.theme,
@@ -60,26 +60,26 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
6060
/** -----------------------------------
6161
* Internal Colors
6262
* ----------------------------------- */
63-
try {
64-
for (const {filename, source, include, theme} of themes) {
65-
// build functional scales
66-
const extendedSD = await PrimerStyleDictionary.extend({
67-
source: [...source, ...include], // build the special formats
68-
include,
69-
log,
70-
platforms: {
71-
css: css(`internalCss/${filename}.css`, buildOptions.prefix, buildOptions.buildPath, {
72-
themed: true,
73-
theme: [theme, getFallbackTheme(theme)],
74-
}),
75-
},
76-
})
77-
await extendedSD.buildAllPlatforms()
78-
}
79-
} catch (e) {
80-
// eslint-disable-next-line no-console
81-
console.error('🛑 Error trying to build internal css colors for code output:', e)
82-
}
63+
// try {
64+
// for (const {filename, source, include, theme} of themes) {
65+
// // build functional scales
66+
// const extendedSD = await PrimerStyleDictionary.extend({
67+
// source: [...source, ...include], // build the special formats
68+
// include,
69+
// log,
70+
// platforms: {
71+
// css: css(`internalCss/${filename}.css`, buildOptions.prefix, buildOptions.buildPath, {
72+
// themed: true,
73+
// theme: [theme, getFallbackTheme(theme)],
74+
// }),
75+
// },
76+
// })
77+
// await extendedSD.buildAllPlatforms()
78+
// }
79+
// } catch (e) {
80+
// // eslint-disable-next-line no-console
81+
// console.error('🛑 Error trying to build internal css colors for code output:', e)
82+
// }
8383

8484
/** -----------------------------------
8585
* Colors, shadows & borders

src/formats/utilities/createPropertyFormatterWithRef.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ export default function createPropertyFormatterWithRef({
194194
const transformedValues = value.split(' ')
195195
value = ['width', 'style', 'color']
196196
.map((prop, index) => {
197-
if (originalValue[prop].startsWith('{')) {
197+
if (
198+
originalValue[prop].startsWith('{') &&
199+
refs.find(ref => ref.path.join('.') === originalValue[prop].replace(/[{}]/g, ''))?.isSource === true
200+
) {
198201
return originalValue[prop]
199202
}
200203
return transformedValues[index]
@@ -223,14 +226,11 @@ export default function createPropertyFormatterWithRef({
223226
}
224227
// TODO: add test
225228
// technically speaking a reference can be made to a number or boolean token, in this case we stringify it first
226-
value = `${value}`.replace(
227-
(value as string).match(/{/) ? new RegExp(`{${ref.path.join('\\.')}(\\.\\$?value)?}`, 'g') : refVal,
228-
replaceFunc,
229-
)
229+
const regex = new RegExp(`{${ref.path.join('\\.')}(\\.\\$?value)?}`, 'g')
230+
value = `${value}`.replace(regex, replaceFunc)
230231
}
231232
})
232233
}
233-
234234
toRetToken += value
235235

236236
const themeableToken = typeof token.themeable === 'boolean' ? token.themeable : themeable
@@ -239,12 +239,10 @@ export default function createPropertyFormatterWithRef({
239239
}
240240

241241
toRetToken += suffix
242-
243242
const comment = token.$description ?? token.comment
244243
if (comment && commentStyle !== 'none') {
245244
toRetToken = addComment(toRetToken, comment, mergedOptions as FormattingOptions)
246245
}
247-
// console.log('toRetToken', toRetToken)
248246
return toRetToken
249247
}
250248
}

src/platforms/css.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {PlatformInitializer} from '../types/platformInitializer.js'
33
import type {PlatformConfig, TransformedToken} from 'style-dictionary/types'
44
import {outputReferencesTransformed, outputReferencesFilter} from 'style-dictionary/utils'
55
import {outputReferencesTransformedWithObject} from './utilities/outputReferencesTransformedWithObject.js'
6+
import {outputReferencesFilterObject} from './utilities/outputReferencesFilterObject.js'
67

78
const getCssSelectors = (outputFile: string) => {
89
// check for dark in the beginning of the output filename
@@ -60,9 +61,12 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
6061
options: {
6162
showFileHeader: false,
6263
// outputReferences: true,
63-
outputReferences: (token, platformOptions) =>
64-
outputReferencesFilter(token, platformOptions) &&
65-
outputReferencesTransformedWithObject(token, platformOptions),
64+
outputReferences: (token, platformOptions) => {
65+
return (
66+
outputReferencesFilterObject(token, platformOptions) &&
67+
outputReferencesTransformedWithObject(token, platformOptions)
68+
)
69+
},
6670
descriptions: false,
6771
queries: getCssSelectors(outputFile),
6872
...options?.options,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// const FILTER_WARNINGS = GroupMessages.GROUP.FilteredOutputReferences
2+
3+
import type {Dictionary, TransformedToken} from 'style-dictionary/types'
4+
import {getReferences} from 'style-dictionary/utils'
5+
6+
/**
7+
* @typedef {import('../../../types/DesignToken.d.ts').TransformedToken} TransformedToken
8+
* @typedef {import('../../../types/DesignToken.d.ts').Dictionary} Dictionary
9+
*
10+
* @param {TransformedToken} token
11+
* @param {{ dictionary: Dictionary, usesDtcg?: boolean }} dictionary
12+
* @returns
13+
*/
14+
export function outputReferencesFilterObject(
15+
token: TransformedToken,
16+
{dictionary, usesDtcg}: {dictionary: Dictionary; usesDtcg?: boolean},
17+
): boolean {
18+
const originalValue = usesDtcg ? token.original.$value : token.original.value
19+
// get refs, pass unfilteredTokens to ensure we find the refs even if they are filtered out
20+
const refs = getReferences(originalValue, dictionary.tokens, {
21+
unfilteredTokens: dictionary.unfilteredTokens,
22+
usesDtcg,
23+
warnImmediately: false,
24+
})
25+
26+
return refs.some(ref => {
27+
// check whether every ref can be found in the filtered set of tokens
28+
const foundToken = dictionary.allTokens.find(thisToken => thisToken.name === ref.name)
29+
if (!foundToken) {
30+
// remove the warning about this ref being filtered out, since we now prevent it from outputting it as a ref
31+
// GroupMessages.remove(FILTER_WARNINGS, ref.path.join('.'))
32+
}
33+
34+
return !!foundToken
35+
})
36+
}

src/platforms/utilities/outputReferencesTransformedWithObject.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ export function outputReferencesTransformedWithObject(
2323
)
2424
}
2525
if (typeof originalValue === 'object') {
26-
const values = Object.values(originalValue).filter(val => typeof val === 'string')
27-
return values.every(val => {
28-
const resolvedValue = resolveReferences(val, dictionary.unfilteredTokens ?? dictionary.tokens, {
26+
const originalValues = Object.values(originalValue).filter(val => typeof val === 'string')
27+
28+
return originalValues.some(origVal => {
29+
const resolvedValue = resolveReferences(origVal, dictionary.unfilteredTokens ?? dictionary.tokens, {
2930
usesDtcg,
3031
warnImmediately: false,
3132
})
3233

33-
return typeof resolvedValue === 'string' ? values.includes(resolvedValue) : true
34+
return typeof resolvedValue === 'string' ? value.split(' ').includes(resolvedValue) : false
3435
})
3536
}
3637
return false

0 commit comments

Comments
 (0)