@skirbi/sugar 0.1.1 → 0.1.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/Changes CHANGED
@@ -1,5 +1,15 @@
1
1
  Revision history for @skirbi/sugar
2
2
 
3
+ 0.1.2 2026-06-05 13:39:51Z
4
+
5
+ * Add replaceChildrenSugarSafe to allow repaints without focus loss
6
+
7
+ Add initial version of a safer version for `replaceChildren()` called
8
+ `replaceChildrenSugarSafe()`. During real life usage it became apparant
9
+ that inputs lose focus with `wire:model` changes. This is slightly
10
+ annoying. Allow for a more fine grained approach where we update
11
+ existing fragements rather than replacing them full fledged.
12
+
3
13
  0.1.1 2026-06-04 15:34:20Z
4
14
 
5
15
  * Added morph-* attributes to withConnectedSugar, the API of this is
@@ -101,7 +101,7 @@ export class HTMLElementSugarSelect extends HTMLElementSugarInput {
101
101
 
102
102
  this.setupSelectControl(frag);
103
103
 
104
- this.replaceChildren(frag);
104
+ this.replaceChildrenSugarSafe(frag);
105
105
  }
106
106
 
107
107
  _addPlaceholder(select, text) {
@@ -437,5 +437,59 @@ export class HTMLElementSugar extends HTMLElement {
437
437
  $$(selector) {
438
438
  return [...this.querySelectorAll(selector)]
439
439
  }
440
+
441
+ _morphElement(oldEl, newEl) {
442
+ this._morphAttributes(oldEl, newEl);
443
+
444
+ const newChildren = Array.from(newEl.childNodes);
445
+ const oldChildren = Array.from(oldEl.childNodes);
446
+
447
+ newChildren.forEach((newNode, i) => {
448
+ const oldNode = oldChildren[i];
449
+ if (!oldNode) {
450
+ oldEl.appendChild(newNode);
451
+ }
452
+ else if (oldNode.nodeType === newNode.nodeType && oldNode.nodeName === newNode.nodeName) {
453
+ if (oldNode.nodeType === Node.TEXT_NODE) {
454
+ if (oldNode.textContent !== newNode.textContent) {
455
+ oldNode.textContent = newNode.textContent;
456
+ }
457
+ }
458
+ else {
459
+ this._morphElement(oldNode, newNode);
460
+ }
461
+ }
462
+ else {
463
+ oldEl.replaceChild(newNode, oldNode);
464
+ }
465
+ });
466
+
467
+ oldChildren.slice(newChildren.length).forEach(n => oldEl.removeChild(n));
468
+ }
469
+
470
+ _morphAttributes(oldEl, newEl) {
471
+ if (!oldEl.attributes || !newEl.attributes) return;
472
+
473
+ for (const attr of newEl.attributes) {
474
+ if (oldEl.getAttribute(attr.name) !== attr.value) {
475
+ oldEl.setAttribute(attr.name, attr.value);
476
+ }
477
+ }
478
+ for (const attr of Array.from(oldEl.attributes)) {
479
+ if (!newEl.hasAttribute(attr.name)) {
480
+ oldEl.removeAttribute(attr.name);
481
+ }
482
+ }
483
+ }
484
+
485
+ _morphChildren(frag) {
486
+ this._morphElement(this, frag);
487
+ }
488
+
489
+
490
+ replaceChildrenSugarSafe(frag) {
491
+ this._morphElement(this, frag);
492
+ }
493
+
440
494
  }
441
495
 
package/lib/index.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  //
3
3
  // SPDX-License-Identifier: MIT
4
4
 
5
- const VERSION = "0.1.1";
5
+ const VERSION = "0.1.2";
6
6
 
7
7
  export { HTMLElementSugar } from './htmlelement.mjs';
8
8
  export { HTMLElementSugarInput } from './htmlelement-input.mjs';
package/package.json CHANGED
@@ -52,5 +52,5 @@
52
52
  },
53
53
  "sideEffects": false,
54
54
  "type": "module",
55
- "version": "0.1.1"
55
+ "version": "0.1.2"
56
56
  }