1 | export function isUnitless(value) {
|
2 | return String(parseFloat(value)).length === String(value).length;
|
3 | }
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export function getUnit(input) {
|
9 | return String(input).match(/[\d.\-+]*\s*(.*)/)[1] || '';
|
10 | }
|
11 |
|
12 |
|
13 | export function toUnitless(length) {
|
14 | return parseFloat(length);
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | export function convertLength(baseFontSize) {
|
20 | return (length, toUnit) => {
|
21 | const fromUnit = getUnit(length);
|
22 |
|
23 |
|
24 | if (fromUnit === toUnit) {
|
25 | return length;
|
26 | }
|
27 |
|
28 |
|
29 | let pxLength = toUnitless(length);
|
30 | if (fromUnit !== 'px') {
|
31 | if (fromUnit === 'em') {
|
32 | pxLength = toUnitless(length) * toUnitless(baseFontSize);
|
33 | } else if (fromUnit === 'rem') {
|
34 | pxLength = toUnitless(length) * toUnitless(baseFontSize);
|
35 | }
|
36 | }
|
37 |
|
38 |
|
39 | let outputLength = pxLength;
|
40 | if (toUnit !== 'px') {
|
41 | if (toUnit === 'em') {
|
42 | outputLength = pxLength / toUnitless(baseFontSize);
|
43 | } else if (toUnit === 'rem') {
|
44 | outputLength = pxLength / toUnitless(baseFontSize);
|
45 | } else {
|
46 | return length;
|
47 | }
|
48 | }
|
49 | return parseFloat(outputLength.toFixed(5)) + toUnit;
|
50 | };
|
51 | }
|
52 | export function alignProperty({
|
53 | size,
|
54 | grid
|
55 | }) {
|
56 | const sizeBelow = size - size % grid;
|
57 | const sizeAbove = sizeBelow + grid;
|
58 | return size - sizeBelow < sizeAbove - size ? sizeBelow : sizeAbove;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export function fontGrid({
|
65 | lineHeight,
|
66 | pixels,
|
67 | htmlFontSize
|
68 | }) {
|
69 | return pixels / (lineHeight * htmlFontSize);
|
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 | export function responsiveProperty({
|
104 | cssProperty,
|
105 | min,
|
106 | max,
|
107 | unit = 'rem',
|
108 | breakpoints = [600, 900, 1200],
|
109 | transform = null
|
110 | }) {
|
111 | const output = {
|
112 | [cssProperty]: `${min}${unit}`
|
113 | };
|
114 | const factor = (max - min) / breakpoints[breakpoints.length - 1];
|
115 | breakpoints.forEach(breakpoint => {
|
116 | let value = min + factor * breakpoint;
|
117 | if (transform !== null) {
|
118 | value = transform(value);
|
119 | }
|
120 | output[`@media (min-width:${breakpoint}px)`] = {
|
121 | [cssProperty]: `${Math.round(value * 10000) / 10000}${unit}`
|
122 | };
|
123 | });
|
124 | return output;
|
125 | } |
\ | No newline at end of file |