Skip to content

Commit d4bc5c1

Browse files
authored
readme and types improvements (#9)
* readme and types improvements * unduplictae imports
1 parent fa8c83c commit d4bc5c1

File tree

3 files changed

+92
-20
lines changed

3 files changed

+92
-20
lines changed

readme.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# css-design-tokens
22

3-
Create Design Tokens by going through CSS to find colors, font-sizes, gradients etcetera and turn them into a spec-compliant token format.
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.
44

55
## Installation
66

@@ -13,26 +13,82 @@ npm install @projectwallace/css-design-tokens
1313
```js
1414
import { css_to_tokens } from '@projectwallace/css-design-tokens'
1515

16-
let tokens = css_to_tokens(`.my-design-system { color: green; }`)
16+
let {
17+
color,
18+
font_size,
19+
font_family,
20+
line_height,
21+
gradient,
22+
box_shadow,
23+
radius,
24+
duration,
25+
easing,
26+
} = css_to_tokens(`.my-design-system { color: green; }`)
1727

18-
// Or if you already have done CSS analysis with @projectwallace/css-analyzer:
19-
// NOTE: it is important that `useLocations` is true
28+
// Or if you already have done CSS analysis with @projectwallace/css-analyzer
2029
import { analyze } from '@projectwallace/css-analyzer'
2130
import { analysis_to_tokens } from '@projectwallace/css-design-tokens'
2231

2332
let analysis = analyze(`.my-design-system { color: green; }`, {
24-
useLocations: true // MUST be true
33+
useLocations: true // may be `true` or `false`, it works either way
2534
})
2635
let tokens = analysis_to_tokens(analysis)
2736
```
2837

38+
### Stable unique token ID's
39+
40+
All tokens have a stabe unique ID using a very simple hashing algorithm. This is helpful when you run analysis multiple times over your project and lets you identify removed or added tokens easily.
41+
42+
```ts
43+
let { color } = css_to_tokens(
44+
`.my-design-system {
45+
color: green;
46+
color: rgb(100 100 100 / 20%);
47+
}`
48+
)
49+
50+
// {
51+
// 'green-5e0cf03': {
52+
// $type: 'color',
53+
// ...
54+
// },
55+
// 'grey-8139d9b': {
56+
// $type: 'color',
57+
// ...
58+
// }
59+
// }
60+
```
61+
62+
### Getting authored values
63+
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.
65+
66+
```ts
67+
let { color } = css_to_tokens(`.my-design-system { color: green; }`)
68+
69+
// {
70+
// 'green-5e0cf03': {
71+
// ...
72+
// $extensions: {
73+
// 'com.projectwallace.css-authored-as': 'green'
74+
// }
75+
// },
76+
// }
77+
78+
let authored_green = color['green-5e0cf03']['$extensions']['com.projectwallace.css-authored-as']
79+
80+
// 'green'
81+
```
82+
2983
## Acknowledgements
3084

3185
- [CSSTree](https://github.com/csstree/csstree) does all the heavy lifting of parsing CSS
32-
- [ColorJS.io](https://colorjs.io/) powers all color conversions necessary for grouping and sorting
86+
- [ColorJS.io](https://colorjs.io/) powers all color conversions necessary for grouping and sorting and converting into Color tokens
3387

3488
## Related projects
3589

90+
- [Design Tokens analyzer](https://www.projectwallace.com/design-tokens) - Online CSS to Design Tokens convernter, uses this package
3691
- [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)
3792
- [Color Sorter](https://github.com/projectwallace/color-sorter) - Sort CSS colors
3893
by hue, saturation, lightness and opacity
94+
- [CSS Time Sort](https://github.com/projectwallace/css-time-sort) - Sort an array of `<time>` values

src/index.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { analyze } from '@projectwallace/css-analyzer'
22
import { convert as convert_duration } from 'css-time-sort'
33
import { group_colors, color_dict } from './group-colors.js'
4-
import { destructure_box_shadow, type DestructuredShadow } from './destructure-box-shadow.js'
4+
import { destructure_box_shadow } from './destructure-box-shadow.js'
55
import { destructure_easing } from './destructure-easing.js'
66
import { destructure_font_family } from './destructure-font-family.js'
77
import { hash } from './hash.js'
88
import { destructure_line_height } from './destructure-line-height.js'
99
import { parse_length } from './parse-length.js'
10-
import type { ColorToken, CssAnalysis } from './types.js'
1110
import {
1211
EXTENSION_AUTHORED_AS,
1312
type CubicBezierToken,
1413
type DimensionToken,
1514
type DurationToken,
1615
type FontFamilyToken,
1716
type NumberToken,
18-
type BaseToken,
1917
type UnparsedToken,
18+
type ColorToken,
19+
type CssAnalysis,
20+
type ShadowToken,
2021
} from './types.js'
2122
import { color_to_token } from './colors.js'
2223

@@ -27,17 +28,31 @@ export function css_to_tokens(css: string) {
2728

2829
// TODO: get @projectwallace/css-analyzer types in order
2930
type UniqueCount = Record<string, number>
30-
type UniqueWithLocations = Record<string, {
31+
type CssLocation = {
3132
line: number
3233
column: number
3334
offset: number
3435
length: number
35-
}[]>
36+
}
37+
type UniqueWithLocations = Record<string, CssLocation[]>
3638
type Collection = {
3739
unique: UniqueCount
3840
} | {
3941
uniqueWithLocations: UniqueWithLocations
4042
}
43+
type TokenID = string
44+
45+
type Tokens = {
46+
color: Record<TokenID, ColorToken | UnparsedToken>
47+
font_size: Record<TokenID, UnparsedToken | DimensionToken>
48+
font_family: Record<TokenID, FontFamilyToken>
49+
line_height: Record<TokenID, UnparsedToken | DimensionToken | NumberToken>
50+
gradient: Record<TokenID, UnparsedToken>
51+
box_shadow: Record<TokenID, ShadowToken | UnparsedToken>
52+
radius: Record<TokenID, UnparsedToken>
53+
duration: Record<TokenID, DurationToken | UnparsedToken>
54+
easing: Record<TokenID, UnparsedToken | CubicBezierToken>
55+
}
4156

4257
/**
4358
* Function to get the unique values from a collection regardless of whether the analysis was run with
@@ -50,7 +65,7 @@ function get_unique(collection: Collection) {
5065
return collection.unique
5166
}
5267

53-
export function analysis_to_tokens(analysis: CssAnalysis) {
68+
export function analysis_to_tokens(analysis: CssAnalysis): Tokens {
5469
return {
5570
color: (() => {
5671
let colors = Object.create(null) as Record<string, ColorToken | UnparsedToken>
@@ -165,11 +180,6 @@ export function analysis_to_tokens(analysis: CssAnalysis) {
165180
return gradients
166181
})(),
167182
box_shadow: (() => {
168-
type ShadowToken = BaseToken & {
169-
$type: 'shadow'
170-
$value: DestructuredShadow | DestructuredShadow[]
171-
}
172-
173183
let shadows = Object.create(null) as Record<string, ShadowToken | UnparsedToken>
174184
let unique = get_unique(analysis.values.boxShadows)
175185

@@ -183,15 +193,15 @@ export function analysis_to_tokens(analysis: CssAnalysis) {
183193
$extensions: {
184194
[EXTENSION_AUTHORED_AS]: box_shadow
185195
}
186-
} as UnparsedToken
196+
}
187197
} else {
188198
shadows[name] = {
189199
$type: 'shadow',
190-
$value: parsed.length === 1 ? parsed[0] : parsed,
200+
$value: parsed.length === 1 ? parsed[0]! : parsed,
191201
$extensions: {
192202
[EXTENSION_AUTHORED_AS]: box_shadow
193203
}
194-
} as ShadowToken
204+
}
195205
}
196206
}
197207
return shadows

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { analyze } from '@projectwallace/css-analyzer'
2+
import type { DestructuredShadow } from './destructure-box-shadow'
23

34
export type CssAnalysis = ReturnType<typeof analyze>
45

@@ -65,3 +66,8 @@ export type FontFamilyToken = BaseToken & {
6566
$type: 'fontFamily'
6667
$value: string[] | string
6768
}
69+
70+
export type ShadowToken = BaseToken & {
71+
$type: 'shadow'
72+
$value: DestructuredShadow | DestructuredShadow[]
73+
}

0 commit comments

Comments
 (0)