@twobirds/microcomponents 0.2.1 → 0.2.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.
Files changed (50) hide show
  1. package/README.md +45 -20
  2. package/dist/MC.min.js +1 -1
  3. package/dist/autoload.min.js +1 -1
  4. package/dist/core/MC.d.ts +289 -238
  5. package/dist/core/MC.d.ts.map +1 -1
  6. package/dist/core/MC.js +445 -184
  7. package/dist/core/elements.d.ts +58 -231
  8. package/dist/core/elements.d.ts.map +1 -1
  9. package/dist/core/elements.js +99 -214
  10. package/dist/core/index.d.ts +75 -0
  11. package/dist/core/index.d.ts.map +1 -0
  12. package/dist/core/index.js +72 -0
  13. package/dist/core/mcGuards.d.ts +7 -7
  14. package/dist/core/mcGuards.d.ts.map +1 -1
  15. package/dist/core/mcGuards.js +1 -1
  16. package/dist/elements.min.js +1 -1
  17. package/dist/formValues.min.js +1 -1
  18. package/dist/mcGuards.min.js +1 -1
  19. package/dist/microcomponents.min.js +1 -1
  20. package/dist/observables.min.js +1 -1
  21. package/dist/state/formValues.d.ts +113 -66
  22. package/dist/state/formValues.d.ts.map +1 -1
  23. package/dist/state/formValues.js +134 -68
  24. package/dist/state/index.d.ts +7 -0
  25. package/dist/state/index.d.ts.map +1 -0
  26. package/dist/state/index.js +5 -0
  27. package/dist/state/observables.d.ts +66 -133
  28. package/dist/state/observables.d.ts.map +1 -1
  29. package/dist/state/observables.js +185 -83
  30. package/dist/state/placeholders.d.ts +1 -0
  31. package/dist/state/placeholders.d.ts.map +1 -1
  32. package/dist/state/placeholders.js +1 -0
  33. package/dist/types/index.d.ts +7 -0
  34. package/dist/types/index.d.ts.map +1 -0
  35. package/dist/types/index.js +1 -0
  36. package/dist/types/mcTypes.d.ts +3 -3
  37. package/dist/types/mcTypes.js +1 -1
  38. package/dist/utils/autoload.d.ts +6 -2
  39. package/dist/utils/autoload.d.ts.map +1 -1
  40. package/dist/utils/autoload.js +7 -5
  41. package/dist/utils/events.d.ts +22 -79
  42. package/dist/utils/events.d.ts.map +1 -1
  43. package/dist/utils/events.js +63 -1
  44. package/dist/utils/helpers.d.ts +10 -9
  45. package/dist/utils/helpers.d.ts.map +1 -1
  46. package/dist/utils/helpers.js +26 -14
  47. package/dist/utils/index.d.ts +17 -0
  48. package/dist/utils/index.d.ts.map +1 -0
  49. package/dist/utils/index.js +12 -0
  50. package/package.json +13 -15
@@ -1,9 +1,9 @@
1
1
  /**
2
- @module Type layer
3
2
 
4
3
  Pure type layer of the microcomponent system.
5
4
 
6
5
  RULE: types only — no runtime exports, no imports. It sits at the bottom of the
7
6
  dependency graph so every other module can import from it without cycles.
7
+ @module Types
8
8
  */
9
9
  export {};
@@ -1,5 +1,5 @@
1
1
  /**
2
- @module
2
+ @module Autoload
3
3
 
4
4
  The {@linkcode autoload} function enables automatic detection and instantiation of microcomponents in the DOM via a `MutationObserver`. When enabled, newly added elements with undefined custom tags or `data-mc` references will trigger dynamic script loading and component attachment.
5
5
 
@@ -20,6 +20,7 @@ autoload(true); // enable auto-loading
20
20
  console.log(autoload.state); // Set of component names currently loading
21
21
  autoload(false); // disable
22
22
  ```
23
+ @group Autoload
23
24
  */
