-
Notifications
You must be signed in to change notification settings - Fork 15
Refactor theme tokens to use SCSS and TypeScript #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0803ddb
Refactor theme tokens to use SCSS and TypeScript
vineethasok afb8adb
Add prettier step to generate-tokens script
vineethasok 44a6c6d
Enforce double quotes in theme tokens and update script
vineethasok e90947d
Update package.json
vineethasok c66a257
Update quote style and improve theme token consistency
vineethasok 29d943e
Remove unnecessary ESLint disable comment in test
vineethasok c661984
Refactor theme palette token arrays
vineethasok e8ec260
Testing out migration branch
gjones 8436843
Merge branch 'main' into styled-dic-migration
vineethasok 939cab9
Add loading background tokens and update alert spacing
vineethasok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,137 +1,14 @@ | ||
| import _ from "lodash"; | ||
| import { registerTransforms, transforms } from "@tokens-studio/sd-transforms"; | ||
| import StyleDictionary from "style-dictionary"; | ||
| import StyleDictionary, { getStyleDictionaryConfig } from "./style-dictionary.config.js"; | ||
|
|
||
| registerTransforms(StyleDictionary); | ||
| const themes = ["dark", "light"]; | ||
| const config = await getStyleDictionaryConfig(); | ||
| const { themes, configs } = config; | ||
|
|
||
| function generateThemeFromDictionary(dictionary, valueFunc = (value) => value) { | ||
| const theme = {}; | ||
| dictionary.allTokens.forEach((token) => { | ||
| _.setWith(theme, token.name, valueFunc(token.value), Object) | ||
| }); | ||
| return theme; | ||
| } | ||
|
|
||
| StyleDictionary.registerTransform({ | ||
| type: "name", | ||
| name: "name/cti/dot", | ||
| transformer: (token, options) => { | ||
| if (options.prefix && options.prefix.length) { | ||
| return [options.prefix].concat(token.path).join("."); | ||
| } else { | ||
| return token.path.join("."); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| StyleDictionary.registerFormat({ | ||
| name: "ThemeFormat", | ||
| formatter: function ({ dictionary, platform, options, file }) { | ||
| const theme = generateThemeFromDictionary(dictionary); | ||
| return JSON.stringify(theme, null, 2); | ||
| } | ||
| }); | ||
|
|
||
| StyleDictionary.registerFormat({ | ||
| name: "TypescriptFormat", | ||
| formatter: function ({ dictionary, platform, options, file }) { | ||
| const theme = generateThemeFromDictionary(dictionary, (value) => typeof value); | ||
|
|
||
| // Convert the theme object to a TypeScript interface string | ||
| // Prettier will format this automatically via the generate-tokens script | ||
| let jsonString = JSON.stringify(theme, null, 2); | ||
| const scssSD = new StyleDictionary(configs.scss); | ||
| await scssSD.cleanAllPlatforms(); | ||
| await scssSD.buildAllPlatforms(); | ||
|
|
||
| // Replace type strings with TypeScript types | ||
| jsonString = jsonString.replaceAll('"string"', 'string'); | ||
| jsonString = jsonString.replaceAll('"number"', 'number'); | ||
|
|
||
| return `export interface Theme ${jsonString}\n`; | ||
| } | ||
| }); | ||
|
|
||
| StyleDictionary.extend({ | ||
| source: [`./tokens/**/!(${themes.join("|*.")}).json`], | ||
| platforms: { | ||
| css: { | ||
| transforms: [...transforms, "name/cti/kebab"], | ||
| buildPath: "build/css/", | ||
| files: [ | ||
| { | ||
| destination: "variables.css", | ||
| format: "css/variables", | ||
| options: { | ||
| outputReferences: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| js: { | ||
| transforms: [...transforms, "name/cti/dot"], | ||
| buildPath: "src/theme/tokens/", | ||
| files: [ | ||
| { | ||
| destination: "variables.json", | ||
| format: "ThemeFormat", | ||
| options: { | ||
| outputReferences: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ts: { | ||
| transforms: [...transforms, "name/cti/dot"], | ||
| buildPath: "src/theme/tokens/", | ||
| files: [ | ||
| { | ||
| destination: "types.ts", | ||
| format: "TypescriptFormat", | ||
| options: { | ||
| outputReferences: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }) | ||
| .cleanAllPlatforms() | ||
| .buildAllPlatforms(); | ||
|
|
||
| themes.forEach(theme => | ||
| StyleDictionary.extend({ | ||
| include: [`./tokens/**/!(${themes.join("|*.")}).json`], | ||
| source: [`./tokens/**/${theme}.json`], | ||
| platforms: { | ||
| css: { | ||
| transforms: [...transforms, "name/cti/kebab"], | ||
| buildPath: "build/css/", | ||
| files: [ | ||
| { | ||
| destination: `variables.${theme}.css`, | ||
| format: "css/variables", | ||
| filter: token => token.filePath.indexOf(`${theme}`) > -1, | ||
| options: { | ||
| outputReferences: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| js: { | ||
| transforms: [...transforms, "name/cti/dot"], | ||
| buildPath: "src/theme/tokens/", | ||
| files: [ | ||
| { | ||
| destination: `variables.${theme}.json`, | ||
| format: "ThemeFormat", | ||
| filter: token => token.filePath.indexOf(`${theme}`) > -1, | ||
| options: { | ||
| outputReferences: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }) | ||
| .cleanAllPlatforms() | ||
| .buildAllPlatforms() | ||
| ); | ||
| for (const theme of themes) { | ||
| const themeSD = new StyleDictionary(configs.theme(theme)); | ||
| await themeSD.cleanAllPlatforms(); | ||
| await themeSD.buildAllPlatforms(); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| @import "variables.css"; | ||
|
|
||
| :root { | ||
| --max-width: 1100px; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.