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

Modify color namespace and update transformations to Figma and style system outputs #4654

Merged
merged 18 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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
22 changes: 17 additions & 5 deletions design-tokens/src/HPEStyleDictionary.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { StyleDictionary } from 'style-dictionary-utils';
import {
commonJs,
commonJsGrommetRefs,
cssColorModes,
cssBreakpoints,
esmGrommetRefs,
javascriptEsm,
jsonFlat,
} from './formats/index.js';
import {
colorNameJs,
cssW3c,
javascriptCss,
javascriptW3c,
linearGradientCss,
nameCSS,
numberToDimension,
shadowCSS,
valueToCssVar,
} from './transforms/index.js';

export const HPEStyleDictionary = new StyleDictionary({
Expand Down Expand Up @@ -42,14 +45,16 @@ HPEStyleDictionary.registerFormat({
name: `esmGrommetRefs`,
format: esmGrommetRefs,
});
HPEStyleDictionary.registerFormat({
name: `commonJsGrommetRefs`,
format: commonJsGrommetRefs,
});
HPEStyleDictionary.registerFormat({
name: `jsonFlat`,
format: jsonFlat,
});
HPEStyleDictionary.registerTransform({
...colorNameJs,
});
HPEStyleDictionary.registerTransform({
...nameCSS,
});
HPEStyleDictionary.registerTransform({
...numberToDimension,
});
Expand All @@ -66,6 +71,13 @@ HPEStyleDictionary.registerTransform({
HPEStyleDictionary.registerTransform({
...linearGradientCss,
});
HPEStyleDictionary.registerTransform({
...valueToCssVar,
});
HPEStyleDictionary.registerTransformGroup({
name: 'js/css',
transforms: javascriptCss,
});
HPEStyleDictionary.registerTransformGroup({
name: 'js/w3c',
transforms: javascriptW3c,
Expand Down
31 changes: 0 additions & 31 deletions design-tokens/src/formats/commonJsGrommetRefs.ts

This file was deleted.

1 change: 0 additions & 1 deletion design-tokens/src/formats/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { commonJs } from './commonJs.js';
export { commonJsGrommetRefs } from './commonJsGrommetRefs.js';
export { cssColorModes } from './cssColorModes.js';
export { cssBreakpoints } from './cssBreakpoints.js';
export { esmGrommetRefs } from './esmGrommetRefs.js';
Expand Down
7 changes: 5 additions & 2 deletions design-tokens/src/formats/utils/getGrommetValue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { usesReferences, getReferences } from 'style-dictionary/utils';
import { isReference } from '../../utils.js';
import { excludedNameParts, isReference } from '../../utils.js';

const tokenToGrommetRef = (value: string): string => {
const temp: string[] = value.slice(1, -1).split('.');
const temp: string[] = value
.slice(1, -1)
.split('.')
.filter(part => !excludedNameParts.includes(part));
temp.shift();
return temp.join('-');
};
Expand Down
27 changes: 25 additions & 2 deletions design-tokens/src/formats/utils/jsonToNestedValue.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import { excludedNameParts } from '../../utils.js';

export const jsonToNestedValue = (
token: string | { [key: string]: any } | undefined,
) => {
keyPath: string[] = [],
): any => {
if (!token || typeof token !== 'object') return token;
if ('$value' in token) return token.$value;
const nextObj: { [key: string]: any } = {};
Object.entries(token).forEach(([key, value]) => {
nextObj[key] = jsonToNestedValue(value);
if (keyPath.length > 2 && keyPath.includes('color')) {
if (typeof value === 'object' && value !== null) {
const result = jsonToNestedValue(value, [...keyPath, key]);
if (typeof result === 'string') {
nextObj[keyPath.concat(key).join('-')] = result;
} else {
Object.entries(result).forEach(([nestedKey, nestedValue]) => {
nextObj[nestedKey] = nestedValue;
});
}
} else {
const flatName = keyPath
.concat(key)
.slice(3) // TO DO make dynamic (removing hpe, color, category (background, text, icon, ...))
.filter(part => !excludedNameParts.includes(part))
.join('-');
nextObj[flatName] = value;
}
} else {
nextObj[key] = jsonToNestedValue(value, [...keyPath, key]);
}
});

return nextObj;
Expand Down
111 changes: 98 additions & 13 deletions design-tokens/src/scripts/build-style-dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getThemeAndMode, numberToPixel } from '../utils.ts';

const TOKENS_DIR = 'tokens';
const ESM_DIR = 'dist/esm/';
const GROMMET_DIR = 'dist/grommet/';
const CJS_DIR = 'dist/cjs/';
const CSS_DIR = 'dist/css/';
const DOCS_DIR = 'dist/docs/';
Expand All @@ -21,8 +22,19 @@ try {
extendedDictionary = await HPEStyleDictionary.extend({
source: [`${TOKENS_DIR}/primitive/primitives.base.json`],
platforms: {
js: {
grommet: {
transformGroup: 'js/w3c',
buildPath: GROMMET_DIR,
prefix: PREFIX,
files: [
{
destination: 'base.js',
format: 'javascript/esm',
},
],
},
js: {
transformGroup: 'js/css',
buildPath: ESM_DIR,
prefix: PREFIX,
files: [
Expand All @@ -33,7 +45,7 @@ try {
],
},
'js/cjs': {
transformGroup: 'js/w3c',
transformGroup: 'js/css',
buildPath: CJS_DIR,
prefix: PREFIX,
files: [
Expand Down Expand Up @@ -81,8 +93,21 @@ try {
`${TOKENS_DIR}/semantic/global.default.json`,
],
platforms: {
js: {
grommet: {
transformGroup: 'js/w3c',
buildPath: GROMMET_DIR,
prefix: PREFIX,
files: [
{
destination: 'global.js',
format: 'esmGrommetRefs',
filter: token =>
token.filePath === `${TOKENS_DIR}/semantic/global.default.json`,
},
],
},
js: {
transformGroup: 'js/css',
buildPath: ESM_DIR,
prefix: PREFIX,
files: [
Expand All @@ -95,13 +120,13 @@ try {
],
},
'js/cjs': {
transformGroup: 'js/w3c',
transformGroup: 'js/css',
buildPath: CJS_DIR,
prefix: PREFIX,
files: [
{
destination: 'global.cjs',
format: 'commonJsGrommetRefs',
format: 'javascript/commonJs',
filter: token =>
token.filePath === `${TOKENS_DIR}/semantic/global.default.json`,
},
Expand Down Expand Up @@ -163,8 +188,22 @@ try {
extendedDictionary = await HPEStyleDictionary.extend({
source: [`${TOKENS_DIR}/primitive/primitives.base.json`, file],
platforms: {
js: {
grommet: {
transformGroup: 'js/w3c',
buildPath: GROMMET_DIR,
prefix: PREFIX,
files: [
{
destination: `color.${
theme ? `${theme}-${mode}` : `${mode || ''}`
}.js`,
format: 'javascript/esm',
filter: token => token.filePath === file,
},
],
},
js: {
transformGroup: 'js/css',
buildPath: ESM_DIR,
prefix: PREFIX,
files: [
Expand All @@ -178,7 +217,7 @@ try {
],
},
'js/cjs': {
transformGroup: 'js/w3c',
transformGroup: 'js/css',
buildPath: CJS_DIR,
prefix: PREFIX,
files: [
Expand Down Expand Up @@ -255,8 +294,20 @@ try {
file,
],
platforms: {
js: {
grommet: {
transformGroup: 'js/w3c',
buildPath: GROMMET_DIR,
prefix: PREFIX,
files: [
{
destination: `dimension.${mode}.js`,
format: 'javascript/esm',
filter: token => token.filePath === file,
},
],
},
js: {
transformGroup: 'js/css',
buildPath: ESM_DIR,
prefix: PREFIX,
files: [
Expand All @@ -268,7 +319,7 @@ try {
],
},
'js/cjs': {
transformGroup: 'js/w3c',
transformGroup: 'js/css',
buildPath: CJS_DIR,
prefix: PREFIX,
files: [
Expand All @@ -280,7 +331,7 @@ try {
],
},
css: {
transformGroup: 'css',
transformGroup: 'css/w3c',
buildPath: CSS_DIR,
prefix: PREFIX,
files: [
Expand Down Expand Up @@ -335,8 +386,21 @@ try {
`${TOKENS_DIR}/component/component.default.json`,
],
platforms: {
js: {
grommet: {
transformGroup: 'js/w3c',
buildPath: GROMMET_DIR,
prefix: PREFIX,
files: [
{
destination: 'components.default.js',
filter: token =>
token.filePath.includes(`${TOKENS_DIR}/component/`),
format: 'esmGrommetRefs',
},
],
},
js: {
transformGroup: 'js/css',
buildPath: ESM_DIR,
prefix: PREFIX,
files: [
Expand All @@ -349,15 +413,15 @@ try {
],
},
'js/cjs': {
transformGroup: 'js/w3c',
transformGroup: 'js/css',
buildPath: CJS_DIR,
prefix: PREFIX,
files: [
{
destination: 'components.default.cjs',
filter: token =>
token.filePath.includes(`${TOKENS_DIR}/component/`),
format: 'commonJsGrommetRefs',
format: 'javascript/commonJs',
},
],
},
Expand Down Expand Up @@ -445,6 +509,27 @@ fs.readdirSync(ESM_DIR)
}
});

/** -----------------------------------
* Create Grommet index.js
* ----------------------------------- */
const grommetCollections = [];
fs.readdirSync(GROMMET_DIR)
.filter(file => file !== 'index.js')
.forEach(file => {
if (file.toLowerCase().endsWith('.js')) {
const filename = file.replace('.js', '');
const parts = filename.split('.');
let mode = parts[1];
// special case for base.js and components
if (mode === 'default' || !mode) [mode] = parts;
fs.appendFileSync(
`${GROMMET_DIR}index.js`,
`export { default as ${mode} } from './${filename}.js';\n`,
);
grommetCollections.push(mode);
}
});

/** -----------------------------------
* Create docs index.js
* ----------------------------------- */
Expand Down
Loading
Loading