@twobirds/microcomponents 0.2.1 → 0.2.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.
@@ -253,16 +253,16 @@ 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()`.
@@ -272,9 +272,9 @@ export declare function loadAssets(name: string, assets: string[]): Promise<stri
272
272
  *
273
273
  * @example
274
274
  * ```html
275
- * <button click="this#onSubmit" payload='{"action":"go"}'>Submit</button>
276
- * <input change="#cart-form#refresh">
275
+ * <button click='this|onSubmit|{"action":"go"}'>Submit</button>
276
+ * <input change="|cart-form|refresh">
277
277
  * ```
278
278
  */
279
- export declare function bindTemplateEvents(root: HTMLElement | ShadowRoot, instance: McController): void;
279
+ export declare function bindTemplateEvents(rootElement: HTMLElement | ShadowRoot, instance: McController): void;
280
280
  //# 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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gB,SAAS,CAAC,MAAM,EAAE,QAAQ,GAAG,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,aAAa,CAOnF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CA4B1F;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAiC9E;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,WAAW,GAAG,UAAU,EAAE,QAAQ,EAAE,YAAY,GAAG,IAAI,CA2CtG"}
@@ -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,10 +466,10 @@ 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()`.
@@ -469,25 +479,28 @@ export function loadAssets(name, assets) {
469
479
  *
470
480
  * @example
471
481
  * ```html
472
- * <button click="this#onSubmit" payload='{"action":"go"}'>Submit</button>
473
- * <input change="#cart-form#refresh">
482
+ * <button click='this|onSubmit|{"action":"go"}'>Submit</button>
483
+ * <input change="|cart-form|refresh">
474
484
  * ```
475
485
  */
476
- export function bindTemplateEvents(root, instance) {
486
+ export function bindTemplateEvents(rootElement, instance) {
477
487
  // Collect all elements that carry at least one attribute matching a native event name.
478
- const allElements = root.querySelectorAll('*');
488
+ const allElements = [...rootElement.querySelectorAll('*')];
479
489
  for (const el of allElements) {
480
490
  if (!(el instanceof HTMLElement))
481
491
  continue;
482
- const attrs = Array.from(el.attributes);
492
+ const attrs = [...el.attributes];
483
493
  for (const attr of attrs) {
484
494
  const lowerName = attr.name.toLowerCase();
485
495
  // Check if this attribute name maps to a native event handler property.
486
496
  if (!('on' + lowerName in el))
487
497
  continue;
488
- const [selector, event, payload] = attr.value.trim().split('#');
489
- if (!event)
498
+ const parts = attr.value.trim().split('|');
499
+ if (parts.length < 2)
490
500
  continue; // need at least selector and eventname
501
+ const [selector, event, payload] = parts;
502
+ // Clean up binding attributes from the DOM.
503
+ el.removeAttribute(attr.name);
491
504
  // Resolve payload from sibling attribute if present.
492
505
  let payloadData; // intentional
493
506
  if (payload) {
@@ -507,8 +520,6 @@ export function bindTemplateEvents(root, instance) {
507
520
  // Selector-based trigger — query and fire on every match.
508
521
  on(el, lowerName, () => trigger(selector, event, payloadData));
509
522
  }
510
- // Clean up binding attributes from the DOM.
511
- el.removeAttribute(attr.name);
512
523
  }
513
524
  }
514
525
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twobirds/microcomponents",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Micro Components Organization Class",
5
5
  "keywords": [
6
6
  "API",
@@ -72,20 +72,20 @@
72
72
  "format": "oxfmt .",
73
73
  "lint": "oxlint \"src\"",
74
74
  "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",
75
+ "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
76
  "check:pack": "publint && attw --pack . --profile esm-only",
77
77
  "test": "vitest",
78
78
  "test:run": "vitest run --reporter=verbose",
79
79
  "test:coverage": "vitest run --coverage",
80
80
  "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",
81
+ "demo:shop:bundled": "tsx demo/shop/vite.shop.bundle.ts",
82
+ "demo:shop:build": "tsx demo/shop/vite.shop.ts && pnpm run demo:shop:bundled",
83
83
  "demo:shop:serve": "vite preview --outDir public/shop --port 5174 --host",
84
84
  "typedoc": "typedoc",
85
85
  "bundle": "tsx vite.build.ts",
86
86
  "clean": "rm -rf dist public build",
87
87
  "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",
88
+ "demo:build": "tsx demo/web-test/vite.demo.ts",
89
89
  "dev": "vite preview --outDir public/test --port 10000 --host",
90
90
  "doc": "vite preview --outDir public/typedoc --port 11000 --host",
91
91
  "watch": "tsx watch.ts",