@projectwallace/css-design-tokens 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
- import { ColorToken, UnparsedToken } from './types.js';
1
+ import { ColorValue } from './types.js';
2
2
  type CssLength = {
3
3
  value: number;
4
4
  unit: string;
5
5
  };
6
6
  export type DestructuredShadow = {
7
- color: ColorToken | UnparsedToken | undefined;
7
+ color: ColorValue | undefined;
8
8
  offsetX: CssLength | undefined;
9
9
  offsetY: CssLength | undefined;
10
10
  blur: CssLength | undefined;
@@ -1,2 +1,2 @@
1
1
  import { Easing } from './types.js';
2
- export declare function destructure_easing(easing: string): Easing | undefined;
2
+ export declare function destructure_easing(easing: string): Easing | null;
package/dist/types.d.ts CHANGED
@@ -2,18 +2,22 @@ import { analyze } from '@projectwallace/css-analyzer';
2
2
  import { DestructuredShadow } from './destructure-box-shadow';
3
3
  export type CssAnalysis = ReturnType<typeof analyze>;
4
4
  export declare const EXTENSION_AUTHORED_AS = "com.projectwallace.css-authored-as";
5
+ export declare const EXTENSION_USAGE_COUNT = "com.projectwallace.usage-count";
6
+ export declare const EXTENSION_CSS_PROPERTIES = "com.projectwallace.css-properties";
5
7
  export type Easing = [number, number, number, number];
6
8
  export type BaseToken = {
7
- $extensions?: {
9
+ $extensions: {
8
10
  [EXTENSION_AUTHORED_AS]: string;
11
+ [EXTENSION_USAGE_COUNT]: number;
9
12
  };
10
13
  };
14
+ type DurationValue = {
15
+ value: number;
16
+ unit: 'ms';
17
+ };
11
18
  export type DurationToken = BaseToken & {
12
19
  $type: 'duration';
13
- $value: {
14
- value: number;
15
- unit: 'ms';
16
- };
20
+ $value: DurationValue;
17
21
  };
18
22
  export type UnparsedToken = BaseToken & {
19
23
  $value: string;
@@ -31,15 +35,16 @@ export type ColorToken = BaseToken & {
31
35
  $type: 'color';
32
36
  $value: ColorValue;
33
37
  $extensions: {
34
- [EXTENSION_AUTHORED_AS]: string;
38
+ [EXTENSION_CSS_PROPERTIES]: Array<string>;
35
39
  };
36
40
  };
41
+ export type DimensionValue = {
42
+ value: number;
43
+ unit: string;
44
+ };
37
45
  export type DimensionToken = BaseToken & {
38
46
  $type: 'dimension';
39
- $value: {
40
- value: number;
41
- unit: string;
42
- };
47
+ $value: DimensionValue;
43
48
  };
44
49
  export type NumberToken = BaseToken & {
45
50
  $type: 'number';
@@ -51,7 +56,7 @@ export type CubicBezierToken = BaseToken & {
51
56
  };
52
57
  export type FontFamilyToken = BaseToken & {
53
58
  $type: 'fontFamily';
54
- $value: string[] | string;
59
+ $value: string[];
55
60
  };
56
61
  export type ShadowToken = BaseToken & {
57
62
  $type: 'shadow';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@projectwallace/css-design-tokens",
3
3
  "description": "Generate spec-compliant Design Tokens from CSS.",
4
- "version": "0.5.0",
4
+ "version": "0.7.0",
5
5
  "license": "EUPL-1.2",
6
6
  "author": "Bart Veneman <bart@projectwallace.com>",
7
7
  "repository": {
@@ -34,7 +34,7 @@
34
34
  "check": "tsc --noEmit"
35
35
  },
36
36
  "dependencies": {
37
- "@projectwallace/css-analyzer": "^7.5.1",
37
+ "@projectwallace/css-analyzer": "^7.6.0",
38
38
  "color-sorter": "^7.0.0",
39
39
  "colorjs.io": "^0.6.0-alpha.1",
40
40
  "css-time-sort": "^3.0.0",
package/readme.md CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  Create Design Tokens by going through CSS to find colors, font-sizes, gradients etcetera and turn them into a [Design Tokens spec](https://tr.designtokens.org/)-compliant token format.
4
4
 
5
+ ## Table of contents
6
+
7
+ - [Installation](#installation)
8
+ - [Usage](#usage)
9
+ - [Token types](#token-types)
10
+ - [Color](#color)
11
+ - [Font-size](#font-size)
12
+ - [Font-family](#font-family)
13
+ - [Line-height](#line-height)
14
+ - [Gradient](#gradient)
15
+ - [Box-shadow](#box-shadow)
16
+ - [Radius](#radius)
17
+ - [Duration](#duration)
18
+ - [Easing](#easing)
19
+ - [Extensions](#extensions)
20
+ - [Acknowledgements](#acknowledgements)
21
+
5
22
  ## Installation
6
23
 
7
24
  ```sh
@@ -59,9 +76,397 @@ let { color } = css_to_tokens(
59
76
  // }
60
77
  ```
61
78
 
62
- ### Getting authored values
79
+ ## Token types
80
+
81
+ ### Color
82
+
83
+ ['Color' Design Token format](https://www.designtokens.org/tr/third-editors-draft/color/#format)
84
+
85
+ Only fully compliant colors are listed. Colors that can't be parsed by [colorjs.io](https://colorjs.io/) are ignored, like `rgb(var(--red) var(--green) var(--blue))` or CSS system colors like `ButtonText`.
86
+
87
+ - The optional `alpha` property is _always_ present.
88
+ - The optional `hex` fallback property is _never_ present.
89
+ - In addition to other tokens all colors have a `com.projectwallace.css-properties` extension that contains all the CSS properties that a specific color was used for.
90
+
91
+ ```js
92
+ let { color } = css_to_tokens(`.my-design-system { color: green; }`)
93
+
94
+ let color = {
95
+ 'green-5e0cf03': {
96
+ $type: 'color',
97
+ $value: {
98
+ colorSpace: 'srgb',
99
+ components: [0, 0.5019607843137255, 0],
100
+ alpha: 1,
101
+ },
102
+ $extensions: {
103
+ 'com.projectwallace.css-authored-as': 'green',
104
+ 'com.projectwallace.usage-count': 2,
105
+ 'com.projectwallace.css-properties': ['color', 'border-color'],
106
+ }
107
+ }
108
+ }
109
+ ```
110
+
111
+ ### Font-size
112
+
113
+ ['Dimension' Design Token format](https://www.designtokens.org/tr/third-editors-draft/format/#dimension)
114
+
115
+ Font-sizes are listed as `$type: 'dimension'` types if the font-size is declared with either `px` or `rem` or as plain type-less tokens otherwise.
116
+
117
+ ```js
118
+ let { font_size } = css_to_tokens(`.my-design-system {
119
+ .my-design-system {
120
+ font-size: 16px;
121
+ font-size: 1rem;
122
+ font-size: 20vmin;
123
+ }
124
+ }`)
125
+
126
+ let font_size = {
127
+ 'fontSize-171eed': {
128
+ $type: 'dimension',
129
+ $value: {
130
+ value: 16,
131
+ unit: 'px'
132
+ },
133
+ $extensions: {
134
+ 'com.projectwallace.css-authored-as': '16px',
135
+ 'com.projectwallace.usage-count': 1,
136
+ }
137
+ },
138
+ 'fontSize-582e015a': {
139
+ $value: '20vmin',
140
+ $extensions: {
141
+ 'com.projectwallace.css-authored-as': '20vmin',
142
+ 'com.projectwallace.usage-count': 1,
143
+ }
144
+ },
145
+ }
146
+ ```
147
+
148
+ ### Font-family
149
+
150
+ ['fontFamily' Design Token format](https://www.designtokens.org/tr/third-editors-draft/format/#font-family)
151
+
152
+ Font-families are _always_ listed as `$type: 'fontFamily'`.
153
+
154
+ ```js
155
+ let { font_family } = css_to_tokens(`.my-design-system {
156
+ .my-design-system {
157
+ font-family: 'Inter';
158
+ font-family: Arial Black, sans-serif;
159
+ }
160
+ }`)
161
+
162
+ let font_family = {
163
+ 'fontFamily-3375cf09': {
164
+ $type: 'fontFamily',
165
+ $value: ['\'Inter\''],
166
+ $extensions: {
167
+ 'com.projectwallace.css-authored-as': '\'Inter\'',
168
+ 'com.projectwallace.usage-count': 1,
169
+ }
170
+ },
171
+ 'fontFamily-582e015a': {
172
+ $value: ['Arial Black', 'sans-serif'],
173
+ $extensions: {
174
+ 'com.projectwallace.css-authored-as': 'Arial Black, sans-serif',
175
+ 'com.projectwallace.usage-count': 1,
176
+ }
177
+ },
178
+ }
179
+ ```
180
+
181
+ ### Line-height
182
+
183
+ Line heights can either be `dimension` or `number` types, or a plain type-less token. This depends on how well the value can be mapped to a valid token.
184
+
185
+ ```ts
186
+ let { line_height } = css_to_tokens(`
187
+ .my-design-system {
188
+ line-height: 1.5rem; /* rem -> type=dimension */
189
+ line-height: 1.5; /* no unit -> type=number */
190
+ line-height: 20vmin; /* can not be mapped to valid token type */
191
+ }
192
+ `)
193
+
194
+ let line_height = {
195
+ 'lineHeight-563f7fe2': {
196
+ $type: 'dimension',
197
+ $value: {
198
+ value: 1.5,
199
+ unit: 'rem'
200
+ },
201
+ $extensions: {
202
+ 'com.projectwallace.css-authored-as': '1.5rem',
203
+ 'com.projectwallace.usage-count': 1,
204
+ }
205
+ },
206
+ 'lineHeight-bdb8': {
207
+ $type: 'number',
208
+ $value: 1.5,
209
+ $extensions: {
210
+ 'com.projectwallace.css-authored-as': '1.5',
211
+ 'com.projectwallace.usage-count': 1,
212
+ }
213
+ },
214
+ 'lineHeight-582e015a': {
215
+ $value: '20vmin',
216
+ $extensions: {
217
+ 'com.projectwallace.css-authored-as': '20vmin',
218
+ 'com.projectwallace.usage-count': 1,
219
+ }
220
+ }
221
+ }
222
+ ```
223
+
224
+ ### Gradient
225
+
226
+ Gradients are passed as-is, no mapping is done. This is because the spec is currently too limited in expressing a CSS gradient.
227
+
228
+ ```ts
229
+ let { gradient } = css_to_tokens(`
230
+ .my-design-system {
231
+ background: linear-gradient(to right, red, blue);
232
+ }
233
+ `)
234
+
235
+ let gradient = {
236
+ 'gradient-2aec04e5': {
237
+ $value: 'linear-gradient(to right, red, blue)',
238
+ $extensions: {
239
+ 'com.projectwallace.css-authored-as': 'linear-gradient(to right, red, blue)',
240
+ 'com.projectwallace.usage-count': 1,
241
+ }
242
+ }
243
+ }
244
+ ```
245
+
246
+ ### Box-shadow
247
+
248
+ ['Shadow' Design Token type](https://www.designtokens.org/tr/third-editors-draft/format/#shadow)
249
+
250
+ - Multiple shadows can be mapped, so beware that `$value` van either be a single object or an array.
251
+ - Only if a box-shadow has a valid `color` type it will be mapped as a `box-shadow` type
252
+
253
+ ```ts
254
+ let { box_shadow } = css_to_tokens(`
255
+ .my-design-system {
256
+ box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.5);
257
+ box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.5), 0 0 10px 0 rgba(0, 0, 0, 0.5);
258
+ box-shadow: 0 0 0 0 var(--red);
259
+ }
260
+ `)
261
+
262
+ let box_shadow = {
263
+ 'boxShadow-6f90da6b': {
264
+ $type: 'shadow',
265
+ $value: {
266
+ offsetX: {
267
+ value: 0,
268
+ unit: 'px'
269
+ },
270
+ offsetY: {
271
+ value: 0,
272
+ unit: 'px'
273
+ },
274
+ blur: {
275
+ value: 10,
276
+ unit: 'px'
277
+ },
278
+ spread: {
279
+ value: 0,
280
+ unit: 'px'
281
+ },
282
+ inset: false,
283
+ color: {
284
+ colorSpace: 'srgb',
285
+ components: [0, 0, 0],
286
+ alpha: 0.5,
287
+ },
288
+ },
289
+ $extensions: {
290
+ 'com.projectwallace.css-authored-as': '0 0 10px 0 rgba(0, 0, 0, 0.5)',
291
+ 'com.projectwwallace.usage-count': 1,
292
+ }
293
+ },
294
+ 'boxShadow-be2751ac': {
295
+ $type: 'shadow',
296
+ $value: [
297
+ {
298
+ offsetX: {
299
+ value: 0,
300
+ unit: 'px'
301
+ },
302
+ offsetY: {
303
+ value: 0,
304
+ unit: 'px'
305
+ },
306
+ blur: {
307
+ value: 10,
308
+ unit: 'px'
309
+ },
310
+ spread: {
311
+ value: 0,
312
+ unit: 'px'
313
+ },
314
+ inset: false,
315
+ color: {
316
+ colorSpace: 'srgb',
317
+ components: [0, 0, 0],
318
+ alpha: 0.5,
319
+ },
320
+ },
321
+ {
322
+ offsetX: {
323
+ value: 0,
324
+ unit: 'px'
325
+ },
326
+ offsetY: {
327
+ value: 0,
328
+ unit: 'px'
329
+ },
330
+ blur: {
331
+ value: 10,
332
+ unit: 'px'
333
+ },
334
+ spread: {
335
+ value: 0,
336
+ unit: 'px'
337
+ },
338
+ inset: false,
339
+ color: {
340
+ colorSpace: 'srgb',
341
+ components: [0, 0, 0],
342
+ alpha: 0.5,
343
+ },
344
+ }
345
+ ],
346
+ $extensions: {
347
+ 'com.projectwwallace.css-authored-as': '0 0 10px 0 rgba(0, 0, 0, 0.5), 0 0 10px 0 rgba(0, 0, 0, 0.5)',
348
+ 'com.projectwwallace.usage-count': 1,
349
+ }
350
+ },
351
+ 'boxShadow-j4h5gas5h': {
352
+ $value: '0 0 0 0 var(--red)',
353
+ $extensions: {
354
+ 'com.projectwwallace.css-authored-as': '0 0 0 0 var(--red)',
355
+ 'com.projectwwallace.usage-count': 1,
356
+ }
357
+ }
358
+ }
359
+ ```
360
+
361
+ ### Radius
362
+
363
+ Radii are passed as-is, no mapping is done.
364
+
365
+ ```ts
366
+ let { radius } = css_to_tokens(`
367
+ .my-design-system {
368
+ border-radius: 10px;
369
+ }
370
+ `)
371
+
372
+ let radius = {
373
+ 'radius-170867': {
374
+ $value: '10px',
375
+ $extensions: {
376
+ 'com.projectwwallace.css-authored-as': '10px',
377
+ 'com.projectwwallace.usage-count': 1,
378
+ }
379
+ }
380
+ }
381
+ ```
382
+
383
+ ### Duration
384
+
385
+ ['Duration' Design Token type](https://www.designtokens.org/tr/third-editors-draft/format/#duration)
63
386
 
64
- The tokens output parses most CSS into Design Tokens but in most cases it also provides a way to get the authored CSS via the `$extensions` property. The custom identifier for this project is `com.projectwallace` and the authored values can be found with `com.projectwallace.css-authored-as` on the `$extensions` object.
387
+ Durations can either be animation- or transition-durations or -delays. Even though `s` is a valid unit we _always_ map to `ms`.
388
+
389
+ ```ts
390
+ let { duration } = css_to_tokens(`
391
+ .my-design-system {
392
+ animation-duration: 1s;
393
+ }
394
+ `)
395
+
396
+ let duration = {
397
+ 'duration-17005f': {
398
+ $type: 'duration',
399
+ $value: {
400
+ value: 1000,
401
+ unit: 'ms'
402
+ },
403
+ $extensions: {
404
+ 'com.projectwwallace.css-authored-as': '1s',
405
+ 'com.projectwwallace.usage-count': 1,
406
+ }
407
+ }
408
+ }
409
+ ```
410
+
411
+ ### Easing
412
+
413
+ ['Cubic Bézier' Design Token type](https://www.designtokens.org/tr/third-editors-draft/format/#cubic-bezier)
414
+
415
+ Easings are mapped to cubic béziers when possible or represented as plain type-less tokens otherwise. [CSS Easing keywords](https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function) are also converted to cubic béziers.
416
+
417
+ ```ts
418
+ let actual = css_to_tokens(`
419
+ .my-design-system {
420
+ animation-timing-function: ease-in-out;
421
+ animation-timing-function: cubic-bezier(0, 0, 0.5, .8);
422
+ animation-timing-function: var(--test);
423
+ }
424
+ `)
425
+
426
+ let easing = {
427
+ 'easing-ea6c7565': {
428
+ $type: 'cubicBezier',
429
+ $value: [
430
+ 0.42,
431
+ 0,
432
+ 0.58,
433
+ 1
434
+ ],
435
+ $extensions: {
436
+ 'com.projectwwallace.css-authored-as': 'ease-in-out',
437
+ 'com.projectwwallace.usage-count': 1,
438
+ }
439
+ },
440
+ 'easing-90111eba': {
441
+ $type: 'cubicBezier',
442
+ $value: [
443
+ 0,
444
+ 0,
445
+ 0.5,
446
+ 0.8
447
+ ],
448
+ $extensions: {
449
+ 'com.projectwwallace.css-authored-as': 'cubic-bezier(0, 0, 0.5, .8)',
450
+ 'com.projectwwallace.usage-count': 1,
451
+ }
452
+ },
453
+ 'easing-12bb7f36': {
454
+ $value: 'var(--test)',
455
+ $extensions: {
456
+ 'com.projectwwallace.css-authored-as': 'var(--test)',
457
+ 'com.projectwwallace.usage-count': 1,
458
+ }
459
+ }
460
+ }
461
+ ```
462
+
463
+ ## Extensions
464
+
465
+ This library adds a couple of potentially extensions to the design token values via the `com.projectwallace` namespace on the `$extensions` property of all generated design tokens.
466
+
467
+ ### Authored CSS values
468
+
469
+ This package parses CSS into Design Tokens but also provides a way to get the authored CSS via the `$extensions['com.projectwallace.css-authored-as']` property on any of the tokens:
65
470
 
66
471
  ```ts
67
472
  let { color } = css_to_tokens(`.my-design-system { color: green; }`)
@@ -80,6 +485,50 @@ let authored_green = color['green-5e0cf03']['$extensions']['com.projectwallace.c
80
485
  // 'green'
81
486
  ```
82
487
 
488
+ ### Usage count
489
+
490
+ If you need to know how often a particalur design token was found in the CSS you can use the `$extensions['com.projectwallace.usage-count']` property on any of the tokens:
491
+
492
+ ```ts
493
+ let { color } = css_to_tokens(`.my-design-system { color: green; }`)
494
+
495
+ // {
496
+ // 'green-5e0cf03': {
497
+ // ...
498
+ // $extensions: {
499
+ // 'com.projectwallace.usage-count': 1
500
+ // }
501
+ // },
502
+ // }
503
+
504
+ let green_count = color['green-5e0cf03']['$extensions']['com.projectwallace.usage-count']
505
+
506
+ // 1
507
+ ```
508
+
509
+ ### CSS property usage
510
+
511
+ __For color tokens only__
512
+
513
+ You can read the `$extensions['com.projectwallace.css-properties']` property to see for which CSS properties a color was used:
514
+
515
+ ```ts
516
+ let { color } = css_to_tokens(`.my-design-system { color: green; }`)
517
+
518
+ // {
519
+ // 'green-5e0cf03': {
520
+ // ...
521
+ // $extensions: {
522
+ // 'com.projectwallace.css-properties': ['color']
523
+ // }
524
+ // },
525
+ // }
526
+
527
+ let properties = color['green-5e0cf03']['$extensions']['com.projectwallace.css-properties']
528
+
529
+ // ['color']
530
+ ```
531
+
83
532
  ## Acknowledgements
84
533
 
85
534
  - [CSSTree](https://github.com/csstree/csstree) does all the heavy lifting of parsing CSS
@@ -88,7 +537,7 @@ let authored_green = color['green-5e0cf03']['$extensions']['com.projectwallace.c
88
537
  ## Related projects
89
538
 
90
539
  - [Design Tokens analyzer](https://www.projectwallace.com/design-tokens) - Online CSS to Design Tokens convernter, uses this package
91
- - [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com?utm_source=github&utm_medium=wallace_format_css_related_projects)
540
+ - [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com?utm_source=github&utm_medium=wallace_css_design_tokens_related_projects)
92
541
  - [Color Sorter](https://github.com/projectwallace/color-sorter) - Sort CSS colors
93
542
  by hue, saturation, lightness and opacity
94
543
  - [CSS Time Sort](https://github.com/projectwallace/css-time-sort) - Sort an array of `<time>` values
@@ -1,8 +0,0 @@
1
- /**
2
- * @description A Set-like construct to search CSS keywords in a case-insensitive way
3
- */
4
- export declare class KeywordSet {
5
- private set;
6
- constructor(items: Lowercase<string>[]);
7
- has(item: string): boolean;
8
- }