UNPKG

9.1 kBJavaScriptView Raw
1const plugin = require('tailwindcss/plugin')
2const _ = require('lodash')
3
4module.exports = plugin.withOptions(() => {
5 return function ({ addComponents, addVariant, e, theme }) {
6 const componentsBase = require('./tailwind.components.js') || {}
7 const componentsCustom = theme(`xtendui`, {}) || {}
8
9 /**
10 * components
11 */
12
13 for (const component of Object.keys(componentsBase)) {
14 const componentBase = componentsBase[component] || {}
15 const componentCustom = componentsCustom[component] || {}
16 if (componentCustom !== false && componentCustom.component !== false) {
17 const base =
18 typeof componentBase.component === 'function' ? componentBase.component(theme) : componentBase.component
19 const custom =
20 typeof componentCustom.component === 'function' ? componentCustom.component(theme) : componentCustom.component
21 const css = _.merge(..._.castArray(base || {}), custom || {})
22 addComponents(css, {
23 respectPrefix: false,
24 })
25 }
26 }
27
28 /**
29 * utilities
30 */
31
32 for (const component of Object.keys(componentsBase)) {
33 const componentBase = componentsBase[component] || {}
34 const componentCustom = componentsCustom[component] || {}
35 if (componentCustom !== false && componentCustom.utility !== false) {
36 const base = typeof componentBase.utility === 'function' ? componentBase.utility(theme) : componentBase.utility
37 const custom =
38 typeof componentCustom.utility === 'function' ? componentCustom.utility(theme) : componentCustom.utility
39 const variants = _.merge(componentBase.variants || [], componentCustom.variants || [])
40 const options = _.merge(..._.castArray(base || {}), custom || {})
41 const utilities = Object.keys(options)
42 for (const utility of utilities) {
43 if (componentsCustom[utility] !== false) {
44 if (component === 'list' && utility === 'space') {
45 // list space
46 const css = {}
47 Object.keys(options[utility]).forEach(name => {
48 const value = options[utility][name]
49 css[`.${e(`xt-list-${name}`)}`] = {
50 marginTop: `-${value}`,
51 marginLeft: `-${value}`,
52 '> *': {
53 marginTop: `${value}`,
54 marginLeft: `${value}`,
55 },
56 }
57 css[`.${e(`xt-list-x-${name}`)}`] = {
58 marginLeft: `-${value}`,
59 '> *': {
60 marginLeft: `${value}`,
61 },
62 }
63 css[`.${e(`xt-list-y-${name}`)}`] = {
64 marginTop: `-${value}`,
65 '> *': {
66 marginTop: `${value}`,
67 },
68 }
69 })
70 addComponents(css, {
71 variants: variants,
72 respectPrefix: false,
73 })
74 } else if (component === 'row' && utility === 'space') {
75 // row space
76 const css = {}
77 Object.keys(options[utility]).forEach(name => {
78 const value = options[utility][name]
79 css[`.${e(`xt-row-${name}`)}`] = {
80 marginTop: `-${value}`,
81 marginLeft: `-${value}`,
82 '> *': {
83 paddingTop: `${value}`,
84 paddingLeft: `${value}`,
85 },
86 }
87 css[`.${e(`xt-row-x-${name}`)}`] = {
88 marginLeft: `-${value}`,
89 '> *': {
90 paddingLeft: `${value}`,
91 },
92 }
93 css[`.${e(`xt-row-y-${name}`)}`] = {
94 marginTop: `-${value}`,
95 '> *': {
96 paddingTop: `${value}`,
97 },
98 }
99 })
100 addComponents(css, {
101 variants: variants,
102 respectPrefix: false,
103 })
104 } else if (component === 'overlay' && utility === '.xt-overlay-container') {
105 const css = {}
106 Object.keys(options[utility]).forEach(name => {
107 const value = options[utility][name]
108 if (name === 'DEFAULT') {
109 css[utility] = {
110 padding: value,
111 }
112 } else {
113 css[utility] = {
114 ...css[utility],
115 [`@screen ${name}`]: {
116 padding: value,
117 },
118 }
119 }
120 })
121 addComponents(css, {
122 respectPrefix: false,
123 })
124 } else if (component === 'global' && utility === '.xt-container-y') {
125 const css = {}
126 Object.keys(options[utility]).forEach(name => {
127 const value = options[utility][name]
128 if (name === 'DEFAULT') {
129 css[utility] = {
130 paddingTop: value,
131 paddingBottom: value,
132 }
133 } else {
134 css[utility] = {
135 ...css[utility],
136 [`@screen ${name}`]: {
137 paddingTop: value,
138 paddingBottom: value,
139 },
140 }
141 }
142 })
143 addComponents(css, {
144 variants: variants,
145 respectPrefix: false,
146 })
147 } else if (component === 'global' && utility === '.xt-container-remove') {
148 const css = {}
149 Object.keys(options[utility]).forEach(name => {
150 const value = options[utility][name]
151 if (name === 'DEFAULT') {
152 css[utility] = {
153 marginLeft: `-${value}`,
154 marginRight: `-${value}`,
155 }
156 } else {
157 css[utility] = {
158 ...css[utility],
159 [`@screen ${name}`]: {
160 marginLeft: `-${value}`,
161 marginRight: `-${value}`,
162 },
163 }
164 }
165 })
166 addComponents(css, {
167 variants: variants,
168 respectPrefix: false,
169 })
170 } else if (component === 'global' && utility === '.xt-container-y-remove') {
171 const css = {}
172 Object.keys(options[utility]).forEach(name => {
173 const value = options[utility][name]
174 if (name === 'DEFAULT') {
175 css[utility] = {
176 marginTop: `-${value}`,
177 marginBottom: `-${value}`,
178 }
179 } else {
180 css[utility] = {
181 ...css[utility],
182 [`@screen ${name}`]: {
183 marginTop: `-${value}`,
184 marginBottom: `-${value}`,
185 },
186 }
187 }
188 })
189 addComponents(css, {
190 variants: variants,
191 respectPrefix: false,
192 })
193 } else {
194 // all others
195 const css = {}
196 css[utility] = options[utility]
197 addComponents(css, {
198 variants: variants,
199 respectPrefix: false,
200 })
201 }
202 }
203 }
204 }
205 }
206
207 /**
208 * variant
209 */
210
211 addVariant('group-dir-before', '.group.dir-before &')
212 addVariant('group-dir-after', '.group.dir-after &')
213 addVariant('group-off-before', '.group.dir-before:not(.on):not(.in):not(.out) &')
214 addVariant('group-off-after', '.group.dir-after:not(.on):not(.in):not(.out) &')
215 addVariant('group-on-before', '.group.on.dir-before &')
216 addVariant('group-on-after', '.group.on.dir-after &')
217 addVariant('group-in-before', '.group.in.dir-before &')
218 addVariant('group-in-after', '.group.in.dir-after &')
219 addVariant('group-out-before', '.group.out.dir-before &')
220 addVariant('group-out-after', '.group.out.dir-after &')
221 addVariant('group-done-before', '.group.done.dir-before &')
222 addVariant('group-done-after', '.group.done.dir-after &')
223 addVariant('group-off', '.group:not(.on):not(.in):not(.out) &')
224 addVariant('group-on', '.group.on &')
225 addVariant('group-in', '.group.in &')
226 addVariant('group-out', '.group.out &')
227 addVariant('group-done', '.group.done &')
228 addVariant('group-active', '.group:active &')
229 addVariant('dir-before', '&.dir-before')
230 addVariant('dir-after', '&.dir-after')
231 addVariant('off', '&:not(.on):not(.in):not(.out)')
232 addVariant('on', '&.on')
233 addVariant('in', '&.in')
234 addVariant('out', '&.out')
235 addVariant('done', '&.done')
236 addVariant('valid-submit', '&.valid-submit')
237 addVariant('invalid-submit', '&.invalid-submit')
238 }
239})