-
Notifications
You must be signed in to change notification settings - Fork 4
/
CreateTextStyles.js
105 lines (88 loc) · 2.5 KB
/
CreateTextStyles.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
import { StyleSheet } from "react-native";
const DEFAULT_PARAGRAPH_SIZES = {
p1: 18,
p2: 16,
p3: 14,
p4: 12,
p5: 10,
p6: 8
};
const DEFAULT_HEADER_SIZES = {
h1: 30,
h2: 26,
h3: 22,
h4: 20,
h5: 18,
h6: 16
};
const DEFAULT_H_FONT_WEIGHT = "300";
const DEFAULT_P_FONT_WEIGHT = "300";
const DEFAULT_FONT_FAMILY = "System";
class CreateTextStyles {
constructor(config = {}) {
const {
paragraphSizes = DEFAULT_PARAGRAPH_SIZES,
headerSizes = DEFAULT_HEADER_SIZES,
hFontWeight = DEFAULT_H_FONT_WEIGHT,
pFontWeight = DEFAULT_P_FONT_WEIGHT,
fontFamily = DEFAULT_FONT_FAMILY,
applyPLetterSpacing = false,
applyHLetterSpacing = false,
hLineHeightCoefficient = 1.65,
pLineHeightCoefficient = 1.65,
} = config;
this.paragraphSizes = paragraphSizes;
this.headerSizes = headerSizes;
this.hFontWeight = hFontWeight;
this.pFontWeight = pFontWeight;
this.fontFamily = fontFamily;
this.applyPLetterSpacing = applyPLetterSpacing;
this.applyHLetterSpacing = applyHLetterSpacing;
this.hLineHeightCoefficient = hLineHeightCoefficient
this.pLineHeightCoefficient = pLineHeightCoefficient
this.allTStyles = this.buildTextStyles();
}
get allTextStyles() {
return this.allTStyles;
}
buildTextStyles = () => {
return StyleSheet.create({
...this.createParFontStyleObj(),
...this.createHeaderFontStyleObj()
});
};
createParFontStyleObj = () => {
const paragraphStyles = {};
Object.keys(this.paragraphSizes).forEach(key => {
const fontSize = this.paragraphSizes[key];
paragraphStyles[key] = {
fontFamily: this.fontFamily,
fontSize,
fontWeight: this.pFontWeight,
lineHeight: fontSize * this.pLineHeightCoefficient
};
if (this.applyPLetterSpacing) {
paragraphStyles[key].letterSpacing = fontSize / 12;
}
});
return paragraphStyles;
};
createHeaderFontStyleObj = () => {
const headerStyles = {};
Object.keys(this.headerSizes).forEach(key => {
const fontSize = this.headerSizes[key];
headerStyles[key] = {
fontFamily: this.fontFamily,
fontSize,
fontWeight: this.pFontWeight,
lineHeight: fontSize * this.hLineHeightCoefficient
};
if (this.applyHLetterSpacing) {
headerStyles[key].letterSpacing = fontSize / 12;
}
});
return headerStyles;
};
}
const DefaultTextStyles = new CreateTextStyles();
export { CreateTextStyles, DefaultTextStyles };