vunor 0.5.1 → 0.5.3

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,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.3
4
+
5
+ ### Fixed
6
+
7
+ - **A stacked state class wins over its base regardless of CSS order.** `class="c8-flat c8-flat-selected"` (and `c8-chrome c8-chrome-selected`, also on a shortcut that composes the base) now looks exactly like `class="c8-flat" aria-pressed="true"`, pressed and disabled included. Base and state class were one class each, so the order of their rules decided, and with an app's own shortcuts composing `c8-flat` the base's dark rule could come last: a selected item lost its scope colour in dark mode (white text and icon instead of `scope-color-400`). The attribute-driven states do not change. Each shortcut that composes `c8-flat` or `c8-chrome` adds two selectors to the CSS.
8
+
9
+ ## 0.5.2
10
+
11
+ ### Fixed
12
+
13
+ - **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:
14
+ - `''` 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);
15
+ - `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);
16
+ - classes are appended after the earlier ones, as before; the generated shortcuts of an app that only appends do not change.
17
+
18
+ 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).
19
+
20
+ - `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).
21
+
3
22
  ## 0.5.1
4
23
 
5
24
  ### 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
@@ -281,6 +281,7 @@ const btn = defineShortcuts({
281
281
  });
282
282
  //#endregion
283
283
  //#region src/theme/shortcuts/c8.ts
284
+ const whenStacked = (stateClass) => `[&.${stateClass.replace("-", "\\-")}]:`;
284
285
  const c8 = defineShortcuts({
285
286
  "c8-filled": {
286
287
  "": "current-bg-scope-color-600 rounded-r1 current-text-white current-icon-white icon-current/100 bg-current text-current",
@@ -308,7 +309,8 @@ const c8 = defineShortcuts({
308
309
  "data-[selected=true]:": "c8-flat-selected",
309
310
  "data-[on=true]:": "c8-flat-selected",
310
311
  "aria-[selected=true]:": "c8-flat-selected",
311
- "aria-[pressed=true]:": "c8-flat-selected"
312
+ "aria-[pressed=true]:": "c8-flat-selected",
313
+ [whenStacked("c8-flat-selected")]: "c8-flat-selected"
312
314
  },
313
315
  "c8-flat-hover": "bg-current/10",
314
316
  "c8-flat-active": "bg-current/15",
@@ -341,7 +343,8 @@ const c8 = defineShortcuts({
341
343
  "data-[selected=true]:": "c8-chrome-selected",
342
344
  "data-[on=true]:": "c8-chrome-selected",
343
345
  "aria-[selected=true]:": "c8-chrome-selected",
344
- "aria-[pressed=true]:": "c8-chrome-selected"
346
+ "aria-[pressed=true]:": "c8-chrome-selected",
347
+ [whenStacked("c8-chrome-selected")]: "c8-chrome-selected"
345
348
  },
346
349
  "c8-chrome-hover": {
347
350
  "": "current-bg-layer-light-1 bg-current",
@@ -521,72 +524,75 @@ const rawVunorShortcuts = [
521
524
  * @returns string
522
525
  */
523
526
  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();
527
+ return prepareScArray(obj).join(" ");
530
528
  }
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;
529
+ /** The class tokens of a value, each prefixed with `prefix` and the variant keys above it */
530
+ function prepareScArray(input, prefix = "") {
531
+ if (typeof input === "object" && !Array.isArray(input)) return Object.entries(input).flatMap(([key, val]) => prepareScArray(val, `${prefix}${key}`));
532
+ return toClassString(input).split(/\s+/).filter(Boolean).map((c) => `${prefix}${c}`);
537
533
  }
538
534
  const REPLACE = Symbol.for("vunor.replaceShortcut");
539
535
  /** A shortcut value as one UnoCSS class string */
540
536
  const toClassString = (value = "") => typeof value === "string" ? value : Array.isArray(value) ? value.join(" ") : toUnoShortcut(value);
541
537
  const markReplacement = (classes) => Object.defineProperty(classes, REPLACE, { value: true });
542
538
  const isReplacement = (value) => Array.isArray(value) && REPLACE in value;
539
+ /** `""`, `[]` or a `replaceShortcut` value: it drops the earlier classes under its key */
540
+ const clearsKey = (value) => isReplacement(value) || (typeof value === "string" || Array.isArray(value)) && prepareScArray(value).length === 0;
543
541
  /**
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.
542
+ * Marks a shortcut value as a replacement: when shortcuts are merged
543
+ * (`mergeVunorShortcuts`, `vunorShortcuts`), it drops the earlier classes under
544
+ * its key (on a shortcut name the whole shortcut, on a variant key only that
545
+ * variant) instead of merging into them. Later entries still merge on top of it.
547
546
  *
548
547
  * @example
549
548
  * vunorShortcuts(defineShortcuts({ press: replaceShortcut({ 'active:': 'scale-95' }) }))
549
+ * vunorShortcuts(defineShortcuts({ 'c8-filled': { 'dark:': replaceShortcut('ring-2') } }))
550
550
  *
551
551
  * @param value the shortcut, in any form a normal entry accepts
552
552
  * @returns the shortcut's class string in a marked array
553
553
  */
554
554
  const replaceShortcut = (value) => markReplacement([toClassString(value)]);
555
555
  /**
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
556
+ * Merged classes as a shortcut value. The cleared key paths stay in it as `''`
557
+ * entries, so they clear again when the result is merged onto an earlier shortcut.
562
558
  */
559
+ function toValue(classes, cleared) {
560
+ if (cleared.has("")) return markReplacement([classes.join(" ")]);
561
+ if (cleared.size === 0) return classes.join(" ");
562
+ const value = Object.fromEntries([...cleared].map((prefix) => [prefix, ""]));
563
+ if (classes.length > 0) value[""] = classes.join(" ");
564
+ return value;
565
+ }
566
+ /** Merges a later value (source) into an earlier one (target) of the same name */
567
+ function mergeValue(target, source) {
568
+ if (source === void 0) return target;
569
+ if (target === void 0 || clearsKey(source)) return source;
570
+ let classes = [];
571
+ const cleared = /* @__PURE__ */ new Set();
572
+ const apply = (value, prefix) => {
573
+ if (clearsKey(value)) {
574
+ classes = classes.filter((c) => !c.startsWith(prefix));
575
+ cleared.add(prefix);
576
+ }
577
+ if (typeof value === "object" && !Array.isArray(value)) for (const [key, val] of Object.entries(value)) apply(val, `${prefix}${key}`);
578
+ else classes.push(...prepareScArray(value, prefix));
579
+ };
580
+ apply(target, "");
581
+ apply(source, "");
582
+ return toValue(classes, cleared);
583
+ }
584
+ const ownValue = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : void 0;
585
+ /** Merges a later shortcut map (source) into an earlier one (target) */
563
586
  const mergeTwoVunorShortcuts = (target, source) => {
564
587
  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
- }
588
+ for (const key of new Set([...Object.keys(source), ...Object.keys(target)])) result[key] = mergeValue(ownValue(target, key), ownValue(source, key));
584
589
  return result;
585
590
  };
586
591
  /**
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.
592
+ * Merges an array of vunor shortcut maps; later maps win. A later value's classes
593
+ * are appended after the earlier ones of its name. At any key (the name or a
594
+ * variant), `""` or `[]` drops the earlier classes under that key path, nested
595
+ * variants included; a `replaceShortcut` value drops them and adds its own.
590
596
  *
591
597
  * @param shortcuts TVunorShortcut[]
592
598
  * @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.3",
4
4
  "files": [
5
5
  "dist",
6
6
  "CHANGELOG.md"