-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
61 lines (54 loc) · 2.12 KB
/
utils.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
// Mostly from Braid Design System by SEEK
// Cf. https://github.com/seek-oss/braid-design-system/blob/7acbe2f92f9313b95d3ab25bfc5fc1bce29dfdb3/lib/hooks/typography/baseline.ts
// Modified to taking in all values as rems.
function baselineStyles({
typeSizeModifier,
baseFontSize,
descenderHeightScale,
typeRowSpan,
gridRowHeight,
capHeight
}) {
const fontSize = typeSizeModifier * baseFontSize;
const calculateTypeOffset = lh => {
const lineHeightScale = lh / fontSize;
return (lineHeightScale - 1) / 2 + descenderHeightScale;
};
const lineHeight = typeRowSpan * gridRowHeight;
const typeOffset = calculateTypeOffset(lineHeight);
const topSpace = lineHeight - capHeight * fontSize;
const heightCorrection =
topSpace > gridRowHeight ? topSpace - (topSpace % gridRowHeight) : 0;
const preventCollapse = gridRowHeight;
return {
fontSize: `${fontSize}rem`,
lineHeight: `${lineHeight}rem`,
transform: `translateY(${typeOffset}em)`,
paddingTop: `${preventCollapse}rem`,
"&:before": {
content: "''",
marginTop: `-${heightCorrection + preventCollapse}rem`,
display: "block",
height: 0
}
};
}
// Mostly from Polished by Styled Components
// Cf. https://github.com/styled-components/polished/blob/3fdfe9b73a5b5c7f5636c42471c2bc9bed2f56ca/src/helpers/getValueAndUnit.js
const cssRegex = /^([+-]?(?:\d+|\d*\.\d+))([a-z]*|%)$/;
function getValueAndUnit(value) {
if (typeof value !== "string") return [value, ""];
const matchedValue = value.match(cssRegex);
if (matchedValue) return [parseFloat(value), matchedValue[2]];
return [value, undefined];
}
function createCssSelectors(fontFamily, sizeName, leading) {
return (
`.font-${fontFamily}.text-${sizeName}.leading-${leading}.baseline, ` +
`.font-${fontFamily} .text-${sizeName}.leading-${leading}.baseline, ` +
`.font-${fontFamily} .text-${sizeName} .leading-${leading}.baseline ,` +
`.text-${sizeName} .font-${fontFamily}.leading-${leading}.baseline, ` +
`.text-${sizeName} .font-${fontFamily} .leading-${leading}.baseline`
);
}
module.exports = { baselineStyles, getValueAndUnit, createCssSelectors };