-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscss-typography-generator.js
141 lines (118 loc) · 3.26 KB
/
scss-typography-generator.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const prefix = 'db';
const fileHeader = `
@use "variables" as *;
@use "icon/icon-family-calc" as *;
// Do not edit directly
// Generated on
// ${new Date().toString()}
`;
const getShortSize = (size) => {
if (size === '3xlarge') {
return '3xl';
}
if (size === '2xlarge') {
return '2xl';
}
if (size === 'xlarge') {
return 'xl';
}
if (size === 'large') {
return 'lg';
}
if (size === 'medium') {
return 'md';
}
if (size === 'small') {
return 'sm';
}
if (size === 'xsmall') {
return 'xs';
}
if (size === '2xsmall') {
return '2xs';
}
if (size === '3xsmall') {
return '3xs';
}
return size;
};
/**
*
* @param properties {{scale: string, textType:string, size:string, sSize:string, isHeadline:boolean, mQuery: string}}
*/
const getMediaQueryProperties = (properties) => {
const { textType, sSize, scale, size, isHeadline, mQuery } = properties;
let result = `--db-type-${textType}-font-size-${sSize}: #{$${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-font-size};
--db-type-${textType}-line-height-${sSize}: #{$${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-line-height};
`;
if (!isHeadline) {
result += `
--db-base-icon-font-size-${sSize}: #{$${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-font-size};
--db-base-icon-font-family-${sSize}: #{get-icon-family($${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-font-size,
$${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-line-height)};
--db-base-icon-font-family-filled-${sSize}: #{get-icon-family($${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-font-size,
$${prefix}-typography-${scale}-${mQuery}-${textType}-${size}-line-height,"filled")};
`;
}
return result;
};
const getSizeProperties = (scale, textType, size, mQuery) => {
const isHeadline = textType === 'headline';
const sSize = getShortSize(size);
return getMediaQueryProperties({
scale,
size,
textType,
sSize,
isHeadline,
mQuery
});
};
const generateTypography = (typography) => {
let allClasses = fileHeader;
// ScaleTypeKey = [regular, functional, expressive]
for (const scaleTypeKey of Object.keys(typography)) {
const scaleObject = typography[scaleTypeKey];
const mediaQueryKeys = Object.keys(scaleObject);
if (mediaQueryKeys.length > 0) {
// Desktop
const firstMediaQueryKey = mediaQueryKeys[0];
const firstMediaQueryObject = scaleObject[firstMediaQueryKey];
// TextTypeKey = [headline, body]
for (const textTypeKey of Object.keys(firstMediaQueryObject)) {
const textTypeObject = firstMediaQueryObject[textTypeKey];
// SizeKey = [3xlarge - 3xsmall]
allClasses += `
%${prefix}-typography-${textTypeKey}-${scaleTypeKey}{
`;
for (const mQuery of ['mobile', 'tablet', 'desktop']) {
if (mQuery !== 'mobile') {
allClasses += `@media only screen and (min-width: ${
mQuery === 'tablet'
? '#{$db-screens-md}'
: '#{$db-screens-lg}'
}) {
`;
}
for (const sizeKey of Object.keys(textTypeObject)) {
allClasses += getSizeProperties(
scaleTypeKey,
textTypeKey,
sizeKey,
mQuery
);
}
if (mQuery !== 'mobile') {
allClasses += `}
`;
}
}
allClasses += `
}
`;
}
}
}
return allClasses;
};
module.exports = generateTypography;