-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
813e375
commit 0cb917b
Showing
25 changed files
with
358 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import {getMockDictionary} from '../test-utilities/getMockDictionary.js' | ||
import {themeOverrides} from './themeOverrides.js' | ||
import {getMockToken} from '../test-utilities/getMockToken.js' | ||
|
||
describe('Preprocessor: themeOverrides', () => { | ||
it('works with default settings', () => { | ||
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, {})).toStrictEqual(dictionary.tokens) | ||
expect( | ||
themeOverrides.preprocessor(dictionary.tokens, { | ||
options: {themeOverrides: {theme: 'dark'}}, | ||
}), | ||
).toStrictEqual(resultDictionary.tokens) | ||
}) | ||
}) | ||
|
||
describe('Preprocessor: themeOverrides', () => { | ||
it('works with custom configuration', () => { | ||
const dictionary = getMockDictionary({ | ||
valueOverride: getMockToken({ | ||
name: 'red', | ||
description: 'This is a description', | ||
value: 'transformedValue', | ||
path: ['tokens', 'subgroup', 'red'], | ||
$extensions: { | ||
theme: { | ||
dark: 'darkValue', | ||
}, | ||
}, | ||
}), | ||
objectOverride: getMockToken({ | ||
name: 'red', | ||
description: 'This is a description', | ||
value: 'transformedValue', | ||
path: ['tokens', 'subgroup', 'red'], | ||
$extensions: { | ||
theme: { | ||
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: { | ||
theme: { | ||
dark: 'darkValue', | ||
}, | ||
}, | ||
}), | ||
objectOverride: getMockToken({ | ||
name: 'red', | ||
description: 'DarkMode description', | ||
value: 'darkValue', | ||
path: ['tokens', 'subgroup', 'red'], | ||
$extensions: { | ||
theme: { | ||
dark: { | ||
value: 'darkValue', | ||
description: 'DarkMode description', | ||
}, | ||
}, | ||
}, | ||
}), | ||
}) | ||
|
||
expect( | ||
themeOverrides.preprocessor(dictionary.tokens, { | ||
themeOverrides: { | ||
valueProp: 'value', | ||
extensionProp: 'theme', | ||
}, | ||
}), | ||
).toStrictEqual(dictionary.tokens) | ||
expect( | ||
themeOverrides.preprocessor(dictionary.tokens, { | ||
options: {themeOverrides: {theme: 'dark', valueProp: 'value', extensionProp: 'theme'}}, | ||
}), | ||
).toStrictEqual(resultDictionary.tokens) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type {PlatformConfig, PreprocessedTokens, Preprocessor} from 'style-dictionary/types' | ||
import {transformTokens} from './utilities/transformTokens.js' | ||
|
||
export const themeOverrides: Preprocessor = { | ||
name: 'themeOverrides', | ||
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 tokens = transformTokens(dictionary, token => { | ||
// return early if no theme value is set | ||
if (!currentTheme || !token.$extensions?.[extensionProp] || !token.$extensions?.[extensionProp][currentTheme]) | ||
return token | ||
// get override | ||
const override = token.$extensions?.[extensionProp][currentTheme] | ||
// token an theme value exist | ||
return { | ||
...token, | ||
...(typeof override === 'object' ? override : {[valueProp]: override}), | ||
} | ||
}) | ||
return tokens | ||
}, | ||
} |
Oops, something went wrong.