@rhi-zone/rainbow-ui 0.2.0-alpha.1 → 0.2.0-alpha.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.
@@ -18,15 +18,27 @@ import type { Optic } from "@rhi-zone/rainbow";
18
18
  import type { AnyEl } from "./html.js";
19
19
  import type { Widget } from "./widget.js";
20
20
  /**
21
- * Maps field names of `T` to optics that convert between `string | null`
22
- * (raw HTML attribute) and `T[K]` (typed signal field).
21
+ * An entry in `AttrSchema<T>`. Either a plain optic (attribute name = prop name)
22
+ * or a named entry `{ name, optic }` where `name` is the HTML attribute name
23
+ * (e.g. `"requires-review"`) and the TypeScript key remains the camelCase prop.
23
24
  *
24
25
  * - `optic.view(raw)` — parse attribute string into `T[K]`;
25
26
  * `undefined` means use `defaults[K]`
26
27
  * - `optic.review(v, _)` — serialise `T[K]` back to a string for reflection
28
+ *
29
+ * @example
30
+ * // Simple: attr name = prop name
31
+ * attrs: { label: attrString, score: attrNumber }
32
+ *
33
+ * // Alias: HTML "requires-review" → TS prop "requiresReview"
34
+ * attrs: { requiresReview: { name: "requires-review", optic: attrBoolean } }
27
35
  */
36
+ export type AttrEntry<V> = Optic<string | null, V> | {
37
+ name: string;
38
+ optic: Optic<string | null, V>;
39
+ };
28
40
  export type AttrSchema<T> = {
29
- [K in keyof T]?: Optic<string | null, T[K]>;
41
+ [K in keyof T]?: AttrEntry<T[K]>;
30
42
  };
