-
Notifications
You must be signed in to change notification settings - Fork 44
/
figmaAttributes.ts
82 lines (77 loc) · 2.37 KB
/
figmaAttributes.ts
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
import type {PlatformConfig, Transform, TransformedToken} from 'style-dictionary/types'
import {asArray} from '../utilities/asArray.js'
type FigmaVariableScope =
| 'ALL_SCOPES'
| 'TEXT_CONTENT'
| 'CORNER_RADIUS'
| 'WIDTH_HEIGHT'
| 'GAP'
| 'ALL_FILLS'
| 'FRAME_FILL'
| 'SHAPE_FILL'
| 'TEXT_FILL'
| 'STROKE_COLOR'
| 'STROKE_FLOAT'
| 'EFFECT_COLOR'
| 'EFFECT_FLOAT'
| 'OPACITY'
| 'FONT_FAMILY'
| 'FONT_STYLE'
| 'FONT_WEIGHT'
| 'FONT_SIZE'
| 'LINE_HEIGHT'
| 'LETTER_SPACING'
| 'PARAGRAPH_SPACING'
| 'PARAGRAPH_INDENT'
const figmaScopes: Record<string, FigmaVariableScope[]> = {
all: ['ALL_SCOPES'],
radius: ['CORNER_RADIUS'],
size: ['WIDTH_HEIGHT'],
gap: ['GAP'],
bgColor: ['FRAME_FILL', 'SHAPE_FILL'],
fgColor: ['TEXT_FILL', 'SHAPE_FILL'],
effectColor: ['EFFECT_COLOR'],
effectFloat: ['EFFECT_FLOAT'],
borderColor: ['STROKE_COLOR'],
borderWidth: ['STROKE_FLOAT'],
opacity: ['OPACITY'],
fontFamily: ['FONT_FAMILY'],
fontStyle: ['FONT_STYLE'],
fontWeight: ['FONT_WEIGHT'],
fontSize: ['FONT_SIZE'],
lineHeight: ['LINE_HEIGHT'],
letterSpacing: ['LETTER_SPACING'],
paragraphSpacing: ['PARAGRAPH_SPACING'],
paragraphIndent: ['PARAGRAPH_INDENT'],
}
const getScopes = (scopes: string[] | string | undefined): FigmaVariableScope[] => {
if (typeof scopes === 'string') scopes = [scopes]
if (Array.isArray(scopes))
return scopes
.map(scope => {
if (scope in figmaScopes) return figmaScopes[scope]
throw new Error(`Invalid scope: ${scope}`)
})
.flat() as FigmaVariableScope[]
return ['ALL_SCOPES']
}
/**
* @description retrieves figma attributes from token and adds them to attributes
* @type attribute transformer — [StyleDictionary.AttributeTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher matches all tokens
* @transformer returns ab object of figma attributes
*/
export const figmaAttributes: Transform = {
name: 'figma/attributes',
type: `attribute`,
transform: (token: TransformedToken, platform: PlatformConfig = {}) => {
const {modeOverride, collection, scopes, group, codeSyntax} = token.$extensions?.['org.primer.figma'] || {}
return {
mode: asArray(platform.options?.theme)[0] || modeOverride || 'default',
collection,
group: group || collection,
scopes: getScopes(scopes),
codeSyntax,
}
},
}