-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (104 loc) · 3.51 KB
/
index.js
File metadata and controls
130 lines (104 loc) · 3.51 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const plugin = require('tailwindcss/plugin')
/**
* Returns object's key according to value match.
* @param {string} value
* @param {[key: string]: string} plainObject
* @returns
*/
const reverseSearch = (value, plainObject) => {
const tuple = Object.entries(plainObject).find(([key, val]) => val === value)
if (tuple !== undefined) {
const [key, _] = tuple
return key
}
return undefined
}
function tailwindcssResponsivePlugin() {
return plugin(function ({ theme, matchUtilities }) {
const PADDING_MAPING = {
'rp': ['padding'],
'rpl': ['padding-left'],
'rpr': ['padding-right'],
'rpt': ['padding-top'],
'rpb': ['padding-bottom'],
'rpx': ['padding-left', 'padding-right'],
'rpy': ['padding-top', 'padding-bottom'],
}
const MARGIN_MAPING = {
'rm': ['margin'],
'rml': ['margin-left'],
'rmr': ['margin-right'],
'rmt': ['margin-top'],
'rmb': ['margin-bottom'],
'rmx': ['margin-left', 'margin-right'],
'rmy': ['margin-top', 'margin-bottom'],
}
const FONT_SIZE = {
'rtext': ['font-size'],
}
const createResponsiveCssProp = (propMap, confPropName, valuesSource) => {
const themeScreens = theme('screens')
const screenTuples = Object.entries(themeScreens).sort(([, aval], [, bval]) => {
const faval = parseFloat(aval)
const fbval = parseFloat(bval)
if (faval > fbval) {
return 1
} else if (faval < fbval) {
return -1
}
return 0
})
// Get corresponding value accordig to screen definition
const mediaQueryValue = (screen, val) => theme(`${confPropName}.${screen}`)[reverseSearch(val, valuesSource)]
for (const [tailwindClassName, cssProps] of Object.entries(propMap)) {
// Handle default screen, no media query is present here
const defaultScreenTailwindHandle = {
[tailwindClassName]: (value) => {
// flat object - no nesting
let fo = {}
cssProps.forEach(cssProp => {
fo[cssProp] = mediaQueryValue('DEFAULT', value)
})
return fo
}
}
// Handle other screens, add media query
const screensExceptDefault = screenTuples.filter(([screenName,]) => screenName !== 'DEFAULT')
const otherScreensTailwindHandle = {
[tailwindClassName]: (value) => {
// flat object - no nesting
let fo = {}
// go over all screens
screensExceptDefault.forEach(([screen, ]) => {
// put all props in one object
let css = {}
cssProps.forEach(prop => {
css[prop] = mediaQueryValue(screen, value)
})
fo[`@media (min-width: ${themeScreens[screen]})`] = {
...css
}
})
return fo
}
}
matchUtilities(
defaultScreenTailwindHandle,
{ values: valuesSource }
)
matchUtilities(
otherScreensTailwindHandle,
{ values: valuesSource }
)
}
}
if (theme('rspacing')) {
createResponsiveCssProp(PADDING_MAPING, 'rspacing', theme('rspacing.DEFAULT'))
createResponsiveCssProp(MARGIN_MAPING, 'rspacing', theme('rspacing.DEFAULT'))
}
if (theme('rtext')) {
createResponsiveCssProp(FONT_SIZE, 'rtext', theme('rtext.DEFAULT'))
}
})
}
module.exports = tailwindcssResponsivePlugin