Skip to content
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

feat: color generation with Style Dictionary #12

Draft
wants to merge 2 commits into
base: dbux-3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@
"pl:serve": "patternlab serve --config ./patternlab-config.json",
"pl:version": "patternlab --version"
},
"homepage": "https://db-ui.github.io/base/"
"homepage": "https://db-ui.github.io/base/",
"dependencies": {
"hex-a11y": "^1.0.2"
}
}
28 changes: 27 additions & 1 deletion style-dictionary.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"source": ["tokens/**/*.json", "tokens/**/*.jsonc"],
"source": ["tokens/**/*.js"],
"platforms": {
"css": {
"transformGroup": "css",
Expand All @@ -23,6 +23,32 @@
}
]
},
"sketch": {
"transforms": [
"attribute/cti",
"color/sketchTransform",
"name/cti/kebab"
],
"buildPath": "build/sketch/",
"prefix": "db",
"files": [
{
"destination": "sketch-palette.sketchpalette",
"format": "sketch/sketchpalette"
}
]
},
"json": {
"transformGroup": "js",
"buildPath": "build/json/",
"prefix": "db",
"files": [
{
"destination": "colors.json",
"format": "json"
}
]
},
"js": {
"transformGroup": "js",
"buildPath": "build/js/",
Expand Down
56 changes: 54 additions & 2 deletions style-dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const StyleDictionary = require('style-dictionary').extend(
'style-dictionary.config.json'
);

const TinyColor2 = require('tinycolor2');

const minifyDictionary = require('style-dictionary/lib/common/formatHelpers/minifyDictionary');

const flattenColors = (dictionary) => {
Expand All @@ -18,9 +20,11 @@ const flattenColors = (dictionary) => {
Object.keys(colors[colorKey]).forEach((subColorKey) => {
// TODO: We should be able to removed this with Amazon Style Dictionary 4, as this might allow parallel "nesting" of a value and subentries
if (subColorKey === '_') {
flatColors[`${colorKey}`] = colors[colorKey][subColorKey];
flatColors[`${colorKey}`] =
colors[colorKey][subColorKey];
} else if (subColorKey.includes('small')) {
flatColors[`${colorKey}-sm`] = colors[colorKey][subColorKey];
flatColors[`${colorKey}-sm`] =
colors[colorKey][subColorKey];
} else {
flatColors[`${colorKey}-${subColorKey}`] =
colors[colorKey][subColorKey];
Expand All @@ -32,6 +36,9 @@ const flattenColors = (dictionary) => {
dictionary['color'] = flatColors;
};

/**
* Custom Tailwind Formatter
*/
StyleDictionary.registerFormat({
name: 'tailwind',
formatter: function ({ dictionary, platform, options, file }) {
Expand All @@ -41,4 +48,49 @@ StyleDictionary.registerFormat({
}
});

/**
* Custom Sketch Palette Transform
*/
StyleDictionary.registerTransform({
type: 'value',
transitive: true,
name: 'color/sketchTransform',
// matcher: (prop) => prop.attributes.category === 'color',
transformer: (prop) => {
let color = TinyColor2(prop.original.value).toRgb();
console.log('--- transformer current token: ', color);
return {
red: color.r / 255, // .toFixed(2)
green: color.g / 255,
blue: color.b / 255,
alpha: color.a
};
}
});

/**
* Custom Sketch Palette Formatter
*/
StyleDictionary.registerFormat({
name: 'sketch/sketchpalette',
formatter: ({ dictionary, platform, options, file }) => {
//console.log('sketch formatter ', dictionary);

var to_ret = {
compatibleVersion: '2.0',
pluginVersion: '2.22',
colors: dictionary.allProperties.map(function (prop) {
// Merging the token's value, which should be an object with r,g,b,a channels
return Object.assign(
{
name: prop.name
},
prop.value
);
})
};
return JSON.stringify(to_ret, null, 2);
}
});

StyleDictionary.buildAllPlatforms();
Loading