vunor 0.5.1 → 0.5.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.2
4
+
5
+ ### Fixed
6
+
7
+ - **One variant of a shortcut can be cleared or replaced.** In `mergeVunorShortcuts` (and with it `vunorShortcuts()`) a later entry reaches each variant of an earlier shortcut of the same name, nested keys included:
8
+ - `''` or `[]` at a key drops every earlier class under that key path: `vunorShortcuts(defineShortcuts({ 'c8-filled': { 'dark:': '' } }))` drops the dark-mode rim and keeps every other variant (it dropped nothing);
9
+ - `replaceShortcut(value)` works on a variant key too: it drops the earlier classes under the key and adds its own, `{ 'c8-filled': { [whenEnabled]: { 'active:': replaceShortcut('current-bg-scope-color-900') } } }` (it had no effect there);
10
+ - classes are appended after the earlier ones, as before; the generated shortcuts of an app that only appends do not change.
11
+
12
+ A key path covers nested variants too (`'dark:': ''` also drops `dark:hover:…`), and a map composed with `mergeVunorShortcuts` keeps the clear or replacement: see [Clearing or Replacing One Variant](./README.md#clearing-or-replacing-one-variant).
13
+
14
+ - `toUnoShortcut` splits a value on any whitespace and skips empty values: a newline between classes no longer leaves the next class without its variant prefix, and an empty value or a double space no longer emits the bare prefix. An `undefined` value adds nothing (it threw in `toUnoShortcut` and removed the key in a merge).
15
+
3
16
  ## 0.5.1
4
17
 
5
18
  ### Changed (visual)
package/README.md CHANGED
@@ -268,7 +268,7 @@ export default defineConfig({
268
268
 
269
269
  Your `'c8-filled'.'':` classes are added to the base classes of `c8-filled`, while all its `hover:`, `dark:`, `active:` variants remain intact from the defaults. Everything flows through a single merge pipeline, producing consistent CSS passed to UnoCSS.
270
270
 
271
- Appending is not always overriding. UnoCSS writes the utilities of a shortcut in the order of its own rules, not in the order of the class string. An appended class wins over an earlier class of the same utility (`rounded-full` over `rounded-base`, one `bg-*` over another). Between different utilities the UnoCSS rule order decides: `px-4` wins over `p-2` and `flex` over `block`, whichever is written last. When an appended class has no effect, append the utility that wins (`px-2` to change a `px-4`), mark your class important (`!p-2`), or [replace the shortcut](#replacing-a-built-in-shortcut).
271
+ Appending is not always overriding. UnoCSS writes the utilities of a shortcut in the order of its own rules, not in the order of the class string. An appended class wins over an earlier class of the same utility (`rounded-full` over `rounded-base`, one `bg-*` over another). Between different utilities the UnoCSS rule order decides: `px-4` wins over `p-2` and `flex` over `block`, whichever is written last. When an appended class has no effect, append the utility that wins (`px-2` to change a `px-4`), mark your class important (`!p-2`), or replace [the variant](#clearing-or-replacing-one-variant) or [the whole shortcut](#replacing-a-built-in-shortcut).
272
272
 
273
273
  `vunorShortcuts()` returns the merged default shortcuts for all built-in styles (c8, i8, card, dialog, etc.). Pass your overrides as the first argument -- they take priority.
274
274
 
@@ -292,12 +292,41 @@ export default defineConfig({
292
292
  })
293
293
  ```
294
294
 
295
- `replaceShortcut` takes any form a normal entry takes (a string, an array or a shortcut object). Put it on the shortcut name, not on a variant key inside a shortcut (there it has no effect). In `mergeVunorShortcuts([a, b, c])` a marked entry drops the definitions of its name from the earlier lists, and entries in later lists merge on top of it as usual. The mark survives the merge, so `vunorShortcuts(mergeVunorShortcuts([x, y]))` still replaces the built-in shortcut when `y` marks it.
295
+ `replaceShortcut` takes any form a normal entry takes (a string, an array or a shortcut object). On a shortcut name it replaces the whole shortcut; on a variant key, only that variant ([below](#clearing-or-replacing-one-variant)). In `mergeVunorShortcuts([a, b, c])` a marked entry drops the definitions of its name from the earlier lists, and entries in later lists merge on top of it as usual. The mark survives the merge, so `vunorShortcuts(mergeVunorShortcuts([x, y]))` still replaces the built-in shortcut when `y` marks it.
296
296
 
297
297
  Without vunor's merge, plain UnoCSS does the same: a shortcut in a later map of the `shortcuts` array replaces an earlier one of the same name, so `shortcuts: [vunorShortcuts(), { press: 'active:scale-95' }]` also drops vunor's `press`.
298
298
 
299
299
  An app shortcut with the same name as a built-in one is merged with it unless it is marked: when a vunor release adds a shortcut whose name your app already uses, mark your app's one with `replaceShortcut` (or rename it).
300
300
 
301
+ ### Clearing or Replacing One Variant
302
+
303
+ Since 0.5.2. A later entry can clear or replace one variant of a shortcut and keep the rest:
304
+
305
+ | Value at a key | Effect |
306
+ | --------------------------------- | ------------------------------------------- |
307
+ | classes (string, array or object) | appended after the earlier classes |
308
+ | `''` or `[]` | drops every earlier class under the key |
309
+ | `replaceShortcut(value)` | drops them, then adds `value` under the key |
310
+
311
+ ```ts
312
+ import { defineShortcuts, replaceShortcut, vunorShortcuts, whenEnabled } from 'vunor/theme'
313
+
314
+ vunorShortcuts(
315
+ defineShortcuts({
316
+ 'c8-filled': {
317
+ // no dark-mode rim; every other variant of c8-filled stays
318
+ 'dark:': '',
319
+ // pressed: only this fill, vunor's `c8-filled-active` is dropped
320
+ [whenEnabled]: { 'active:': replaceShortcut('current-bg-scope-color-900') },
321
+ },
322
+ })
323
+ )
324
+ ```
325
+
326
+ - A key path covers every class whose variants start with it: `'dark:': ''` also drops `dark:hover:…` classes. It matches the earlier classes however they are written, under nested keys or as `dark:…` in a string.
327
+ - The shortcut name is a key too: `{ press: '' }` leaves `press` with no classes.
328
+ - Entries in later lists append after a cleared or replaced key, and the clear or replacement survives a merge: `vunorShortcuts(mergeVunorShortcuts([x, y]))` gives the same result as merging `x` and `y` onto the built-ins one after the other.
329
+
301
330
  ---
302
331
 
303
332
  ## Semantic CSS Classes
@@ -379,7 +408,7 @@ vunorShortcuts(
379
408
  )
380
409
  ```
381
410
 
382
- Add `'dark:': 'ring-0'` to the `c8-filled` entry to drop the dark-mode rim too. With the default palette, white text on these fills measures 3.4–4.1:1 at rest and 2.1–3.0:1 on the dark-mode hover and press fills, below WCAG AA.
411
+ Add `'dark:': ''` to the `c8-filled` entry to drop the dark-mode rim too. With the default palette, white text on these fills measures 3.4–4.1:1 at rest and 2.1–3.0:1 on the dark-mode hover and press fills, below WCAG AA.
383
412
 
384
413
  To change a c8 hover colour, override the same key vunor uses, so your class merges into vunor's rule and wins:
385
414
 
package/dist/theme.d.mts CHANGED
@@ -383,21 +383,24 @@ declare const rawVunorShortcuts: TVunorShortcut[];
383
383
  */
384
384
  declare function toUnoShortcut(obj: TVunorShortcut): string;
385
385
  /**
386
- * Marks a top-level shortcut entry as a replacement: when shortcuts are merged
387
- * (`mergeVunorShortcuts`, `vunorShortcuts`), it drops every earlier definition of
388
- * the same name instead of merging into it. Later entries still merge on top of it.
386
+ * Marks a shortcut value as a replacement: when shortcuts are merged
387
+ * (`mergeVunorShortcuts`, `vunorShortcuts`), it drops the earlier classes under
388
+ * its key (on a shortcut name the whole shortcut, on a variant key only that
389
+ * variant) instead of merging into them. Later entries still merge on top of it.
389
390
  *
390
391
  * @example
391
392
  * vunorShortcuts(defineShortcuts({ press: replaceShortcut({ 'active:': 'scale-95' }) }))
393
+ * vunorShortcuts(defineShortcuts({ 'c8-filled': { 'dark:': replaceShortcut('ring-2') } }))
392
394
  *
393
395
  * @param value the shortcut, in any form a normal entry accepts
394
396
  * @returns the shortcut's class string in a marked array
395
397
  */
396
398
  declare const replaceShortcut: (value: string | string[] | TVunorShortcut) => string[];
397
399
  /**
398
- * Merges an array of vunor shortcuts
399
- * Items from the beginning have less priority. An entry marked with
400
- * `replaceShortcut` drops the earlier entries of its name.
400
+ * Merges an array of vunor shortcut maps; later maps win. A later value's classes
401
+ * are appended after the earlier ones of its name. At any key (the name or a
402
+ * variant), `""` or `[]` drops the earlier classes under that key path, nested
403
+ * variants included; a `replaceShortcut` value drops them and adds its own.
401
404
  *
402
405
  * @param shortcuts TVunorShortcut[]
403
406
  * @returns TVunorShortcut
package/dist/theme.mjs CHANGED
@@ -521,72 +521,75 @@ const rawVunorShortcuts = [
521
521
  * @returns string
522
522
  */
523
523
  function toUnoShortcut(obj) {
524
- let s = "";
525
- for (const [key, val] of Object.entries(obj)) {
526
- const a = prepareScArray(val);
527
- s += `${a.map((v) => `${key}${v}`).join(" ")} `;
528
- }
529
- return s.trim();
524
+ return prepareScArray(obj).join(" ");
530
525
  }
531
- function prepareScArray(input) {
532
- if (typeof input === "string") return input.split(" ");
533
- if (Array.isArray(input)) return input.flatMap((item) => item.split(" "));
534
- const a = [];
535
- for (const [key, val] of Object.entries(input)) a.push(...prepareScArray(val).map((v) => `${key}${v}`));
536
- return a;
526
+ /** The class tokens of a value, each prefixed with `prefix` and the variant keys above it */
527
+ function prepareScArray(input, prefix = "") {
528
+ if (typeof input === "object" && !Array.isArray(input)) return Object.entries(input).flatMap(([key, val]) => prepareScArray(val, `${prefix}${key}`));
529
+ return toClassString(input).split(/\s+/).filter(Boolean).map((c) => `${prefix}${c}`);
537
530
  }
538
531
  const REPLACE = Symbol.for("vunor.replaceShortcut");
539
532
  /** A shortcut value as one UnoCSS class string */
540
533
  const toClassString = (value = "") => typeof value === "string" ? value : Array.isArray(value) ? value.join(" ") : toUnoShortcut(value);
541
534
  const markReplacement = (classes) => Object.defineProperty(classes, REPLACE, { value: true });
542
535
  const isReplacement = (value) => Array.isArray(value) && REPLACE in value;
536
+ /** `""`, `[]` or a `replaceShortcut` value: it drops the earlier classes under its key */
537
+ const clearsKey = (value) => isReplacement(value) || (typeof value === "string" || Array.isArray(value)) && prepareScArray(value).length === 0;
543
538
  /**
544
- * Marks a top-level shortcut entry as a replacement: when shortcuts are merged
545
- * (`mergeVunorShortcuts`, `vunorShortcuts`), it drops every earlier definition of
546
- * the same name instead of merging into it. Later entries still merge on top of it.
539
+ * Marks a shortcut value as a replacement: when shortcuts are merged
540
+ * (`mergeVunorShortcuts`, `vunorShortcuts`), it drops the earlier classes under
541
+ * its key (on a shortcut name the whole shortcut, on a variant key only that
542
+ * variant) instead of merging into them. Later entries still merge on top of it.
547
543
  *
548
544
  * @example
549
545
  * vunorShortcuts(defineShortcuts({ press: replaceShortcut({ 'active:': 'scale-95' }) }))
546
+ * vunorShortcuts(defineShortcuts({ 'c8-filled': { 'dark:': replaceShortcut('ring-2') } }))
550
547
  *
551
548
  * @param value the shortcut, in any form a normal entry accepts
552
549
  * @returns the shortcut's class string in a marked array
553
550
  */
554
551
  const replaceShortcut = (value) => markReplacement([toClassString(value)]);
555
552
  /**
556
- * Merges source vunor shortcut with target vunor shortcut
557
- * The target vunor shortcut has more power
558
- *
559
- * @param target TVunorShortcut
560
- * @param source TVunorShortcut
561
- * @returns TVunorShortcut
553
+ * Merged classes as a shortcut value. The cleared key paths stay in it as `''`
554
+ * entries, so they clear again when the result is merged onto an earlier shortcut.
562
555
  */
556
+ function toValue(classes, cleared) {
557
+ if (cleared.has("")) return markReplacement([classes.join(" ")]);
558
+ if (cleared.size === 0) return classes.join(" ");
559
+ const value = Object.fromEntries([...cleared].map((prefix) => [prefix, ""]));
560
+ if (classes.length > 0) value[""] = classes.join(" ");
561
+ return value;
562
+ }
563
+ /** Merges a later value (source) into an earlier one (target) of the same name */
564
+ function mergeValue(target, source) {
565
+ if (source === void 0) return target;
566
+ if (target === void 0 || clearsKey(source)) return source;
567
+ let classes = [];
568
+ const cleared = /* @__PURE__ */ new Set();
569
+ const apply = (value, prefix) => {
570
+ if (clearsKey(value)) {
571
+ classes = classes.filter((c) => !c.startsWith(prefix));
572
+ cleared.add(prefix);
573
+ }
574
+ if (typeof value === "object" && !Array.isArray(value)) for (const [key, val] of Object.entries(value)) apply(val, `${prefix}${key}`);
575
+ else classes.push(...prepareScArray(value, prefix));
576
+ };
577
+ apply(target, "");
578
+ apply(source, "");
579
+ return toValue(classes, cleared);
580
+ }
581
+ const ownValue = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : void 0;
582
+ /** Merges a later shortcut map (source) into an earlier one (target) */
563
583
  const mergeTwoVunorShortcuts = (target, source) => {
564
584
  const result = {};
565
- const keys = new Set([...Object.keys(source), ...Object.keys(target)]);
566
- for (const key of Array.from(keys)) {
567
- const inSource = Object.prototype.hasOwnProperty.call(source, key);
568
- const inTarget = Object.prototype.hasOwnProperty.call(target, key);
569
- const targetEntry = target[key];
570
- if (inSource && isReplacement(source[key])) result[key] = source[key];
571
- else if (inSource && inTarget && isReplacement(targetEntry)) result[key] = markReplacement([...targetEntry, toClassString(source[key])]);
572
- else if (inSource && inTarget) {
573
- let sourceValue = source[key];
574
- let targetValue = targetEntry;
575
- if (Array.isArray(sourceValue)) sourceValue = sourceValue.join(" ");
576
- if (Array.isArray(targetValue)) targetValue = targetValue.join(" ");
577
- if (typeof sourceValue === "string" && typeof targetValue === "string") result[key] = `${targetValue} ${sourceValue}`;
578
- else if (typeof sourceValue === "object" && typeof targetValue === "string") result[key] = `${targetValue} ${toUnoShortcut(sourceValue)}`;
579
- else if (typeof sourceValue === "string" && typeof targetValue === "object") result[key] = `${toUnoShortcut(targetValue)} ${sourceValue}`;
580
- else if (typeof sourceValue === "object" && typeof targetValue === "object") result[key] = `${toUnoShortcut(targetValue)} ${toUnoShortcut(sourceValue)}`;
581
- } else if (inSource) result[key] = source[key];
582
- else result[key] = target[key];
583
- }
585
+ for (const key of new Set([...Object.keys(source), ...Object.keys(target)])) result[key] = mergeValue(ownValue(target, key), ownValue(source, key));
584
586
  return result;
585
587
  };
586
588
  /**
587
- * Merges an array of vunor shortcuts
588
- * Items from the beginning have less priority. An entry marked with
589
- * `replaceShortcut` drops the earlier entries of its name.
589
+ * Merges an array of vunor shortcut maps; later maps win. A later value's classes
590
+ * are appended after the earlier ones of its name. At any key (the name or a
591
+ * variant), `""` or `[]` drops the earlier classes under that key path, nested
592
+ * variants included; a `replaceShortcut` value drops them and adds its own.
590
593
  *
591
594
  * @param shortcuts TVunorShortcut[]
592
595
  * @returns TVunorShortcut
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vunor",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "files": [
5
5
  "dist",
6
6
  "CHANGELOG.md"