-
Notifications
You must be signed in to change notification settings - Fork 7
/
unocss.config.ts
276 lines (243 loc) · 6.84 KB
/
unocss.config.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { defineConfig, toEscapedSelector as e } from 'unocss';
import acss from './acss';
const setProperty = (property: string = ''): string => {
const removeEndDash = property.replace(/\-$/, '');
return acss[removeEndDash] || removeEndDash;
};
const setUnits = (cssProperty: string, unit: string): string => {
if (!cssProperty || !unit) return '';
/**
* @description
* Some of CSS properties are just numbers, and there is no unit requirement (like rem,px,...)
* like opacity, font-weight, etc.
*/
if (['opacity', 'font-weight', 'line-height'].includes(cssProperty)) return unit;
/**
* @description
* When other CSS properties are just numbers (float or integer), the default units are rem.
*/
if (/^[0-9\.\-]+$/.test(unit)) {
return `${unit}rem`;
}
/**
* @description
* The CSS content value must be a string, and it must be converted to a string with quotation marks
*/
if (cssProperty === 'cnt' || cssProperty === 'content') {
return `"${unit}"`;
}
return unit;
};
const cssPseudoClassesList = [
'active:',
'hover:',
'focus:',
'visited:',
'focus-within:',
'focus-visible:',
'target:',
'link:',
'first:',
'first-child:',
'checked:',
'disabled:',
'empty:',
'enabled:',
'first-of-type:',
'in-range:',
'invalid:',
'last:',
'last-child:',
'last-of-type:',
'optional:',
'out-of-range:',
'read-only:',
'read-write:',
'required:',
'valid:',
'odd:',
'even:',
];
const cssPseudoElementsList = ['before:', 'after:', 'first-letter:', 'first-line:', 'placeholder:'];
export default defineConfig({
shortcuts: [],
variants: [
/**
* @description
* 1) In classNames, spaces cannot be used (because a new class has already been defined).
* Therefore, the underscore(_) sign should be used instead of a space.
* We will now replace the underscore(_) sign with a space.
* 2) Add body wrapping to classNames (for CSS Specification)
* {@link https://www.w3schools.com/css/css_specificity.asp}
*/
(matcher) => {
return {
matcher: matcher.replace(/_/g, ' '),
selector: (s) => {
return `body ${s}`;
},
};
},
/**
* @description Group Selectors
* @rule
* - Group Selectors must start with @
* - All selectors must end with dollar sign ($)
* - The classname can only contain one dollar sign ($)
*
* @example
* space between
* 👆
* 1) @.wrapper_p$fs[16px] => .wrapper p{font-size: 16px;}
* 👇 👇
* 👇 end group selectors
* 👇
* start group selectors
*
*
* 2) @#wrapper_button$hover:bg[green] => .wrapper button:hover{background: green;}
*
* 3) @form_input$bd[1px_solid_red] => form input{border: 1px solid red;}
*/
(matcher) => {
if (!/^@[\w\s\[\]\-:.#$]+$/gi.test(matcher)) return matcher;
if (!matcher.includes('$')) return matcher;
const selectors = matcher.split('$');
if (!selectors || selectors.length !== 2) return matcher;
return {
/**
* Slice `selectors` and passed to the next variants and rules
*
* @example
* @#wrapper_button$hover:bg[green] => hover:bg[green]
* @form_input$bd[1px_solid_red] => bd[1px_solid_red]
*/
matcher: selectors[1],
layer: 'Group Selectors',
selector: () => {
/**
* Remove first @ for the selector
*/
return selectors[0].slice(1);
},
};
},
/**
* @description Pseudo classes/elements Selectors
* @example
* 1) hover:bg[red] => .hover\:bg\[red\]:hover{background: red;}
* 2) after:hover:c[red] => .after\:hover\:c\[red\]:hover::after{color: red;}
* 3) hover:after:c[red] => .after\:hover\:c\[red\]:hover::after{color: red;}
*/
(matcher, { rawSelector }) => {
const getCssPseudoClasses = cssPseudoClassesList.find((pseudoClass) => {
return matcher.includes(pseudoClass);
});
const getCssPseudoElements = cssPseudoElementsList.find((pseudoElements) => {
return matcher.includes(pseudoElements);
});
const layer = 'Pseudo classes/elements Selectors';
// For Both pseudo classes and pseudo elements
if (getCssPseudoClasses && getCssPseudoElements) {
return {
matcher: matcher.slice(getCssPseudoClasses.length + getCssPseudoElements.length),
layer,
selector: (s) => {
return `${s}:${getCssPseudoClasses.slice(0, -1)}::${getCssPseudoElements.slice(0, -1)}`;
},
};
}
// For pseudo classes
if (getCssPseudoClasses) {
if (getCssPseudoClasses === 'odd:') {
return {
matcher: matcher.slice(getCssPseudoClasses.length),
layer,
selector: (s) => `${s}:nth-child(odd)`,
};
}
if (getCssPseudoClasses === 'even:') {
return {
matcher: matcher.slice(getCssPseudoClasses.length),
layer,
selector: (s) => `${s}:nth-child(even)`,
};
}
return {
matcher: matcher.slice(getCssPseudoClasses.length),
layer,
selector: (s) => `${s}:${getCssPseudoClasses.slice(0, -1)}`,
};
}
// For pseudo elements
if (getCssPseudoElements) {
return {
matcher: matcher.slice(getCssPseudoElements.length),
layer,
selector: (s) => `${s}::${getCssPseudoElements.slice(0, -1)}`,
};
}
// default
return matcher;
},
],
rules: [
/**
* @description Defining CSS variables
* @example
* 1) root:[--primary:#0d6efd] => root:{--primary:#0d6efd}
* 2) root:[--font-size:16px] => root:{--font-size:16px}
* 3) local-var[--green:#198754] => local-var{--green:#198754}
*/
[
/^([a-zA-Z\-\:]+)\[(\-\-.+):(.+)\]$/,
([all, selector, varName, varValue], { rawSelector }) => {
const cssSelector = selector === 'root:' ? ':root' : e(rawSelector);
return `
${cssSelector} {
${varName}: ${varValue};
}
`.replace(/[\s]+/gi, '');
},
{ layer: 'CSS Variables' },
],
/**
* @description For all css properties
* property: @see getProperty() function
* units: 10px, 10rem, 10%, 10vh, auto & etc.
* @example
* m-[1rem_auto] ==> margin: 10px auto;
* mr-[10px] ==> margin-right: 10px
* pl-[auto] ==> margin-left: auto
*/
[
/^([a-zA-Z\-]+)\[(.+)\]$/,
([all, property, units], { rawSelector }) => {
// escape the css variable
if (units.startsWith('--')) return {};
const cssProperty = setProperty(property);
const cssUnit = setUnits(cssProperty, units);
if (!cssProperty || !cssUnit) return {};
// console.log({ cssProperty, cssUnit });
/**
* For margin-x & margin-y & padding-x & padding-y
*/
if (/^(padding|margin)-(x|y)$/gi.test(cssProperty)) {
const [type, dir] = cssProperty.split('-');
const direction = {
x: ['left', 'right'],
y: ['top', 'bottom'],
};
return {
[`${type}-${direction[dir][0]}`]: cssUnit,
[`${type}-${direction[dir][1]}`]: cssUnit,
};
}
return {
[cssProperty]: `${cssUnit} !important;`,
};
},
],
],
presets: [],
});