Skip to content

Commit a357a50

Browse files
authored
feat: support composite typography tokens (#39)
1 parent e7a0ba5 commit a357a50

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

packages/tokens-builder/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,35 @@ Overall, this script orchestrates the generation of design tokens by applying cu
99
filters, and formats to the specified theme configuration for multiple platforms,
1010
facilitating a streamlined design token workflow compatible with
1111
various output formats like SCSS, CSS, and TypeScript.
12+
13+
## Typography composite tokens
14+
15+
Tokens builder supports usage of [composite tokens](https://design-tokens.github.io/community-group/format/#composite-design-token) for CSS platform.
16+
For now, typography token sets are supported.
17+
18+
### Example of usage
19+
20+
```json
21+
{
22+
"<component-name>": {
23+
"font": {
24+
"type": "font",
25+
"<token-with-composite-token-reference>": "{typography.<typography-set>}"
26+
}
27+
}
28+
}
29+
```
30+
31+
where `<typography-set>` is a set from `@koobiq/design-tokens/web/properties/typography.json5`
32+
33+
"type": "font" property is **required.**
34+
35+
output:
36+
37+
```
38+
--kbq-<component-name>-font-<token-with-composite-token-reference>-font-size: <value>;
39+
--kbq-<component-name>-font-<token-with-composite-token-reference>-font-weight: <value>;
40+
--kbq-<component-name>-font-<token-with-composite-token-reference>-line-height: <value>;
41+
--kbq-<component-name>-font-<token-with-composite-token-reference>-font-family: <value>;
42+
// ...etc
43+
```

packages/tokens-builder/filters/css-variables.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = (StyleDictionary) => {
77
!prop.attributes.palette &&
88
!prop.attributes.light &&
99
!prop.attributes.dark &&
10-
!prop.attributes.font
10+
!prop.attributes.font &&
11+
prop.type !== 'font'
1112
);
1213
}
1314
});
@@ -24,6 +25,6 @@ module.exports = (StyleDictionary) => {
2425

2526
StyleDictionary.registerFilter({
2627
name: 'css-variables-font',
27-
matcher: (prop) => prop.attributes.font
28+
matcher: (prop) => prop.attributes.font || prop.type === 'font'
2829
});
2930
};
+2-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { getMapFromObj } = require('../formats/utils');
2+
13
module.exports = (StyleDictionary) => {
24
StyleDictionary.registerFormat({
35
name: 'kbq-scss/palette',
@@ -6,17 +8,3 @@ module.exports = (StyleDictionary) => {
68
}
79
});
810
};
9-
10-
function getMapFromObj(object) {
11-
const result = Object.keys(object)
12-
.map((key) => {
13-
if (key === 'contrast') {
14-
return `${key}: ${getMapFromObj(object[key])}`;
15-
}
16-
17-
return `${key}: ${object[key].value},\n`;
18-
})
19-
.join('');
20-
21-
return `(\n${result}\n)`;
22-
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const transform = require('style-dictionary/lib/common/transforms');
2+
3+
const unwrapObjectTransformer = (token) => {
4+
return Object.keys(token.value).map((key) => {
5+
const subToken = token.value[key];
6+
const res = {
7+
...subToken,
8+
filePath: token.filePath,
9+
path: [...token.path, key],
10+
name: `kbq-${[...token.path, key].join('-')}`,
11+
original: { value: subToken }
12+
};
13+
res.attributes = { ...transform['attribute/cti'].transformer(res), font: true };
14+
return res;
15+
}, {});
16+
};
17+
18+
const getMapFromObj = (object) => {
19+
const result = Object.keys(object)
20+
.map((key) => {
21+
if (key === 'contrast') {
22+
return `${key}: ${getMapFromObj(object[key])}`;
23+
}
24+
25+
return `${key}: ${object[key].value},\n`;
26+
})
27+
.join('');
28+
29+
return `(\n${result}\n)`;
30+
};
31+
32+
module.exports = {
33+
unwrapObjectTransformer,
34+
getMapFromObj
35+
};

packages/tokens-builder/formats/variables.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { formatHelpers } = require('style-dictionary');
2+
const { unwrapObjectTransformer } = require('../formats/utils');
23

34
module.exports = (StyleDictionary) => {
45
StyleDictionary.registerFormat({
@@ -7,6 +8,14 @@ module.exports = (StyleDictionary) => {
78
const selector = options.selector ? options.selector : `:root`;
89
const { outputReferences } = options;
910

11+
// apply custom transformations for tokens
12+
dictionary.allProperties = dictionary.allTokens = dictionary.allTokens.flatMap((token) => {
13+
if (typeof token.value === 'object' && token.type === 'font') {
14+
return unwrapObjectTransformer(token);
15+
}
16+
return token;
17+
});
18+
1019
dictionary.allTokens.forEach((token) => {
1120
token.name = token.name.replace(/(light|dark)-/, '');
1221
});

0 commit comments

Comments
 (0)