-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (142 loc) · 4.04 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const plugin = require("tailwindcss/plugin");
const { map, fromPairs, find, isPlainObject, get } = require("lodash");
const parser = require("postcss-selector-parser");
const escapeClassName = (className) => {
const node = parser.className();
node.value = className;
return get(node, "raws.value", node.value);
};
const stripUnit = (string) => string.match(/[\d.]+/)[0];
const asClass = (name) => `.${escapeClassName(name)}`;
const nameClass = (classPrefix, key) => {
if (key === "DEFAULT") {
return asClass(classPrefix);
}
if (key === "-") {
return asClass(`-${classPrefix}`);
}
if (key.startsWith("-")) {
return asClass(`-${classPrefix}${key}`);
}
return asClass(`${classPrefix}-${key}`);
};
const offsetFromLineHeight = (value) => {
const valueStripped = stripUnit(value);
return valueStripped === value
? (Number(value) - 1) / -2 + "em"
: `calc( (${value} - 1em) / -2)`;
};
const fontMetricsList = require("./metrics.json");
module.exports = plugin(
({ addUtilities, e, theme, variants }) => {
addUtilities({
".trim-start": {
"margin-top":
"calc( var(--leading-offset,-.25em) + var(--font-offset-start,0em) )",
},
".trim-end": {
"margin-bottom":
"calc( var(--leading-offset,-.25em) + var(--font-offset-end,0em) )",
},
".trim-both": {
"margin-top":
"calc( var(--leading-offset,-.25em) + var(--font-offset-start,0em) )",
"margin-bottom":
"calc( var(--leading-offset,-.25em) + var(--font-offset-end,0em) )",
},
});
const fontFamilyUtilities = fromPairs(
map(theme("fontFamily"), (value, modifier) => {
const preferedFont = Array.isArray(value) ? value[0] : value;
const fontFamily = Array.isArray(value) ? value.join(", ") : value;
const fontMetrics =
theme("fontMetrics." + preferedFont) ||
find(fontMetricsList, {
familyName: preferedFont,
});
if (!fontMetrics) {
console.log("can't find font metrics for font: " + preferedFont);
return [
`.${e(`font-${modifier}`)}`,
{
"font-family": fontFamily,
},
];
}
const { ascent, descent, unitsPerEm, capHeight } = fontMetrics;
const foo = (ascent - descent - unitsPerEm) / 2;
const offsetStart = (ascent - capHeight - foo) / -unitsPerEm + "em";
const offsetEnd = (foo + descent) / unitsPerEm + "em";
const ajust = unitsPerEm / capHeight + "";
return [
`.${e(`font-${modifier}`)}`,
{
"font-family": fontFamily,
"--font-offset-start": offsetStart,
"--font-offset-end": offsetEnd,
"--font-adjust": ajust,
},
];
})
);
addUtilities(fontFamilyUtilities, variants("fontFamily"));
const numericFontSizeUtilities = fromPairs(
map(theme("spacing"), (value, modifier) => {
return [
nameClass("text", modifier),
{
"font-size": `calc(${value} * var(--font-adjust,1))`,
},
];
})
);
addUtilities(numericFontSizeUtilities, variants("fontSize"));
const fontSizeUtilities = fromPairs(
map(theme("fontSize"), (value, modifier) => {
const [fontSize, options] = Array.isArray(value) ? value : [value];
const { lineHeight, letterSpacing } = isPlainObject(options)
? options
: {
lineHeight: options,
};
return [
nameClass("text", modifier),
{
"font-size": fontSize,
...(lineHeight === undefined
? {}
: {
"line-height": lineHeight,
"--leading-offset": offsetFromLineHeight(lineHeight),
}),
...(letterSpacing === undefined
? {}
: {
"letter-spacing": letterSpacing,
}),
},
];
})
);
addUtilities(fontSizeUtilities, variants("fontSize"));
const lineHeightUtilities = fromPairs(
map(theme("lineHeight"), (value, modifier) => {
return [
`.${e(`leading-${modifier}`)}`,
{
"line-height": value,
"--leading-offset": offsetFromLineHeight(value),
},
];
})
);
addUtilities(lineHeightUtilities, variants("lineHeight"));
},
{
corePlugins: {
fontFamily: false,
fontSize: false,
lineHeight: false,
},
}
);