This repository has been archived by the owner on Jul 26, 2021. It is now read-only.
forked from mars-ou1991/theme-chalk-css-variable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style-dictionary.js
166 lines (145 loc) · 3.45 KB
/
style-dictionary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const StyleDictionaryPackage = require('style-dictionary');
const CUSTOM_SIZE_TRANSFORM = 'size/transform';
const TRANSFORMS = ["attribute/cti", "name/cti/kebab", CUSTOM_SIZE_TRANSFORM];
const CUSTOM_FORMAT = 'css/custom';
const THEMES = ['dark'];
const build = (config) => {
buildDictionary(getGlobalConfig(config), {
wrapper: ':root'
});
const themes = config.themes ? config.themes : [];
themes.forEach((theme) => {
buildDictionary(getThemeConfig(theme, config), {
wrapper: `body.theme-${theme}`
});
});
};
function buildDictionary(config, {wrapper}) {
const StyleDictionary = StyleDictionaryPackage.extend(config);
const {
fileHeader,
formattedVariables
} = StyleDictionary.formatHelpers;
StyleDictionary.registerTransform({
name: CUSTOM_SIZE_TRANSFORM,
type: 'value',
matcher: (token) => ['spacing'].includes(token.attributes.category) || ['size'].includes(token.attributes.type),
transformer: function(token) {
return token.value + 'rem';
}
});
StyleDictionary.registerFormat({
name: CUSTOM_FORMAT,
formatter: function ({
dictionary,
file,
options
}) {
const {
outputReferences
} = options;
dictionary.allTokens = dictionary.allTokens.reduce((accu, token) => {
if (token.attributes.ignore) {
return accu;
}
if (isHSLColorToken(token)) {
return accu.concat(getColorTokens(token));
}
accu.push(token);
return accu;
}, []);
return fileHeader({
file
}) +
`@mixin theme {\n` +
formattedVariables({
format: 'css',
dictionary,
outputReferences
}) +
`\n}\n\n${wrapper} { @include theme }`;
}
});
StyleDictionary.buildAllPlatforms();
}
function getGlobalConfig({source, buildPath}) {
return {
source,
platforms: {
scss: {
transforms: TRANSFORMS,
buildPath,
files: [{
destination: '_tokens.scss',
format: CUSTOM_FORMAT
}]
}
}
}
}
function getThemeConfig(theme, {themeSource, buildPath}) {
return {
source: themeSource.map(source => source.replace(':theme', theme)),
platforms: {
scss: {
transforms: TRANSFORMS,
buildPath,
files: [{
destination: `_tokens.${theme}.scss`,
format:CUSTOM_FORMAT
}]
}
}
}
}
function isHSLColorToken(token) {
return token.attributes.category === 'color' && typeof token.value === 'string' && token.value.startsWith('hsl(');
}
function getColorTokens(token) {
const res = [];
const colors = token.value.replace('hsl(', '').replace(')', '').split(',');
const parts = ['h', 's', 'l'];
colors.map((v, i) => {
res.push({
...token,
value: v,
name: `${token.name}-${parts[i]}`,
});
});
res.push({
...token,
value: `hsl(var(--${token.name}-h), var(--${token.name}-s), var(--${token.name}-l))`
});
if (token.tint) {
Object.keys(token.tint).forEach((i) => {
res.push({
...token,
name: `${token.name}-tint-${i}-l`,
value: `${token.tint[i]}%`
});
res.push({
...token,
name: `${token.name}-tint-${i}`,
value: `hsl(var(--${token.name}-h), var(--${token.name}-s), var(--${token.name}-tint-${i}-l))`
});
});
}
if (token.shade) {
Object.keys(token.shade).forEach((i) => {
res.push({
...token,
name: `${token.name}-shade-${i}-l`,
value: `${token.shade[i]}%`
});
res.push({
...token,
name: `${token.name}-shade-${i}`,
value: `hsl(var(--${token.name}-h), var(--${token.name}-s), var(--${token.name}-shade-${i}-l))`
});
});
}
return res;
}
module.exports = {
build
};