-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (66 loc) · 2.07 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
const plugin = require('tailwindcss/plugin')
const viewportSpacing = plugin.withOptions((options) => function ({matchUtilities}) {
const units = ['vw', 'vh']
const utilityNames = {
p: 'padding',
px: ['padding-left', 'padding-right'],
py: ['padding-top', 'padding-bottom'],
pt: 'padding-top',
pr: 'padding-right',
pb: 'padding-bottom',
pl: 'padding-left',
m: 'margin',
mx: ['margin-left', 'margin-right'],
my: ['margin-top', 'margin-bottom'],
mt: 'margin-top',
mr: 'margin-right',
mb: 'margin-bottom',
ml: 'margin-left',
top: 'top',
right: 'right',
bottom: 'bottom',
left: 'left',
w: 'width',
h: 'height',
'max-w': 'max-width',
'max-h': 'max-height',
'min-w': 'min-width',
'min-h': 'min-height',
text: 'font-size',
start: 'inset-inline-start',
end: 'inset-inline-end',
inset: 'inset',
}
const generateCssValue = (value, multiplier, unit) => `${value * (100 / multiplier)}${unit}`
const generateUtilities = (prop, value, multiplier, unit) => {
const props = Array.isArray(prop) ? prop : [prop]
return props.reduce((acc, prop) => {
acc[prop] = generateCssValue(value, multiplier, unit)
return acc
}, {})
}
const processEntries = (entries, generateFunction, multiplier, unit) => {
entries.forEach(([key, prop]) => {
matchUtilities({
[`${key}`]: (value) => {
return generateFunction(prop, value, multiplier, unit)
},
})
})
}
const utilityEntries = (unit, screenName = '') => {
return Object.entries(utilityNames).map(([key, prop]) => {
return [`${key}-${unit}${screenName ? `-${screenName}` : ''}`, prop]
})
}
const processScreens = (unit, screenName) => {
const screenMultiplier = options[unit][screenName]
processEntries(utilityEntries(unit, screenName), generateUtilities, screenMultiplier, unit)
}
units.forEach((unit) => {
Object.keys(options[unit]).forEach((screenName) => {
processScreens(unit, screenName)
})
})
})
module.exports = viewportSpacing