-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (77 loc) · 2.58 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const plugin = require('tailwindcss/plugin')
const rem = (px) => {
return (parseFloat(px) / 16).toFixed(4).replace(/\.?0+$/, '') + 'rem'
}
const dynamicSpacing = plugin.withOptions(
() => {
return () => {
// empty
}
},
(options) => {
const spacing = (screens, pn = 'positive') => {
const sizes = [
...(options?.sizes ?? Object.keys(require('tailwindcss/defaultTheme').spacing)),
...(options?.extend?.sizes ?? []),
]
.filter((size) => Number(size) !== 0 && !isNaN(size))
.map((size) => `${size}`)
const values = sizes.reduce(
(acc, cur) => ({
...acc,
[cur]: cur * 0.3125,
}),
{}
)
const obj = {}
for (const [size, value] of Object.entries(values)) {
if (pn === 'positive') {
obj[`vw-${size}`] = `${value}vw`
}
for (const [screenName, screenSize] of Object.entries(screens)) {
const minmax = (parseFloat(screenSize) * value) / 100
if (pn === 'positive') {
obj[`vw-${size}-min@${screenName}`] = `max(${value}vw, ${rem(minmax)})`
obj[`vw-${size}-max@${screenName}`] = `min(${value}vw, ${rem(minmax)})`
} else if (pn === 'negative') {
obj[`-vw-${size}-min@${screenName}`] = `min(-${value}vw, -${rem(minmax)})`
obj[`-vw-${size}-max@${screenName}`] = `max(-${value}vw, -${rem(minmax)})`
}
for (const [screenName2, screenSize2] of Object.entries(screens)) {
if (parseFloat(screenSize2) <= parseFloat(screenSize)) continue
const minmax2 = (parseFloat(screenSize2) * value) / 100
if (pn === 'positive') {
obj[`vw-${size}-min@${screenName}-max@${screenName2}`] = `clamp(
${rem(minmax)}, ${value}vw, ${rem(minmax2)}
)`
} else if (pn === 'negative') {
obj[`-vw-${size}-min@${screenName}-max@${screenName2}`] = `clamp(
-${rem(minmax2)}, -${value}vw, -${rem(minmax)}
)`
}
}
}
}
return obj
}
return {
theme: {
extend: {
spacing: (theme) => {
return spacing(theme('screens'))
},
margin: (theme) => {
return spacing(theme('screens'), 'negative')
},
inset: (theme) => {
return spacing(theme('screens'), 'negative')
},
translate: (theme) => {
return spacing(theme('screens'), 'negative')
},
},
},
}
}
)
module.exports = dynamicSpacing