-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluidFontSizePlugin.js
69 lines (56 loc) · 2.08 KB
/
fluidFontSizePlugin.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
module.exports = function ({ addComponents, addUtilities, theme }) {
const fluidFontSizeUtilities = {};
const fontSizes = theme("fontSize", {});
const defaultSettings = {
breakpoint: 1440,
minValue: 16,
minValueBreakpoint: 320,
slopeAdjust: 0.6
};
const config = theme("fluidFontSize", { settings: defaultSettings });
const settings = config.settings;
const { breakpoint, minValue, minValueBreakpoint, slopeAdjust } = settings;
const parseCSSValue = (value) => {
// Use a regular expression to extract the numeric part and the unit part
const matches = value.match(/^([\d.]+)([^\d]*)/);
if (!matches) {
return [16, "px"];
}
const numericValue = parseFloat(matches[1]);
const unit = matches[2];
return [numericValue, unit];
};
const getComputedValue = (propertyValue) => {
const [value, unit] = parseCSSValue(propertyValue);
if (["rem", "em"].includes(unit)) {
return value * 16;
}
if (unit === "px") {
return value;
}
return 16;
};
for (const [fontSizeName, size] of Object.entries(fontSizes)) {
const className = `text-fluid-${fontSizeName}`;
const fontSize = size[0];
const lineHeight = size[1];
const computedValue = getComputedValue(fontSize);
const y0 = computedValue - (computedValue - minValue) * slopeAdjust;
const a = (computedValue - y0) / (breakpoint - minValueBreakpoint);
const b = y0 - a * minValueBreakpoint;
fluidFontSizeUtilities[`.${className}`] = {
fontSize: `min(calc(${a * 100} * var(--_fluid-unit, 1vw) + ${b}px), ${computedValue}px)`,
};
if (lineHeight) {
fluidFontSizeUtilities[`.${className}`].lineHeight = lineHeight;
}
}
const customComponents = {
".fluid-root": {
containerType: "inline-size",
"--_fluid-unit": "1cqw",
},
};
addComponents(customComponents);
addUtilities(fluidFontSizeUtilities);
};