wrec 0.22.0 → 0.22.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
@@ -2,8 +2,7 @@
2
2
 
3
3
  <img alt="shipwreck" src="shipwreck.png" style="width: 256px">
4
4
 
5
- Wrec is a small, zero dependency library that
6
- greatly simplifies building web components.
5
+ Wrec is a small library that greatly simplifies building web components.
7
6
  It is described in detail, with several working examples,
8
7
  in [my blog](https://mvolkmann.github.io/blog/wrec/).
9
8
  Also, see my series of
@@ -8,18 +8,13 @@ export declare function css(strings: TemplateStringsArray, ...values: unknown[])
8
8
 
9
9
  export declare function html(strings: TemplateStringsArray, ...values: unknown[]): string;
10
10
 
11
- declare const HTMLElementBase: {
12
- new (): HTMLElement;
13
- prototype: HTMLElement;
14
- };
15
-
16
11
  declare type LooseObject = Record<string, unknown>;
17
12
 
18
13
  declare type StringToAny = Record<string, any>;
19
14
 
20
15
  declare type StringToString = Record<string, string>;
21
16
 
22
- export declare abstract class Wrec extends HTMLElementBase implements ChangeListener {
17
+ export declare abstract class Wrec extends HTMLElement implements ChangeListener {
23
18
  #private;
24
19
  private static attrToPropMap;
25
20
  private static propToAttrMap;
package/dist/wrec.d.ts CHANGED
@@ -8,18 +8,13 @@ export declare function css(strings: TemplateStringsArray, ...values: unknown[])
8
8
 
9
9
  export declare function html(strings: TemplateStringsArray, ...values: unknown[]): string;
10
10
 
11
- declare const HTMLElementBase: {
12
- new (): HTMLElement;
13
- prototype: HTMLElement;
14
- };
15
-
16
11
  declare type LooseObject = Record<string, unknown>;
17
12
 
18
13
  declare type StringToAny = Record<string, any>;
19
14
 
20
15
  declare type StringToString = Record<string, string>;
21
16
 
22
- export declare abstract class Wrec extends HTMLElementBase implements ChangeListener {
17
+ export declare abstract class Wrec extends HTMLElement implements ChangeListener {
23
18
  #private;
24
19
  private static attrToPropMap;
25
20
  private static propToAttrMap;
package/dist/wrec.es.js CHANGED
@@ -206,8 +206,7 @@ const nt = /* @__PURE__ */ new Set([
206
206
  "id",
207
207
  "tabindex",
208
208
  "title"
209
- ]), rt = globalThis.HTMLElement ?? class {
210
- }, F = globalThis.customElements ?? {
209
+ ]), F = globalThis.customElements ?? {
211
210
  get: (n) => {
212
211
  },
213
212
  getName: () => "",
@@ -215,22 +214,24 @@ const nt = /* @__PURE__ */ new Set([
215
214
  },
216
215
  upgrade: () => {
217
216
  },
218
- whenDefined: () => Promise.reject(new Error("customElements is not available in this environment"))
217
+ whenDefined: () => Promise.reject(
218
+ new Error("customElements is not available in this environment")
219
+ )
219
220
  };
220
221
  class E extends Error {
221
222
  }
222
- const ct = /([a-zA-Z-]+)\s*:\s*([^;}]+)/g, Y = "a-zA-Z_$", at = Y + "0-9", S = `[${Y}][${at}]*`, ft = /<!--\s*(.*?)\s*-->/, ht = /<(\w+)(?:\s[^>]*)?>((?:[^<]|<(?!\w))*?)<\/\1>/g, L = new RegExp(`^this\\.${S}$`), j = new RegExp(`this\\.${S}(\\.${S})*`, "g"), G = new RegExp(`this\\.${S}(\\.${S})*`), lt = 5;
223
- function pt(n) {
223
+ const rt = /([a-zA-Z-]+)\s*:\s*([^;}]+)/g, Y = "a-zA-Z_$", ct = Y + "0-9", S = `[${Y}][${ct}]*`, at = /<!--\s*(.*?)\s*-->/, ft = /<(\w+)(?:\s[^>]*)?>((?:[^<]|<(?!\w))*?)<\/\1>/g, L = new RegExp(`^this\\.${S}$`), j = new RegExp(`this\\.${S}(\\.${S})*`, "g"), G = new RegExp(`this\\.${S}(\\.${S})*`), ht = 5;
224
+ function lt(n) {
224
225
  return n instanceof HTMLButtonElement || n instanceof HTMLFieldSetElement || n instanceof HTMLInputElement || n instanceof HTMLSelectElement || n instanceof HTMLTextAreaElement || n instanceof u;
225
226
  }
226
- function wt(n, t, e) {
227
+ function yt(n, t, e) {
227
228
  const s = document.createElement(n);
228
229
  if (t)
229
230
  for (const [o, i] of Object.entries(t))
230
231
  s.setAttribute(o, i);
231
232
  return e && (s.innerHTML = e), s;
232
233
  }
233
- const ut = (n) => n === String ? "" : n === Number ? 0 : n === Boolean ? !1 : n === Array ? [] : n === Object ? {} : void 0;
234
+ const pt = (n) => n === String ? "" : n === Number ? 0 : n === Boolean ? !1 : n === Array ? [] : n === Object ? {} : void 0;
234
235
  function R(n) {
235
236
  const t = [];
236
237
  let e = n.firstElementChild;
@@ -238,7 +239,7 @@ function R(n) {
238
239
  t.push(e), e.shadowRoot && t.push(...R(e.shadowRoot)), e.firstElementChild && t.push(...R(e)), e = e.nextElementSibling;
239
240
  return t;
240
241
  }
241
- const M = (n) => n.substring(lt).split(".")[0];
242
+ const M = (n) => n.substring(ht).split(".")[0];
242
243
  function Q(n, t) {
243
244
  let e = n[0];
244
245
  return t.forEach((s, o) => {
@@ -256,11 +257,11 @@ function D(n) {
256
257
  const { localName: t } = n;
257
258
  return t === "input" || t === "select";
258
259
  }
259
- const dt = (n) => n.replace(/<!--[\s\S]*?-->/g, "");
260
+ const ut = (n) => n.replace(/<!--[\s\S]*?-->/g, "");
260
261
  function tt(n, t, e, s) {
261
262
  return n.slice(0, t) + s + n.slice(t + e);
262
263
  }
263
- function H(n) {
264
+ function k(n) {
264
265
  const t = Number(n);
265
266
  if (isNaN(t)) throw new E(`can't convert "${n}" to a number`);
266
267
  return t;
@@ -281,11 +282,11 @@ function et(n, t, e) {
281
282
  n[i] = e;
282
283
  }
283
284
  }
284
- function k(n, t, e) {
285
+ function H(n, t, e) {
285
286
  const [s, o] = t.split(":");
286
287
  n instanceof CSSStyleRule ? n.style.setProperty(s, e) : (et(n, s, e), s === "value" && D(n) && (n.value = e));
287
288
  }
288
- async function mt(n) {
289
+ async function dt(n) {
289
290
  const t = /* @__PURE__ */ new Set();
290
291
  for (const s of R(n.content)) {
291
292
  const { localName: o } = s;
@@ -301,11 +302,14 @@ async function mt(n) {
301
302
  }
302
303
  return Promise.all(
303
304
  [...t].map(
304
- async (s) => Promise.race([F.whenDefined(s), e(s)])
305
+ async (s) => Promise.race([
306
+ F.whenDefined(s),
307
+ e(s)
308
+ ])
305
309
  )
306
310
  );
307
311
  }
308
- class u extends rt {
312
+ class u extends HTMLElement {
309
313
  // There is one instance of `attrToPropMap`, `properties`, `propToAttrMap`,
310
314
  // `propToComputedMap`, and `propToExprsMap` per Wrec subclass,
311
315
  // not one for only the Wrec class.
@@ -431,7 +435,7 @@ class u extends rt {
431
435
  async #x() {
432
436
  const t = this.#t;
433
437
  let { template: e } = t;
434
- e || (e = t.template = document.createElement("template"), e.innerHTML = t.buildHTML()), await mt(e), this.shadowRoot.replaceChildren(e.content.cloneNode(!0));
438
+ e || (e = t.template = document.createElement("template"), e.innerHTML = t.buildHTML()), await dt(e), this.shadowRoot.replaceChildren(e.content.cloneNode(!0));
435
439
  }
436
440
  static buildHTML() {
437
441
  let t = `<style>
@@ -469,19 +473,19 @@ class u extends rt {
469
473
  e.required && !i && this.#e(this, o, "is a required attribute");
470
474
  let r = e.value;
471
475
  this.hasOwnProperty(t) && (r = this[t], delete this[t]);
472
- const { type: c } = e, a = c === Boolean ? r || i : s.includes(o) && i ? this.#A(t, o) : r || ut(c), h = "#" + t;
476
+ const { type: c } = e, a = c === Boolean ? r || i : s.includes(o) && i ? this.#A(t, o) : r || pt(c), h = "#" + t;
473
477
  this[h] = a, e.computed && this.#L(t, e), Object.defineProperty(this, t, {
474
478
  enumerable: !0,
475
479
  get() {
476
480
  return this[h];
477
481
  },
478
482
  set(f) {
479
- c === Number && typeof f == "string" && (f = H(f));
483
+ c === Number && typeof f == "string" && (f = k(f));
480
484
  const l = this[h];
481
485
  if (f === l) return;
482
486
  this.#I(t, c, f), this[h] = f;
483
487
  const m = this.#m.get(t);
484
- m && st(m.state, m.stateProp, f), this.#j(t, c, f, o), this.#f || (this.#H(t), this.#M(t)), this.#k(t, f);
488
+ m && st(m.state, m.stateProp, f), this.#j(t, c, f, o), this.#f || (this.#k(t), this.#M(t)), this.#H(t, f);
485
489
  const x = this.#h[t];
486
490
  x && this.setFormValue(x, String(f)), this.propertyChangedCallback(t, l, f), e.dispatch && this.dispatch("change", {
487
491
  tagName: this.localName,
@@ -495,7 +499,7 @@ class u extends rt {
495
499
  #g() {
496
500
  const t = this.hasAttribute("disabled"), e = R(this.shadowRoot);
497
501
  for (const s of e)
498
- pt(s) && (s.disabled = t);
502
+ lt(s) && (s.disabled = t);
499
503
  }
500
504
  disconnectedCallback() {
501
505
  this.#o.clear(), this.#l.clear(), this.#u.clear();
@@ -539,7 +543,7 @@ class u extends rt {
539
543
  this.#v(i, s);
540
544
  else if (!(i instanceof CSSStyleRule)) {
541
545
  const { element: r, attrName: c } = i;
542
- r instanceof CSSStyleRule ? r.style.setProperty(c, s) : k(r, c, s);
546
+ r instanceof CSSStyleRule ? r.style.setProperty(c, s) : H(r, c, s);
543
547
  }
544
548
  }
545
549
  }
@@ -567,7 +571,7 @@ class u extends rt {
567
571
  let s = "";
568
572
  if (A(t)) {
569
573
  this.#c(t.textContent, t);
570
- const o = t.textContent?.match(ft);
574
+ const o = t.textContent?.match(at);
571
575
  o && (s = o[1]);
572
576
  } else {
573
577
  const o = Array.from(t.childNodes).find(
@@ -635,7 +639,7 @@ class u extends rt {
635
639
  const { target: l } = f;
636
640
  if (!l) return;
637
641
  const m = l.value, { type: x } = this.#t.properties[h];
638
- this[h] = x === Number ? H(m) : m, this.#M(h);
642
+ this[h] = x === Number ? k(m) : m, this.#M(h);
639
643
  });
640
644
  }
641
645
  #n(t) {
@@ -699,7 +703,7 @@ class u extends rt {
699
703
  const o = this.#d(e, s, t);
700
704
  if (!o) {
701
705
  const a = t.replaceAll("this..", "this.");
702
- s ? k(e, s, a) : "textContent" in e && (e.textContent = a);
706
+ s ? H(e, s, a) : "textContent" in e && (e.textContent = a);
703
707
  return;
704
708
  }
705
709
  const i = this.#t;
@@ -721,7 +725,7 @@ class u extends rt {
721
725
  let r = this.#o.get(t);
722
726
  r || (r = [], this.#o.set(t, r)), r.push(s ? { element: e, attrName: s } : e), e instanceof HTMLElement && this.#O(e, s, o);
723
727
  const c = this.#s(t);
724
- s ? k(e, s, c) : this.#v(e, c);
728
+ s ? H(e, s, c) : this.#v(e, c);
725
729
  }
726
730
  // This follows the best practice
727
731
  // "Do not override author-set, global attributes."
@@ -732,9 +736,7 @@ class u extends rt {
732
736
  !this.#i || !I(e) || (this.#i.set(t, e), this.#p?.setFormValue(this.#i));
733
737
  }
734
738
  static ssr(t = {}) {
735
- throw new E(
736
- 'SSR support moved to the "wrec/ssr" entry. Import from "wrec/ssr" to use Wrec.ssr().'
737
- );
739
+ throw new E('Import Wrec from "wrec/ssr" to use the ssr method.');
738
740
  }
739
741
  #e(t, e, s) {
740
742
  const o = t instanceof HTMLElement ? t.localName : "CSS rule";
@@ -752,7 +754,7 @@ class u extends rt {
752
754
  if (e?.match(j)) return e;
753
755
  const s = this.#t, { type: o } = s.properties[t];
754
756
  if (o || this.#e(null, t, "does not specify its type"), o === String) return e;
755
- if (o === Number) return H(e);
757
+ if (o === Number) return k(e);
756
758
  if (o === Boolean) {
757
759
  if (e === "true") return !0;
758
760
  if (e === "false" || e === "null") return !1;
@@ -774,7 +776,7 @@ class u extends rt {
774
776
  }
775
777
  // Updates all computed properties that reference this property.
776
778
  // VS Code thinks this is never called, but it is called by #defineProp.
777
- #H(t) {
779
+ #k(t) {
778
780
  const s = this.#t.propToComputedMap.get(t) || [];
779
781
  for (const [o, i] of s)
780
782
  this[o] = this.#s(i);
@@ -799,7 +801,7 @@ class u extends rt {
799
801
  }
800
802
  // Update corresponding parent web component property if bound to one.
801
803
  // VS Code thinks this is never called, but it is called by #defineProp.
802
- #k(t, e) {
804
+ #H(t, e) {
803
805
  const s = this.#u.get(t);
804
806
  if (!s) return;
805
807
  const o = this.getRootNode();
@@ -902,10 +904,10 @@ class u extends rt {
902
904
  }
903
905
  }
904
906
  }
905
- function Et(n, ...t) {
907
+ function wt(n, ...t) {
906
908
  let e = Q(n, t);
907
909
  for (; ; ) {
908
- const s = ct.exec(e);
910
+ const s = rt.exec(e);
909
911
  if (!s) break;
910
912
  const o = s[2];
911
913
  if (G.test(o)) {
@@ -919,12 +921,12 @@ function Et(n, ...t) {
919
921
  }
920
922
  return e;
921
923
  }
922
- function Tt(n, ...t) {
924
+ function Et(n, ...t) {
923
925
  let e = Q(n, t);
924
926
  for (; ; ) {
925
- const s = ht.exec(e);
927
+ const s = ft.exec(e);
926
928
  if (!s || s[1] === "style") break;
927
- const o = dt(s[2]);
929
+ const o = ut(s[2]);
928
930
  if (G.test(o)) {
929
931
  const i = `<!-- ${o.trim()} -->`, r = s.index + s[0].indexOf(">") + 1;
930
932
  e = tt(e, r, o.length, i);
@@ -935,7 +937,7 @@ function Tt(n, ...t) {
935
937
  export {
936
938
  u as Wrec,
937
939
  _ as WrecState,
938
- wt as createElement,
939
- Et as css,
940
- Tt as html
940
+ yt as createElement,
941
+ wt as css,
942
+ Et as html
941
943
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "wrec",
3
3
  "description": "a small library that greatly simplifies building web components",
4
4
  "author": "R. Mark Volkmann",
5
- "version": "0.22.0",
5
+ "version": "0.22.1",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -19,8 +19,7 @@
19
19
  "exports": {
20
20
  ".": {
21
21
  "types": "./dist/wrec.d.ts",
22
- "import": "./dist/wrec.es.js",
23
- "require": "./dist/wrec.umd.js"
22
+ "import": "./dist/wrec.es.js"
24
23
  },
25
24
  "./ssr": {
26
25
  "types": "./dist/wrec-ssr.d.ts",