-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
111 lines (95 loc) · 2.83 KB
/
index.ts
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
const variables = {
scalingRatio: 1.5,
viewportMin: 320,
viewportMax: 1500,
scalingRatioFont: 1.25,
fontSizeToLineHeightRatio: 1.25,
}
export const configure = (configuration: Partial<typeof variables>) => {
Object.assign(variables, configuration)
}
// Round number to three digits and remove zeros.
const round = (value: number) => parseFloat(value.toFixed(3))
const calculation = (_max: number, _min: number) => {
const max = round(_max)
const min = round(_min)
const minReached = `${min}px * var(--min)`
const maxReached = `${max}px * var(--max)`
const difference = max - min
const between = `var(--factor)) * var(--between)`
return `calc((${min}px + ${difference} * ${between} + ${minReached} + ${maxReached})`
}
export const head = () => `
:root {
--factor: (100vw - ${variables.viewportMin}px) / ${variables.viewportMax - variables.viewportMin};
--max: 0;
--between: 1;
--min: 0;
}
@media (min-width: ${variables.viewportMax}px), print {
:root {
--max: 1;
--between: 0;
--min: 0;
}
}
@media (max-width: ${variables.viewportMin}px) {
:root {
--max: 0;
--between: 0;
--min: 1;
}
}`
export const globalVariables = () => ({
':root': {
'--factor': `(100vw - ${variables.viewportMin}px) / ${
variables.viewportMax - variables.viewportMin
}`,
'--max': '0',
'--between': '1',
'--min': '0',
[`@media (min-width: ${variables.viewportMax}px), print`]: {
'--max': '1',
'--between': '0',
},
[`@media (max-width: ${variables.viewportMin}px)`]: {
'--between': '0',
'--min': '1',
},
},
})
export const wasser = (max: number, min = max / variables.scalingRatio) => {
const errorMessage = (place) =>
`wasser: A number is expected as the ${place} parameter for wasser(max: number, min: number).`
if (typeof max !== 'number') {
throw new Error(errorMessage('first'))
}
if (typeof max !== 'number') {
throw new Error(errorMessage('second'))
}
return calculation(max, min)
}
export const font = (
max: number,
min = max / variables.scalingRatioFont,
lineHeightRaio = variables.fontSizeToLineHeightRatio,
) => {
if (typeof max !== 'number' || typeof min !== 'number') {
throw new Error('wasser font(): A number is expected as the first and second parameter.')
}
return `font-size: ${calculation(max, min)};
line-height: ${calculation(max * lineHeightRaio, min * lineHeightRaio)};`
}
export const fontObject = (
max: number,
min = max / variables.scalingRatioFont,
lineHeightRaio = variables.fontSizeToLineHeightRatio,
) => {
if (typeof max !== 'number' || typeof min !== 'number') {
throw new Error('wasser fontObject(): A number is expected as the first and second parameter.')
}
return {
fontSize: calculation(max, min),
lineHeight: calculation(max * lineHeightRaio, min * lineHeightRaio),
}
}