@unocss/preset-mini 0.42.0 → 0.43.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -82,6 +82,54 @@ will generate:
82
82
  }
83
83
  ```
84
84
 
85
+ ### Theme
86
+ You can fully customize your theme property in your config, and UnoCSS will eventually deeply merge it to the default theme.
87
+
88
+ <!--eslint-skip-->
89
+
90
+ ```ts
91
+ presetMini({
92
+ theme: {
93
+ // ...
94
+ colors: {
95
+ 'veryCool': '#0000ff', // class="text-very-cool"
96
+ 'brand': {
97
+ 'primary': 'hsla(var(--hue, 217), 78%, 51%)', //class="bg-brand-primary"
98
+ }
99
+ },
100
+ }
101
+ })
102
+ ```
103
+
104
+ To consume the theme in rules:
105
+
106
+ ```ts
107
+ rules: [
108
+ [/^text-(.*)$/, ([, c], { theme }) => {
109
+ if (theme.colors[c])
110
+ return { color: theme.colors[c] }
111
+ }],
112
+ ]
113
+ ```
114
+
115
+ One exception is that UnoCSS gives full control of `breakpoints` to users. When a custom `breakpoints` is provided, the default will be overridden instead of merging. For example:
116
+
117
+ ```ts
118
+ presetMini({
119
+ theme: {
120
+ // ...
121
+ breakpoints: {
122
+ sm: '320px',
123
+ md: '640px',
124
+ },
125
+ },
126
+ })
127
+ ```
128
+
129
+ Right now, you can only use the `sm:` and `md:` breakpoint variants.
130
+
131
+ `verticalBreakpoints` is same as `breakpoints` but for vertical layout.
132
+
85
133
  ## License
86
134
 
87
135
  MIT License &copy; 2021-PRESENT [Anthony Fu](https://github.com/antfu)
@@ -96,233 +96,6 @@ const globalKeywords = [
96
96
  "unset"
97
97
  ];
98
98
 
99
- const cssColorFunctions = ["hsl", "hsla", "hwb", "lab", "lch", "oklab", "oklch", "rgb", "rgba"];
100
- const alphaPlaceholders = ["%alpha", "<alpha-value>"];
101
- const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map((v) => core.escapeRegExp(v)).join("|"));
102
- function hex2rgba(hex = "") {
103
- const color = parseHexColor(hex);
104
- if (color != null) {
105
- const { components, alpha } = color;
106
- if (alpha == null)
107
- return components;
108
- return [...components, alpha];
109
- }
110
- }
111
- function parseCssColor(str = "") {
112
- const color = parseColor$1(str);
113
- if (color == null || color === false)
114
- return;
115
- const { type: casedType, components, alpha } = color;
116
- const type = casedType.toLowerCase();
117
- if (components.length === 0)
118
- return;
119
- if (["rgba", "hsla"].includes(type) && alpha == null)
120
- return;
121
- if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
122
- return;
123
- return { type, components, alpha };
124
- }
125
- function colorOpacityToString(color) {
126
- const alpha = color.alpha ?? 1;
127
- return typeof alpha === "string" && alphaPlaceholders.includes(alpha) ? 1 : alpha;
128
- }
129
- function colorToString(color, alphaOverride) {
130
- if (typeof color === "string")
131
- return color.replace(alphaPlaceholdersRE, `${alphaOverride ?? 1}`);
132
- const { components } = color;
133
- let { alpha, type } = color;
134
- alpha = alphaOverride ?? alpha;
135
- type = type.toLowerCase();
136
- if (["hsla", "hsl", "rgba", "rgb"].includes(type))
137
- return `${type.replace("a", "")}a(${components.join(",")}${alpha == null ? "" : `,${alpha}`})`;
138
- alpha = alpha == null ? "" : ` / ${alpha}`;
139
- if (cssColorFunctions.includes(type))
140
- return `${type}(${components.join(" ")}${alpha})`;
141
- return `color(${type} ${components.join(" ")}${alpha})`;
142
- }
143
- function parseColor$1(str) {
144
- if (!str)
145
- return;
146
- let color = parseHexColor(str);
147
- if (color != null)
148
- return color;
149
- color = cssColorKeyword(str);
150
- if (color != null)
151
- return color;
152
- color = parseCssCommaColorFunction(str);
153
- if (color != null)
154
- return color;
155
- color = parseCssSpaceColorFunction(str);
156
- if (color != null)
157
- return color;
158
- color = parseCssColorFunction(str);
159
- if (color != null)
160
- return color;
161
- }
162
- function parseHexColor(str) {
163
- const [, body] = str.match(/^#([\da-f]+)$/i) || [];
164
- if (!body)
165
- return;
166
- switch (body.length) {
167
- case 3:
168
- case 4:
169
- const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
170
- return {
171
- type: "rgb",
172
- components: digits.slice(0, 3),
173
- alpha: body.length === 3 ? void 0 : Math.round(digits[3] / 255 * 100) / 100
174
- };
175
- case 6:
176
- case 8:
177
- const value = Number.parseInt(body, 16);
178
- return {
179
- type: "rgb",
180
- components: body.length === 6 ? [value >> 16 & 255, value >> 8 & 255, value & 255] : [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255],
181
- alpha: body.length === 6 ? void 0 : Math.round((value & 255) / 255 * 100) / 100
182
- };
183
- }
184
- }
185
- function cssColorKeyword(str) {
186
- const color = {
187
- rebeccapurple: [102, 51, 153, 1]
188
- }[str];
189
- if (color != null) {
190
- return {
191
- type: "rgb",
192
- components: color.slice(0, 3),
193
- alpha: color[3]
194
- };
195
- }
196
- }
197
- function parseCssCommaColorFunction(color) {
198
- const match = color.match(/^(rgb|rgba|hsl|hsla)\((.+)\)$/i);
199
- if (!match)
200
- return;
201
- const [, type, componentString] = match;
202
- const components = getComponents(componentString, ",", 5);
203
- if (components) {
204
- if ([3, 4].includes(components.length)) {
205
- return {
206
- type,
207
- components: components.slice(0, 3),
208
- alpha: components[3]
209
- };
210
- } else if (components.length !== 1) {
211
- return false;
212
- }
213
- }
214
- }
215
- const cssColorFunctionsRe = new RegExp(`^(${cssColorFunctions.join("|")})\\((.+)\\)$`, "i");
216
- function parseCssSpaceColorFunction(color) {
217
- const match = color.match(cssColorFunctionsRe);
218
- if (!match)
219
- return;
220
- const [, fn, componentString] = match;
221
- const parsed = parseCssSpaceColorValues(`${fn} ${componentString}`);
222
- if (parsed) {
223
- const { alpha, components: [type, ...components] } = parsed;
224
- return {
225
- type,
226
- components,
227
- alpha
228
- };
229
- }
230
- }
231
- function parseCssColorFunction(color) {
232
- const match = color.match(/^color\((.+)\)$/);
233
- if (!match)
234
- return;
235
- const parsed = parseCssSpaceColorValues(match[1]);
236
- if (parsed) {
237
- const { alpha, components: [type, ...components] } = parsed;
238
- return {
239
- type,
240
- components,
241
- alpha
242
- };
243
- }
244
- }
245
- function parseCssSpaceColorValues(componentString) {
246
- const components = getComponents(componentString);
247
- if (!components)
248
- return;
249
- let totalComponents = components.length;
250
- if (components[totalComponents - 2] === "/") {
251
- return {
252
- components: components.slice(0, totalComponents - 2),
253
- alpha: components[totalComponents - 1]
254
- };
255
- }
256
- if (components[totalComponents - 2] != null && (components[totalComponents - 2].endsWith("/") || components[totalComponents - 1].startsWith("/"))) {
257
- const removed = components.splice(totalComponents - 2);
258
- components.push(removed.join(" "));
259
- --totalComponents;
260
- }
261
- const withAlpha = getComponents(components[totalComponents - 1], "/", 3);
262
- if (!withAlpha)
263
- return;
264
- if (withAlpha.length === 1 || withAlpha[withAlpha.length - 1] === "")
265
- return { components };
266
- const alpha = withAlpha.pop();
267
- components[totalComponents - 1] = withAlpha.join("/");
268
- return {
269
- components,
270
- alpha
271
- };
272
- }
273
- function getComponent(str, separator) {
274
- str = str.trim();
275
- if (str === "")
276
- return;
277
- const l = str.length;
278
- let parenthesis = 0;
279
- for (let i = 0; i < l; i++) {
280
- switch (str[i]) {
281
- case "(":
282
- parenthesis++;
283
- break;
284
- case ")":
285
- if (--parenthesis < 0)
286
- return;
287
- break;
288
- case separator:
289
- if (parenthesis === 0) {
290
- const component = str.slice(0, i).trim();
291
- if (component === "")
292
- return;
293
- return [
294
- component,
295
- str.slice(i + 1).trim()
296
- ];
297
- }
298
- }
299
- }
300
- return [
301
- str,
302
- ""
303
- ];
304
- }
305
- function getComponents(str, separator, limit) {
306
- separator = separator ?? " ";
307
- if (separator.length !== 1)
308
- return;
309
- limit = limit ?? 10;
310
- const components = [];
311
- let i = 0;
312
- while (str !== "") {
313
- if (++i > limit)
314
- return;
315
- const componentPair = getComponent(str, separator);
316
- if (!componentPair)
317
- return;
318
- const [component, rest] = componentPair;
319
- components.push(component);
320
- str = rest;
321
- }
322
- if (components.length > 0)
323
- return components;
324
- }
325
-
326
99
  const cssProps = [
327
100
  "color",
328
101
  "border-color",
@@ -533,7 +306,7 @@ const directionSize = (propertyPrefix) => ([_, direction, size], { theme }) => {
533
306
  return directionMap[direction].map((i) => [`${propertyPrefix}${i}`, v]);
534
307
  };
535
308
  const getThemeColor = (theme, colors) => theme.colors?.[colors.join("-").replace(/(-[a-z])/g, (n) => n.slice(1).toUpperCase())];
536
- const parseColor = (body, theme) => {
309
+ const parseColor$1 = (body, theme) => {
537
310
  const split = body.split(/(?:\/|:)/);
538
311
  let main, opacity;
539
312
  if (split[0] === "[color") {
@@ -585,7 +358,7 @@ const parseColor = (body, theme) => {
585
358
  };
586
359
  };
587
360
  const colorResolver = (property, varName) => ([, body], { theme }) => {
588
- const data = parseColor(body, theme);
361
+ const data = parseColor$1(body, theme);
589
362
  if (!data)
590
363
  return;
591
364
  const { alpha, color, cssColor } = data;
@@ -621,7 +394,7 @@ const colorableShadows = (shadows, colorVar) => {
621
394
  return colored;
622
395
  };
623
396
  const hasParseableColor = (color, theme) => {
624
- return color != null && !!parseColor(color, theme)?.color;
397
+ return color != null && !!parseColor$1(color, theme)?.color;
625
398
  };
626
399
  const resolveBreakpoints = ({ theme, generator }) => {
627
400
  let breakpoints;
@@ -642,6 +415,234 @@ const resolveVerticalBreakpoints = ({ theme, generator }) => {
642
415
  const makeGlobalStaticRules = (prefix, property) => {
643
416
  return globalKeywords.map((keyword) => [`${prefix}-${keyword}`, { [property ?? prefix]: keyword }]);
644
417
  };
418
+ function getComponent(str, open, close, separator) {
419
+ if (str === "")
420
+ return;
421
+ const l = str.length;
422
+ let parenthesis = 0;
423
+ for (let i = 0; i < l; i++) {
424
+ switch (str[i]) {
425
+ case open:
426
+ parenthesis++;
427
+ break;
428
+ case close:
429
+ if (--parenthesis < 0)
430
+ return;
431
+ break;
432
+ case separator:
433
+ if (parenthesis === 0) {
434
+ if (i === 0 || i === l - 1)
435
+ return;
436
+ return [
437
+ str.slice(0, i),
438
+ str.slice(i + 1)
439
+ ];
440
+ }
441
+ }
442
+ }
443
+ return [
444
+ str,
445
+ ""
446
+ ];
447
+ }
448
+ function getComponents(str, separator, limit) {
449
+ if (separator.length !== 1)
450
+ return;
451
+ limit = limit ?? 10;
452
+ const components = [];
453
+ let i = 0;
454
+ while (str !== "") {
455
+ if (++i > limit)
456
+ return;
457
+ const componentPair = getComponent(str, "(", ")", separator);
458
+ if (!componentPair)
459
+ return;
460
+ const [component, rest] = componentPair;
461
+ components.push(component);
462
+ str = rest;
463
+ }
464
+ if (components.length > 0)
465
+ return components;
466
+ }
467
+
468
+ const cssColorFunctions = ["hsl", "hsla", "hwb", "lab", "lch", "oklab", "oklch", "rgb", "rgba"];
469
+ const alphaPlaceholders = ["%alpha", "<alpha-value>"];
470
+ const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map((v) => core.escapeRegExp(v)).join("|"));
471
+ function hex2rgba(hex = "") {
472
+ const color = parseHexColor(hex);
473
+ if (color != null) {
474
+ const { components, alpha } = color;
475
+ if (alpha == null)
476
+ return components;
477
+ return [...components, alpha];
478
+ }
479
+ }
480
+ function parseCssColor(str = "") {
481
+ const color = parseColor(str);
482
+ if (color == null || color === false)
483
+ return;
484
+ const { type: casedType, components, alpha } = color;
485
+ const type = casedType.toLowerCase();
486
+ if (components.length === 0)
487
+ return;
488
+ if (["rgba", "hsla"].includes(type) && alpha == null)
489
+ return;
490
+ if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
491
+ return;
492
+ return {
493
+ type,
494
+ components: components.map((c) => typeof c === "string" ? c.trim() : c),
495
+ alpha: typeof alpha === "string" ? alpha.trim() : alpha
496
+ };
497
+ }
498
+ function colorOpacityToString(color) {
499
+ const alpha = color.alpha ?? 1;
500
+ return typeof alpha === "string" && alphaPlaceholders.includes(alpha) ? 1 : alpha;
501
+ }
502
+ function colorToString(color, alphaOverride) {
503
+ if (typeof color === "string")
504
+ return color.replace(alphaPlaceholdersRE, `${alphaOverride ?? 1}`);
505
+ const { components } = color;
506
+ let { alpha, type } = color;
507
+ alpha = alphaOverride ?? alpha;
508
+ type = type.toLowerCase();
509
+ if (["hsla", "hsl", "rgba", "rgb"].includes(type))
510
+ return `${type.replace("a", "")}a(${components.join(",")}${alpha == null ? "" : `,${alpha}`})`;
511
+ alpha = alpha == null ? "" : ` / ${alpha}`;
512
+ if (cssColorFunctions.includes(type))
513
+ return `${type}(${components.join(" ")}${alpha})`;
514
+ return `color(${type} ${components.join(" ")}${alpha})`;
515
+ }
516
+ function parseColor(str) {
517
+ if (!str)
518
+ return;
519
+ let color = parseHexColor(str);
520
+ if (color != null)
521
+ return color;
522
+ color = cssColorKeyword(str);
523
+ if (color != null)
524
+ return color;
525
+ color = parseCssCommaColorFunction(str);
526
+ if (color != null)
527
+ return color;
528
+ color = parseCssSpaceColorFunction(str);
529
+ if (color != null)
530
+ return color;
531
+ color = parseCssColorFunction(str);
532
+ if (color != null)
533
+ return color;
534
+ }
535
+ function parseHexColor(str) {
536
+ const [, body] = str.match(/^#([\da-f]+)$/i) || [];
537
+ if (!body)
538
+ return;
539
+ switch (body.length) {
540
+ case 3:
541
+ case 4:
542
+ const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
543
+ return {
544
+ type: "rgb",
545
+ components: digits.slice(0, 3),
546
+ alpha: body.length === 3 ? void 0 : Math.round(digits[3] / 255 * 100) / 100
547
+ };
548
+ case 6:
549
+ case 8:
550
+ const value = Number.parseInt(body, 16);
551
+ return {
552
+ type: "rgb",
553
+ components: body.length === 6 ? [value >> 16 & 255, value >> 8 & 255, value & 255] : [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255],
554
+ alpha: body.length === 6 ? void 0 : Math.round((value & 255) / 255 * 100) / 100
555
+ };
556
+ }
557
+ }
558
+ function cssColorKeyword(str) {
559
+ const color = {
560
+ rebeccapurple: [102, 51, 153, 1]
561
+ }[str];
562
+ if (color != null) {
563
+ return {
564
+ type: "rgb",
565
+ components: color.slice(0, 3),
566
+ alpha: color[3]
567
+ };
568
+ }
569
+ }
570
+ function parseCssCommaColorFunction(color) {
571
+ const match = color.match(/^(rgb|rgba|hsl|hsla)\((.+)\)$/i);
572
+ if (!match)
573
+ return;
574
+ const [, type, componentString] = match;
575
+ const components = getComponents(componentString, ",", 5);
576
+ if (components) {
577
+ if ([3, 4].includes(components.length)) {
578
+ return {
579
+ type,
580
+ components: components.slice(0, 3),
581
+ alpha: components[3]
582
+ };
583
+ } else if (components.length !== 1) {
584
+ return false;
585
+ }
586
+ }
587
+ }
588
+ const cssColorFunctionsRe = new RegExp(`^(${cssColorFunctions.join("|")})\\((.+)\\)$`, "i");
589
+ function parseCssSpaceColorFunction(color) {
590
+ const match = color.match(cssColorFunctionsRe);
591
+ if (!match)
592
+ return;
593
+ const [, fn, componentString] = match;
594
+ const parsed = parseCssSpaceColorValues(`${fn} ${componentString}`);
595
+ if (parsed) {
596
+ const { alpha, components: [type, ...components] } = parsed;
597
+ return {
598
+ type,
599
+ components,
600
+ alpha
601
+ };
602
+ }
603
+ }
604
+ function parseCssColorFunction(color) {
605
+ const match = color.match(/^color\((.+)\)$/);
606
+ if (!match)
607
+ return;
608
+ const parsed = parseCssSpaceColorValues(match[1]);
609
+ if (parsed) {
610
+ const { alpha, components: [type, ...components] } = parsed;
611
+ return {
612
+ type,
613
+ components,
614
+ alpha
615
+ };
616
+ }
617
+ }
618
+ function parseCssSpaceColorValues(componentString) {
619
+ const components = getComponents(componentString, " ");
620
+ if (!components)
621
+ return;
622
+ let totalComponents = components.length;
623
+ if (components[totalComponents - 2] === "/") {
624
+ return {
625
+ components: components.slice(0, totalComponents - 2),
626
+ alpha: components[totalComponents - 1]
627
+ };
628
+ }
629
+ if (components[totalComponents - 2] != null && (components[totalComponents - 2].endsWith("/") || components[totalComponents - 1].startsWith("/"))) {
630
+ const removed = components.splice(totalComponents - 2);
631
+ components.push(removed.join(" "));
632
+ --totalComponents;
633
+ }
634
+ const withAlpha = getComponents(components[totalComponents - 1], "/", 2);
635
+ if (!withAlpha)
636
+ return;
637
+ if (withAlpha.length === 1 || withAlpha[withAlpha.length - 1] === "")
638
+ return { components };
639
+ const alpha = withAlpha.pop();
640
+ components[totalComponents - 1] = withAlpha.join("/");
641
+ return {
642
+ components,
643
+ alpha
644
+ };
645
+ }
645
646
 
646
647
  exports.CONTROL_MINI_NO_NEGATIVE = CONTROL_MINI_NO_NEGATIVE;
647
648
  exports.colorOpacityToString = colorOpacityToString;
@@ -651,6 +652,7 @@ exports.colorableShadows = colorableShadows;
651
652
  exports.cornerMap = cornerMap;
652
653
  exports.directionMap = directionMap;
653
654
  exports.directionSize = directionSize;
655
+ exports.getComponent = getComponent;
654
656
  exports.getComponents = getComponents;
655
657
  exports.globalKeywords = globalKeywords;
656
658
  exports.h = h;
@@ -659,7 +661,7 @@ exports.hasParseableColor = hasParseableColor;
659
661
  exports.hex2rgba = hex2rgba;
660
662
  exports.insetMap = insetMap;
661
663
  exports.makeGlobalStaticRules = makeGlobalStaticRules;
662
- exports.parseColor = parseColor;
664
+ exports.parseColor = parseColor$1;
663
665
  exports.parseCssColor = parseCssColor;
664
666
  exports.positionMap = positionMap;
665
667
  exports.resolveBreakpoints = resolveBreakpoints;