24
25
  export interface Autoload {
25
26
  /**
@@ -50,5 +51,8 @@ declare global {
50
51
  autoload?: Autoload;
51
52
  }
52
53
  }
53
- export declare const autoload: Autoload;
54
+ export declare function autoload(enable?: boolean): boolean;
55
+ export declare namespace autoload {
56
+ var state: Set<string>;
57
+ }
54
58
  //# sourceMappingURL=autoload.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"autoload.d.ts","sourceRoot":"","sources":["../../src/utils/autoload.ts"],"names":[],"mappings":"AAAA;;;;;;;;;EASE;AAoJF;;;;;;;;;;;;EAYE;AACF,MAAM,WAAW,QAAQ;IACxB;;;;;;;;;;;;;IAaG;IACH,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IAC5B;;;;;IAKG;IACH,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACnB;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,MAAM;QACf,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACpB;CACD;AAID,eAAO,MAAM,QAAQ,EAAE,QAuBtB,CAAC"}
1
+ {"version":3,"file":"autoload.d.ts","sourceRoot":"","sources":["../../src/utils/autoload.ts"],"names":[],"mappings":"AAAA;;;;;;;;;EASE;AAqJF;;;;;;;;;;;;;EAaE;AACF,MAAM,WAAW,QAAQ;IACxB;;;;;;;;;;;;;IAaG;IACH,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IAC5B;;;;;IAKG;IACH,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACnB;AAED,OAAO,CAAC,MAAM,CAAC;IACd,UAAU,MAAM;QACf,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACpB;CACD;AAID,wBAAgB,QAAQ,CAAC,MAAM,GAAE,OAAc,GAAG,OAAO,CAuBxD;yBAvBe,QAAQ"}
@@ -1,5 +1,5 @@
1
1
  /**
2
- @module
2
+ @module Autoload
3
3
 
4
4
  The {@linkcode autoload} function enables automatic detection and instantiation of microcomponents in the DOM via a `MutationObserver`. When enabled, newly added elements with undefined custom tags or `data-mc` references will trigger dynamic script loading and component attachment.
5
5
 
@@ -9,7 +9,7 @@ importing `DC`/`MC` never pulls a DOM observer into the bundle unless
9
9
  {@link autoload} itself is imported.
10
10
  */
11
11
  import { DC } from '../core/MC.js';
12
- import { isMcDOM } from '../core/mcGuards.js';
12
+ import { isMcHost } from '../core/mcGuards.js';
13
13
  const loadingDCs = new Set();
14
14
  /**
15
15
  Creates a closure that attaches DOM elements with the specified tag name to
@@ -24,6 +24,7 @@ element definition becomes available, and performs:
24
24
 
25
25
  @param element - The tag name (custom element name) of the microcomponent to instantiate.
26
26
  @returns A closure that performs the attachment logic when invoked.
27
+ @group Autoload
27
28
  */
28
29
  function attachDC(element) {
29
30
  return () => {
@@ -39,7 +40,7 @@ function attachDC(element) {
39
40
  /* v8 ignore start */
40
41
  if ('create' in customElementClass && typeof customElementClass.create === 'function') {
41
42
  const customElement = customElementClass.create(node);
42
- if (!isMcDOM(customElement))
43
+ if (!isMcHost(customElement))
43
44
  throw `create() of Class ${customElementClass.name} did not return a McHost.`;
44
45
  customElement.setAttribute('data-mc', node.getAttribute('data-mc') ?? '');
45
46
  node.replaceWith(customElement);
@@ -135,7 +136,7 @@ function loadUndefinedMCs() {
135
136
  });
136
137
  }
137
138
  let observer = null;
138
- export const autoload = function (enable = true) {
139
+ export function autoload(enable = true) {
139
140
  if (!enable) {
140
141
  if (observer) {
141
142
  observer.disconnect();
@@ -154,5 +155,6 @@ export const autoload = function (enable = true) {
154
155
  }, 5);
155
156
  window.autoload ??= autoload;
156
157
  return true;
157
- };
158
+ }
159
+ ;
158
160
  autoload.state = loadingDCs;
@@ -1,5 +1,5 @@
1
1
  /**
2
- @module
2
+ @module Events
3
3
 
4
4
  This module provides event utilities for interfacing **between microcomponents and external
5
5
  frameworks or native DOM/JS environments**. It is not intended for communication between
@@ -94,91 +94,34 @@ export declare function getMcEventName(eventName: string): string;
94
94
  */
95
95
  export declare function trueEventName(target: HTMLElement, eventName: string): string;
96
96
  /**
97
- * A query selector string used to match one or more DOM elements.
98
- *
99
- * @example
100
- * ```ts
101
- * const selector: QuerySelectorString = '.my-component';
102
- * ```
103
- */
97
+ A query selector string used to match one or more DOM elements.
98
+
99
+ @example
100
+ ```ts
101
+ const selector: QuerySelectorString = '.my-component';
102
+ ```
103
+ @group Events
104
+ */
104
105
  export type QuerySelectorString = string;
105
106
  /**
106
- * Represents the target of an event operation — either a concrete element,
107
- * a CSS selector string, or `undefined` (which is silently ignored).
108
- *
109
- * @example
110
- * ```ts
111
- * // Pass an HTMLElement directly
112
- * const el: TargetSelect = document.getElementById('root');
113
- *
114
- * // Or pass a selector string to match multiple elements
115
- * const allCards: TargetSelect = '.card';
116
- * ```
117
- */
107
+ Represents the target of an event operation — either a concrete element,
108
+ a CSS selector string, or `undefined` (which is silently ignored).
109
+
110
+ @example
111
+ ```ts
112
+ // Pass an HTMLElement directly
113
+ const el: TargetSelect = document.getElementById('root');
114
+
115
+ // Or pass a selector string to match multiple elements
116
+ const allCards: TargetSelect = '.card';
117
+ ```
118
+ @group Events
119
+ */
118
120
  export type TargetSelect = HTMLElement | QuerySelectorString | undefined | null;
119
- /**
120
- * Removes a previously attached event handler from the DOM node. See {@link on} and {@link one}.
121
- *
122
- * Uses internal microcomponent-prefixed naming for event handlers.
123
- * This is primarily used to interface with external frameworks or vanilla JS environments.
124
- * For communication between frontend microcomponents, use the `DC.off()` method instead.
125
- *
126
- * @param target - The DOM element from which to remove the listener.
127
- * @param eventName - The name of the event (e.g., 'click', 'update').
128
- * @param handler - The listener function or object to detach.
129
- *
130
- * @example
131
- * ```ts
132
- * const handler = on(myElement, 'click', () => console.log('clicked'));
133
- * off(myElement, 'click', handler); // removes the listener
134
- * ```
135
- */
136
121
  export declare function off<D = unknown>(target: TargetSelect, eventName: string, handler: McEventHandler<D>): void;
137
122
  export declare function off(target: TargetSelect, eventName: string, handler: EventListenerOrEventListenerObject): void;
138
- /**
139
- * Attaches a persistent event listener that will fire every time the specified event occurs.
140
- *
141
- * This is primarily used to interface with external frameworks or vanilla JS environments.
142
- * For communication between frontend microcomponents, use the `DC.on()` method instead.
143
- *
144
- * The handler receives a `CustomEvent<McDetail<D>>`: the payload sits under
145
- * `ev.detail.data`, the bubble-control handle under `ev.detail.control`.
146
- *
147
- * @param target - The DOM element to attach the listener to.
148
- * @param eventName - The name of the event (e.g., 'click', 'update').
149
- * @param handler - The callback function or object to invoke on each event.
150
- * @param once - If true, remove the listener after first invocation.
151
- * @returns The attached handler reference. Useful for later deletion with {@link off}.
152
- *
153
- * @example
154
- * ```ts
155
- * const handler = on<{ value: number }>(myElement, 'update', (ev) => {
156
- * console.log(ev.detail.data.value); // number
157
- * });
158
- * // to remove: off(myElement, 'update', handler);
159
- * ```
160
- */
161
123
  export declare function on<D = unknown>(target: TargetSelect, eventName: string, handler: McEventHandler<D>, once?: boolean): McEventHandler<D>;
162
124
  export declare function on(target: TargetSelect, eventName: string, handler: EventListenerOrEventListenerObject, once?: boolean): EventListenerOrEventListenerObject;
163
- /**
164
- * Attaches an event listener that will fire only once.
165
- *
166
- * Internally calls {@link on} with the `once` flag set to true. The handler
167
- * is automatically removed after its first invocation.
168
- * This is primarily used to interface with external frameworks or vanilla JS environments.
169
- * For communication between frontend microcomponents, use the `DC.one()` method instead.
170
- *
171
- * @param target - The DOM element to attach the listener to.
172
- * @param eventName - The name of the event (e.g., 'click', 'update').
173
- * @param handler - The callback function or object to invoke once.
174
- * @returns The attached one-time handler reference. Useful for later deletion with {@link off}.
175
- *
176
- * @example
177
- * ```ts
178
- * one<{ ready: boolean }>(myElement, 'dataReady', (ev) => console.log('received:', ev.detail.data));
179
- * // fires only once then detaches automatically
180
- * ```
181
- */
182
125
  export declare function one<D = unknown>(target: TargetSelect, eventName: string, handler: McEventHandler<D>): McEventHandler<D>;
183
126
  export declare function one(target: TargetSelect, eventName: string, handler: EventListenerOrEventListenerObject): EventListenerOrEventListenerObject;
184
127
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/utils/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAyDE;AAIF,OAAO,KAAK,EACX,gBAAgB,EAChB,kBAAkB,EAElB,cAAc,EACd,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAExD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAK5E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SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,IAAI,CAAC;AA4CR,wBAAgB,EAAE,CAAC,CAAC,GAAG,OAAO,EAC7B,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,EAC1B,IAAI,CAAC,EAAE,OAAO,GACZ,cAAc,CAAC,CAAC,CAAC,CAAC;AACrB,wBAAgB,EAAE,CACjB,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,EAC3C,IAAI,CAAC,EAAE,OAAO,GACZ,kCAAkC,CAAC;AAsDtC,wBAAgB,GAAG,CAAC,CAAC,GAAG,OAAO,EAC9B,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,GACxB,cAAc,CAAC,CAAC,CAAC,CAAC;AACrB,wBAAgB,GAAG,CAClB,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,kCAAkC,CAAC;AA0CtC;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,gBAAiB,YAAW,kBAAkB;IAC1D,OAAO,UAAS;;IAEhB;;;;;;;;;;;;;;;OAeG;IACH,IAAI;CAGJ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,OAAO,CAAC,CAAC,GAAG,OAAO,EAClC,MAAM,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,EAC/C,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,CAAW,EACjB,MAAM,GAAE,gBAAsB,EAAE,wBAAwB;AACxD,UAAU,CAAC,EAAE,gBAAgB,QA+C7B"}
@@ -1,5 +1,5 @@
1
1
  /**
2
- @module
2
+ @module Events
3
3
 
4
4
  This module provides event utilities for interfacing **between microcomponents and external
5
5
  frameworks or native DOM/JS environments**. It is not intended for communication between
@@ -99,6 +99,24 @@ export function trueEventName(target, eventName) {
99
99
  return lower;
100
100
  return '_mc' + eventName[0].toUpperCase() + eventName.substring(1);
101
101
  }
102
+ /**
103
+ Removes a previously attached event handler from the DOM node. See {@link on} and {@link one}.
104
+
105
+ Uses internal microcomponent-prefixed naming for event handlers.
106
+ This is primarily used to interface with external frameworks or vanilla JS environments.
107
+ For communication between frontend microcomponents, use the `DC.off()` method instead.
108
+
109
+ @param target - The DOM element from which to remove the listener.
110
+ @param eventName - The name of the event (e.g., 'click', 'update').
111
+ @param handler - The listener function or object to detach.
112
+
113
+ @example
114
+ ```ts
115
+ const handler = on(myElement, 'click', () => console.log('clicked'));
116
+ off(myElement, 'click', handler); // removes the listener
117
+ ```
118
+ @group Events
119
+ */
102
120
  export function off(target, eventName, handler) {
103
121
  /* v8 ignore next */
104
122
  if (!target)
@@ -113,6 +131,30 @@ export function off(target, eventName, handler) {
113
131
  // the cast re-widens it at the DOM boundary.
114
132
  target.removeEventListener(trueEventName(target, eventName), handler);
115
133
  }
134
+ /**
135
+ Attaches a persistent event listener that will fire every time the specified event occurs.
136
+
137
+ This is primarily used to interface with external frameworks or vanilla JS environments.
138
+ For communication between frontend microcomponents, use the `DC.on()` method instead.
139
+
140
+ The handler receives a `CustomEvent<McDetail<D>>`: the payload sits under
141
+ `ev.detail.data`, the bubble-control handle under `ev.detail.control`.
142
+
143
+ @param target - The DOM element to attach the listener to.
144
+ @param eventName - The name of the event (e.g., 'click', 'update').
145
+ @param handler - The callback function or object to invoke on each event.
146
+ @param once - If true, remove the listener after first invocation.
147
+ @returns The attached handler reference. Useful for later deletion with {@link off}.
148
+
149
+ @example
150
+ ```ts
151
+ const handler = on<{ value: number }>(myElement, 'update', (ev) => {
152
+ console.log(ev.detail.data.value); // number
153
+ });
154
+ // to remove: off(myElement, 'update', handler);
155
+ ```
156
+ @group Events
157
+ */
116
158
  export function on(target, eventName, handler, once = false) {
117
159
  /* v8 ignore next */
118
160
  if (!target)
@@ -129,6 +171,26 @@ export function on(target, eventName, handler, once = false) {
129
171
  });
130
172
  return handler;
131
173
  }
174
+ /**
175
+ Attaches an event listener that will fire only once.
176
+
177
+ Internally calls {@link on} with the `once` flag set to true. The handler
178
+ is automatically removed after its first invocation.
179
+ This is primarily used to interface with external frameworks or vanilla JS environments.
180
+ For communication between frontend microcomponents, use the `DC.one()` method instead.
181
+
182
+ @param target - The DOM element to attach the listener to.
183
+ @param eventName - The name of the event (e.g., 'click', 'update').
184
+ @param handler - The callback function or object to invoke once.
185
+ @returns The attached one-time handler reference. Useful for later deletion with {@link off}.
186
+
187
+ @example
188
+ ```ts
189
+ one<{ ready: boolean }>(myElement, 'dataReady', (ev) => console.log('received:', ev.detail.data));
190
+ // fires only once then detaches automatically
191
+ ```
192
+ @group Events
193
+ */
132
194
  export function one(target, eventName, handler) {
133
195
  /* v8 ignore next */
134
196
  if (!target)
@@ -216,7 +216,7 @@ export declare function injectCSS(target: Document | ShadowRoot, css: string): C
216
216
  * Recognized keys: `'css'`, `'targetCss'` (document-level styles), `'shadowCss'`
217
217
  * (shadow-root styles), `'shadowHtml'` (shadow innerHTML), `'targetHtml'` (target innerHTML).
218
218
  *
219
- * @param target - The HTMLElement to apply assets onto.
219
+ * @param instance - The micro component to apply assets onto.
220
220
  * @param assets - A record mapping asset type keys to their string content.
221
221
  *
222
222
  * @example
@@ -253,28 +253,29 @@ export declare function processAssets(instance: McController, assets: Record<str
253
253
  * });
254
254
  * ```
255
255
  */
256
- export declare function loadAssets(name: string, assets: string[]): Promise<string[]>;
256
+ export declare function loadAssets(name: string, assets: string[]): Promise<Array<any>>;
257
257
  /**
258
258
  * Binds declarative event attributes in a template fragment to microcomponent triggers.
259
259
  *
260
260
  * Scans all elements under `root` for attributes whose names correspond to native DOM
261
261
  * events (e.g. `click`, `submit`, `change`). The attribute value encodes the target and
262
- * handler method in the form `target#methodName`:
262
+ * handler method in the form `target|methodName`:
263
263
  *
264
- * - `this#myHandler` — triggers locally on the component attached to this element.
265
- * - `#other-comp#submit` — resolves elements via `querySelectorAll('#other-comp')` and
264
+ * - `this|myHandler` — triggers locally on the component attached to this element.
265
+ * - `other-comp|submit` — resolves elements via `querySelectorAll('other-comp')` and
266
266
  * triggers the event on each matched host's `_mc`.
267
267
  *
268
268
  * An optional sibling `payload` attribute supplies the data argument for `trigger()`.
269
269
  * After binding, both the event attribute and the payload attribute are removed from the DOM.
270
270
  *
271
- * @param root - The container element whose descendants should be scanned for bindings.
271
+ * @param rootElement - The container element whose descendants should be scanned for bindings.
272
+ * @param instance - The instance to trigger events on.
272
273
  *
273
274
  * @example
274
275
  * ```html
275
- * <button click="this#onSubmit" payload='{"action":"go"}'>Submit</button>
276
- * <input change="#cart-form#refresh">
276
+ * <button click='this|onSubmit|{"action":"go"}'>Submit</button>
277
+ * <input change="|cart-form|refresh">
277
278
  * ```
278
279
  */
279
- export declare function bindTemplateEvents(root: HTMLElement | ShadowRoot, instance: McController): void;
280
+ export declare function bindTemplateEvents(rootElement: HTMLElement | ShadowRoot, instance: McController): void;
280
281
  //# sourceMappingURL=helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/utils/helpers.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,OAAO,KAAK,EAAE,YAAY,EAAU,MAAM,qBAAqB,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,IAAI,OAAO,CAEnC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE/E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAiB1E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAA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iC9E;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,WAAW,GAAG,UAAU,EAAE,QAAQ,EAAE,YAAY,GAAG,IAAI,CA2CtG"}
@@ -362,7 +362,7 @@ export function injectCSS(target, css) {
362
362
  * Recognized keys: `'css'`, `'targetCss'` (document-level styles), `'shadowCss'`
363
363
  * (shadow-root styles), `'shadowHtml'` (shadow innerHTML), `'targetHtml'` (target innerHTML).
364
364
  *
365
- * @param target - The HTMLElement to apply assets onto.
365
+ * @param instance - The micro component to apply assets onto.
366
366
  * @param assets - A record mapping asset type keys to their string content.
367
367
  *
368
368
  * @example
@@ -444,6 +444,16 @@ export function loadAssets(name, assets) {
444
444
  if (!resp.ok) {
445
445
  return `file not found: ${filename}`;
446
446
  }
447
+ if (filename.endsWith('json')) {
448
+ const text = await resp.text();
449
+ try {
450
+ return JSON.parse(text);
451
+ }
452
+ catch {
453
+ console.error(`could not JSON.parse() asset '${asset}' from ${filename}:\n`, text);
454
+ return null;
455
+ }
456
+ }
447
457
  return resp.text();
448
458
  }
449
459
  catch {
@@ -456,38 +466,42 @@ export function loadAssets(name, assets) {
456
466
  *
457
467
  * Scans all elements under `root` for attributes whose names correspond to native DOM
458
468
  * events (e.g. `click`, `submit`, `change`). The attribute value encodes the target and
459
- * handler method in the form `target#methodName`:
469
+ * handler method in the form `target|methodName`:
460
470
  *
461
- * - `this#myHandler` — triggers locally on the component attached to this element.
462
- * - `#other-comp#submit` — resolves elements via `querySelectorAll('#other-comp')` and
471
+ * - `this|myHandler` — triggers locally on the component attached to this element.
472
+ * - `other-comp|submit` — resolves elements via `querySelectorAll('other-comp')` and
463
473
  * triggers the event on each matched host's `_mc`.
464
474
  *
465
475
  * An optional sibling `payload` attribute supplies the data argument for `trigger()`.
466
476
  * After binding, both the event attribute and the payload attribute are removed from the DOM.
467
477
  *
468
- * @param root - The container element whose descendants should be scanned for bindings.
478
+ * @param rootElement - The container element whose descendants should be scanned for bindings.
479
+ * @param instance - The instance to trigger events on.
469
480
  *
470
481
  * @example
471
482
  * ```html
472
- * <button click="this#onSubmit" payload='{"action":"go"}'>Submit</button>
473
- * <input change="#cart-form#refresh">
483
+ * <button click='this|onSubmit|{"action":"go"}'>Submit</button>
484
+ * <input change="|cart-form|refresh">
474
485
  * ```
475
486
  */
476
- export function bindTemplateEvents(root, instance) {
487
+ export function bindTemplateEvents(rootElement, instance) {
477
488
  // Collect all elements that carry at least one attribute matching a native event name.
478
- const allElements = root.querySelectorAll('*');
489
+ const allElements = [...rootElement.querySelectorAll('*')];
479
490
  for (const el of allElements) {
480
491
  if (!(el instanceof HTMLElement))
481
492
  continue;
482
- const attrs = Array.from(el.attributes);
493
+ const attrs = [...el.attributes];
483
494
  for (const attr of attrs) {
484
495
  const lowerName = attr.name.toLowerCase();
485
496
  // Check if this attribute name maps to a native event handler property.
486
497
  if (!('on' + lowerName in el))
487
498
  continue;
488
- const [selector, event, payload] = attr.value.trim().split('#');
489
- if (!event)
499
+ const parts = attr.value.trim().split('|');
500
+ if (parts.length < 2)
490
501
  continue; // need at least selector and eventname
502
+ const [selector, event, payload] = parts;
503
+ // Clean up binding attributes from the DOM.
504
+ el.removeAttribute(attr.name);
491
505
  // Resolve payload from sibling attribute if present.
492
506
  let payloadData; // intentional
493
507
  if (payload) {
@@ -507,8 +521,6 @@ export function bindTemplateEvents(root, instance) {
507
521
  // Selector-based trigger — query and fire on every match.
508
522
  on(el, lowerName, () => trigger(selector, event, payloadData));
509
523
  }
510
- // Clean up binding attributes from the DOM.
511
- el.removeAttribute(attr.name);
512
524
  }
513
525
  }
514
526
  }
@@ -0,0 +1,17 @@
1
+ /**
2
+ Utils module — events, autoload, and helper utilities.
3
+
4
+ @module Utilities
5
+ @showGroups
6
+ */
7
+ /** @group Events */
8
+ export { getMcEventName, trueEventName, off, on, one, trigger, BubbleController } from './events.js';
9
+ /** @hidden */
10
+ export type { QuerySelectorString, TargetSelect } from './events.js';
11
+ /** @group Autoload */
12
+ export { autoload } from './autoload.js';
13
+ /** @hidden */
14
+ export type { Autoload } from './autoload.js';
15
+ /** @group Helpers */
16
+ export { isBrowser, isNativeObject, sortObject, flattenObject, deepEqual, nameSpace, parse, kebabToPascal, debounce, htmlToElements, copyGettersSetters, injectCSS, processAssets, loadAssets, bindTemplateEvents } from './helpers.js';
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;EAKE;AAEF,oBAAoB;AACpB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrG,cAAc;AACd,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACrE,sBAAsB;AACtB,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,cAAc;AACd,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,qBAAqB;AACrB,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,UAAU,EAAE,aAAa,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,cAAc,EAAE,kBAAkB,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ Utils module — events, autoload, and helper utilities.
3
+
4
+ @module Utilities
5
+ @showGroups
6
+ */
7
+ /** @group Events */
8
+ export { getMcEventName, trueEventName, off, on, one, trigger, BubbleController } from './events.js';
9
+ /** @group Autoload */
10
+ export { autoload } from './autoload.js';
11
+ /** @group Helpers */
12
+ export { isBrowser, isNativeObject, sortObject, flattenObject, deepEqual, nameSpace, parse, kebabToPascal, debounce, htmlToElements, copyGettersSetters, injectCSS, processAssets, loadAssets, bindTemplateEvents } from './helpers.js';
package/package.json CHANGED
@@ -1,19 +1,17 @@
1
1
  {
2
2
  "name": "@twobirds/microcomponents",
3
- "version": "0.2.1",
4
- "description": "Micro Components Organization Class",
3
+ "version": "0.2.3",
4
+ "description": "Micro Components",
5
5
  "keywords": [
6
- "API",
7
- "DOM manipulation",
8
- "ES2015",
9
- "ES2016",
10
- "ES6",
11
- "ES7",
6
+ "custom elements",
7
+ "web components",
8
+ "javascript",
9
+ "typescript",
12
10
  "PWA",
13
11
  "SPA",
14
12
  "client",
15
- "javascript",
16
13
  "library",
14
+ "framework",
17
15
  "lightweight",
18
16
  "native",
19
17
  "standalone"
@@ -72,20 +70,20 @@
72
70
  "format": "oxfmt .",
73
71
  "lint": "oxlint \"src\"",
74
72
  "fix": "oxfmt . && oxlint \"src\" --fix",
75
- "check": "tsc --noEmit -p tsconfig.lib.json && tsc -p tsconfig.test.json && tsc -p tsconfig.demo.json && tsc -p tsconfig.node.json",
73
+ "check": "tsc --noEmit -p tsconfig.lib.json && tsc -p tsconfig.test.json && tsc -p demo/web-test/tsconfig.demo.json && tsc -p tsconfig.node.json",
76
74
  "check:pack": "publint && attw --pack . --profile esm-only",
77
75
  "test": "vitest",
78
76
  "test:run": "vitest run --reporter=verbose",
79
77
  "test:coverage": "vitest run --coverage",
80
78
  "demo:shop": "vite --config demo/shop/vite.config.ts",
81
- "demo:shop:bundled": "tsx vite.shop.bundle.ts",
82
- "demo:shop:build": "tsx vite.shop.ts && pnpm run demo:shop:bundled",
79
+ "demo:shop:bundled": "tsx demo/shop/vite.shop.bundle.ts",
80
+ "demo:shop:build": "tsx demo/shop/vite.shop.ts && pnpm run demo:shop:bundled",
83
81
  "demo:shop:serve": "vite preview --outDir public/shop --port 5174 --host",
84
- "typedoc": "typedoc",
82
+ "typedoc": "typedoc --displayBasePath ./typedoc/",
85
83
  "bundle": "tsx vite.build.ts",
86
84
  "clean": "rm -rf dist public build",
87
85
  "build": "pnpm run clean && tsc -p tsconfig.lib.json && pnpm run test:coverage && pnpm run bundle && pnpm run check:pack && pnpm run typedoc && pnpm run demo:build && pnpm run demo:shop:build && find src -name '*.js' -delete",
88
- "demo:build": "tsx vite.demo.ts",
86
+ "demo:build": "tsx demo/web-test/vite.demo.ts",
89
87
  "dev": "vite preview --outDir public/test --port 10000 --host",
90
88
  "doc": "vite preview --outDir public/typedoc --port 11000 --host",
91
89
  "watch": "tsx watch.ts",
@@ -108,5 +106,5 @@
108
106
  "engines": {
109
107
  "node": ">=22.18"
110
108
  },
111
- "packageManager": "pnpm@11.15.1"
109
+ "packageManager": "pnpm@12.4.1"
112
110
  }