Skip to content

Commit

Permalink
remove old overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Dec 16, 2024
1 parent d594d58 commit ba3083e
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 740 deletions.
5 changes: 4 additions & 1 deletion scripts/buildFigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {PrimerStyleDictionary} from '../src/primerStyleDictionary.js'
import {themes} from './themes.config.js'
import {figma} from '../src/platforms/index.js'
import type {ConfigGeneratorOptions} from '../src/types/styleDictionaryConfigGenerator.js'
import {getFallbackTheme} from './utilities/getFallbackTheme.js'

const buildFigma = async (buildOptions: ConfigGeneratorOptions): Promise<void> => {
/** -----------------------------------
Expand Down Expand Up @@ -225,7 +226,9 @@ const buildFigma = async (buildOptions: ConfigGeneratorOptions): Promise<void> =
source,
include,
platforms: {
figma: figma(`figma/shadows/${name}.json`, buildOptions.prefix, buildOptions.buildPath, {theme}),
figma: figma(`figma/shadows/${name}.json`, buildOptions.prefix, buildOptions.buildPath, {
theme: [theme, getFallbackTheme(theme)],
}),
},
})

Expand Down
11 changes: 6 additions & 5 deletions scripts/buildTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {TokenBuildInput} from '../src/types/tokenBuildInput.js'
import glob from 'fast-glob'
import {themes} from './themes.config.js'
import fs from 'fs'
import {getFallbackTheme} from './utilities/getFallbackTheme.js'

/**
* getStyleDictionaryConfig
Expand Down Expand Up @@ -39,13 +40,13 @@ const getStyleDictionaryConfig: StyleDictionaryConfigGenerator = (
Object.entries({
css: css(`css/${filename}.css`, options.prefix, options.buildPath, {
themed: options.themed,
theme: options.theme,
theme: [options.theme, getFallbackTheme(options.theme)],
}),
docJson: docJson(`docs/${filename}.json`, options.prefix, options.buildPath, {
theme: options.theme,
theme: [options.theme, getFallbackTheme(options.theme)],
}),
styleLint: styleLint(`styleLint/${filename}.json`, options.prefix, options.buildPath, {
theme: options.theme,
theme: [options.theme, getFallbackTheme(options.theme)],
}),
fallbacks: fallbacks(`fallbacks/${filename}.json`, options.prefix, options.buildPath),
...platforms,
Expand All @@ -66,7 +67,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
platforms: {
css: css(`internalCss/${filename}.css`, buildOptions.prefix, buildOptions.buildPath, {
themed: true,
theme,
theme: [theme, getFallbackTheme(theme)],
}),
},
})
Expand All @@ -88,7 +89,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
`functional/themes/${filename}`,
source,
include,
{...buildOptions, themed: true, theme},
{...buildOptions, themed: true, theme: [theme, getFallbackTheme(theme)]},
// disable fallbacks for themes
{fallbacks: undefined},
),
Expand Down
3 changes: 3 additions & 0 deletions scripts/utilities/getFallbackTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const getFallbackTheme = (theme?: string) => {
return theme ? (theme.startsWith('light') ? 'light' : 'dark') : undefined
}
69 changes: 69 additions & 0 deletions src/preprocessors/themeOverrides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,73 @@ describe('Preprocessor: themeOverrides', () => {
}),
).toStrictEqual(resultDictionary.tokens)
})

it('works with fallback theme', () => {
const dictionary = getMockDictionary({
valueOverride: getMockToken({
name: 'red',
description: 'This is a description',
$value: 'transformedValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: 'darkValue',
},
},
}),
objectOverride: getMockToken({
name: 'red',
description: 'This is a description',
$value: 'transformedValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: {
$value: 'darkValue',
description: 'DarkMode description',
},
},
},
}),
})

const resultDictionary = getMockDictionary({
valueOverride: getMockToken({
name: 'red',
description: 'This is a description',
$value: 'darkValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: 'darkValue',
},
},
}),
objectOverride: getMockToken({
name: 'red',
description: 'DarkMode description',
$value: 'darkValue',
path: ['tokens', 'subgroup', 'red'],
$extensions: {
'org.primer.overrides': {
dark: {
$value: 'darkValue',
description: 'DarkMode description',
},
},
},
}),
})

expect(
themeOverrides.preprocessor(dictionary.tokens, {
options: {themeOverrides: {theme: ['dark-dimmed', undefined]}},
}),
).toStrictEqual(dictionary.tokens)
expect(
themeOverrides.preprocessor(dictionary.tokens, {
options: {themeOverrides: {theme: ['dark-dimmed', 'dark']}},
}),
).toStrictEqual(resultDictionary.tokens)
})
})
12 changes: 8 additions & 4 deletions src/preprocessors/themeOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ export const themeOverrides: Preprocessor = {
preprocessor: (dictionary: PreprocessedTokens, config: PlatformConfig): PreprocessedTokens => {
const extensionProp = config.options?.themeOverrides?.extensionProp || 'org.primer.overrides'
const valueProp = config.options?.themeOverrides?.valueProp || '$value'
const currentTheme = config.options?.themeOverrides?.theme

const [currentTheme, fallbackTheme] = asArray(config.options?.themeOverrides?.theme)
const tokens = transformTokens(dictionary, token => {
// return early if no theme value is set
if (!currentTheme || !token.$extensions?.[extensionProp] || !token.$extensions?.[extensionProp][currentTheme]) {
if (
!currentTheme ||
!token.$extensions?.[extensionProp] ||
(!token.$extensions?.[extensionProp][currentTheme] && !token.$extensions?.[extensionProp][fallbackTheme])
) {
return token
}

// get override
const override = token.$extensions?.[extensionProp][currentTheme]
const override =
token.$extensions?.[extensionProp][currentTheme] || token.$extensions?.[extensionProp][fallbackTheme]
// token an theme value exist
return {
...token,
Expand Down
4 changes: 2 additions & 2 deletions src/tokens/component/diffBlob.json5
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
},
'dark-high-contrast': '{bgColor.danger.emphasis}',
'dark-tritanopia': {
$value: '{base.color.orange.4}',
$value: '{base.color.red.4}',
alpha: 0.4,
},
dark: {
Expand Down Expand Up @@ -239,7 +239,7 @@
alpha: 0.3,
},
'dark-tritanopia': {
$value: '{base.color.orange.4}',
$value: '{base.color.red.4}',
alpha: 0.3,
},
dark: {
Expand Down
Loading

0 comments on commit ba3083e

Please sign in to comment.