-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtypography.js
56 lines (48 loc) · 1.55 KB
/
typography.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
import * as R from 'ramda'
import { withType, withName, removeNilAndEmpty } from '../functions/utils.js'
import { rootFontSize, rem, em } from '../functions/units.js'
import { fillToRgba } from './colors.js'
const fallback = {}
const cssVarName = (name) => `eds_${name}_color`
export const toTypography = (figmaNode, name) => {
let typography = {}
const {
fontFamily,
fontSize,
fontWeight,
letterSpacing,
textAlignHorizontal: textAlign,
lineHeightPercentFontSize,
textDecoration,
textCase,
italic,
} = figmaNode.style
const { fills } = figmaNode
const isMonospaced = withName('monospaced', {
name: R.isNil(name) ? figmaNode.name : name,
})
if (isMonospaced) {
typography = {
...typography,
fontFeature: "'tnum' on,'lnum' on",
}
}
const fill = R.find(withType('solid'), fills) || fallback
const fontSizeRem = (fontSize / rootFontSize).toFixed(3)
const lineHeightEm = (lineHeightPercentFontSize / 100).toFixed(3)
const letterSpacingEm = (letterSpacing / rootFontSize).toFixed(3)
typography = {
...typography,
color: `var(--${cssVarName(name)}, ${fillToRgba(fill)})`,
fontFamily,
fontSize: rem(fontSizeRem),
fontWeight,
letterSpacing: letterSpacing ? em(letterSpacingEm) : null,
lineHeight: em(lineHeightEm),
textDecoration: textDecoration ? R.toLower(textDecoration) : null,
textTransform: textCase ? R.toLower(`${textCase}CASE`) : null,
fontStyle: italic ? 'italic' : null,
textAlign: R.toLower(textAlign),
}
return removeNilAndEmpty(typography)
}