31
43
  /**
32
44
  * Pass-through: `view` returns the raw string, or `undefined` when absent.
package/dist/elements.js CHANGED
@@ -1,16 +1,23 @@
1
- var y = Object.defineProperty;
2
- var m = (t, e, n) => e in t ? y(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
3
- var a = (t, e, n) => m(t, typeof e != "symbol" ? e + "" : e, n);
4
- import { signal as v } from "@rhi-zone/rainbow";
5
- import { mount as S } from "./widget.js";
6
- const w = {
1
+ var v = Object.defineProperty;
2
+ var S = (t, e, n) => e in t ? v(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
3
+ var f = (t, e, n) => S(t, typeof e != "symbol" ? e + "" : e, n);
4
+ import { signal as w } from "@rhi-zone/rainbow";
5
+ import { mount as k } from "./widget.js";
6
+ function N(t, e) {
7
+ if ("name" in e && typeof e.name == "string") {
8
+ const n = e;
9
+ return { attrName: n.name, optic: n.optic };
10
+ }
11
+ return { attrName: t, optic: e };
12
+ }
13
+ const O = {
7
14
  view(t) {
8
15
  return t ?? void 0;
9
16
  },
10
17
  review(t) {
11
18
  return t;
12
19
  }
13
- }, k = {
20
+ }, C = {
14
21
  view(t) {
15
22
  if (t == null) return;
16
23
  const e = Number(t);
@@ -19,7 +26,7 @@ const w = {
19
26
  review(t) {
20
27
  return String(t);
21
28
  }
22
- }, N = {
29
+ }, E = {
23
30
  view(t) {
24
31
  if (t != null)
25
32
  return t !== "false" && t !== "0";
@@ -28,7 +35,7 @@ const w = {
28
35
  return String(t);
29
36
  }
30
37
  };
31
- function A() {
38
+ function K() {
32
39
  return {
33
40
  view(t) {
34
41
  if (t != null)
@@ -43,15 +50,15 @@ function A() {
43
50
  }
44
51
  };
45
52
  }
46
- function J(t) {
53
+ function M(t) {
47
54
  const e = {};
48
55
  for (const n of Object.keys(t)) {
49
56
  const s = typeof t[n];
50
- s === "string" ? e[n] = w : s === "number" ? e[n] = k : s === "boolean" && (e[n] = N);
57
+ s === "string" ? e[n] = O : s === "number" ? e[n] = C : s === "boolean" && (e[n] = E);
51
58
  }
52
59
  return e;
53
60
  }
54
- function h(t) {
61
+ function m(t) {
55
62
  if (typeof t == "string") {
56
63
  const e = new CSSStyleSheet();
57
64
  return e.replaceSync(t), e;
@@ -62,57 +69,62 @@ function x(t, e) {
62
69
  const {
63
70
  widget: n,
64
71
  defaults: s,
65
- attrs: u = {},
66
- shadow: l = "open",
72
+ attrs: p = {},
73
+ shadow: c = "open",
67
74
  styles: i
68
- } = e, g = Object.keys(u), c = i == null ? [] : Array.isArray(i) ? i.map(h) : [h(i)];
69
- class f extends HTMLElement {
75
+ } = e, u = /* @__PURE__ */ new Map();
76
+ for (const o of Object.keys(p)) {
77
+ const a = p[o], { attrName: r, optic: g } = N(o, a);
78
+ u.set(r, { propKey: o, optic: g });
79
+ }
80
+ const h = [...u.keys()], b = i == null ? [] : Array.isArray(i) ? i.map(m) : [m(i)];
81
+ class d extends HTMLElement {
70
82
  constructor() {
71
83
  super(...arguments);
72
84
  // Use underscore prefix rather than # so Object.defineProperty below can
73
85
  // access instance state from outside the class body.
74
- a(this, "_rb_signal", v({ ...s }));
75
- a(this, "_rb_cleanup", null);
86
+ f(this, "_rb_signal", w({ ...s }));
87
+ f(this, "_rb_cleanup", null);
76
88
  }
77
89
  static get observedAttributes() {
78
- return g;
90
+ return h;
79
91
  }
80
92
  connectedCallback() {
81
- const r = l !== !1 ? this.shadowRoot ?? this.attachShadow({ mode: l }) : this;
82
- l !== !1 && c.length > 0 && (r.adoptedStyleSheets = c), this._rb_cleanup = S(n, this._rb_signal, r);
93
+ const r = c !== !1 ? this.shadowRoot ?? this.attachShadow({ mode: c }) : this;
94
+ c !== !1 && b.length > 0 && (r.adoptedStyleSheets = b), this._rb_cleanup = k(n, this._rb_signal, r);
83
95
  }
84
96
  disconnectedCallback() {
85
97
  var r;
86
98
  (r = this._rb_cleanup) == null || r.call(this), this._rb_cleanup = null;
87
99
  }
88
- attributeChangedCallback(r, O, _) {
89
- const d = u[r];
90
- if (d == null) return;
91
- const p = d.view(_) ?? s[r];
92
- this._rb_signal.set({ ...this._rb_signal.get(), [r]: p });
100
+ attributeChangedCallback(r, g, y) {
101
+ const l = u.get(r);
102
+ if (l == null) return;
103
+ const _ = l.optic.view(y) ?? s[l.propKey];
104
+ this._rb_signal.set({ ...this._rb_signal.get(), [l.propKey]: _ });
93
105
  }
94
106
  }
95
107
  for (const o of Object.keys(s))
96
- Object.defineProperty(f.prototype, o, {
108
+ Object.defineProperty(d.prototype, o, {
97
109
  get() {
98
110
  return this._rb_signal.get()[o];
99
111
  },
100
- set(b) {
112
+ set(a) {
101
113
  this._rb_signal.set({
102
114
  ...this._rb_signal.get(),
103
- [o]: b
115
+ [o]: a
104
116
  });
105
117
  },
106
118
  configurable: !0,
107
119
  enumerable: !0
108
120
  });
109
- customElements.define(t, f);
121
+ customElements.define(t, d);
110
122
  }
111
123
  export {
112
- N as attrBoolean,
113
- A as attrJson,
114
- k as attrNumber,
115
- w as attrString,
116
- J as attrsFrom,
124
+ E as attrBoolean,
125
+ K as attrJson,
126
+ C as attrNumber,
127
+ O as attrString,
128
+ M as attrsFrom,
117
129
  x as defineElement
118
130
  };
package/dist/index.d.ts CHANGED
@@ -3,4 +3,4 @@ export * from "./widget.js";
3
3
  export * from "./form-state.js";
4
4
  export * from "./keybinds.js";
5
5
  export { attrString, attrNumber, attrBoolean, attrJson, attrsFrom, defineElement } from "./elements.js";
6
- export type { AttrSchema, PrimitiveAttrSchema } from "./elements.js";
6
+ export type { AttrEntry, AttrSchema, PrimitiveAttrSchema } from "./elements.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhi-zone/rainbow-ui",
3
- "version": "0.2.0-alpha.1",
3
+ "version": "0.2.0-alpha.2",
4
4
  "description": "Type-safe DOM factories and algebraic widget combinators for rainbow",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",