-
Notifications
You must be signed in to change notification settings - Fork 43
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
7f0212a
commit cff2cf7
Showing
8 changed files
with
211 additions
and
60 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 was deleted.
Oops, something went wrong.
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 {themeOverride} from './themeOverride.js' | ||
import {getMockToken} from '../test-utilities/getMockToken.js' | ||
|
||
describe('Preprocessor: themeOverride', () => { | ||
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(themeOverride.preprocessor(dictionary.tokens, {})).toStrictEqual(dictionary.tokens) | ||
expect( | ||
themeOverride.preprocessor(dictionary.tokens, { | ||
options: {themeOverrides: {theme: 'dark'}}, | ||
}), | ||
).toStrictEqual(resultDictionary.tokens) | ||
}) | ||
}) | ||
|
||
describe('Preprocessor: themeOverride', () => { | ||
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( | ||
themeOverride.preprocessor(dictionary.tokens, { | ||
themeOverrides: { | ||
valueProp: 'value', | ||
extensionProp: 'theme', | ||
}, | ||
}), | ||
).toStrictEqual(dictionary.tokens) | ||
expect( | ||
themeOverride.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 themeOverride: Preprocessor = { | ||
name: 'themeOverride', | ||
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 | ||
}, | ||
} |
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