@twobirds/microcomponents 0.0.66 → 0.0.68

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 (202) hide show
  1. package/.vscode/settings.json +1 -1
  2. package/README.md +11 -7
  3. package/assets/documentation/formvalues.md +26 -0
  4. package/assets/{test.html → test/test.html} +7 -7
  5. package/dist/microcomponents.d.ts +0 -1
  6. package/dist/microcomponents.d.ts.map +1 -1
  7. package/dist/microcomponents.min.js +1 -1
  8. package/dist/microcomponents.ts +1 -1
  9. package/package.json +5 -4
  10. package/public/MC.d.ts +36 -0
  11. package/public/MC.d.ts.map +1 -0
  12. package/public/MC.ts +605 -0
  13. package/public/McEvent.d.ts +15 -0
  14. package/public/McEvent.d.ts.map +1 -0
  15. package/public/McEvent.ts +96 -0
  16. package/public/MicroComponents.svg +15 -0
  17. package/public/documentation/MC.md +588 -0
  18. package/public/documentation/ajaxian-1.png +0 -0
  19. package/public/documentation/ajaxian-2.png +0 -0
  20. package/public/documentation/ajaxian-3.png +0 -0
  21. package/public/documentation/elements.md +278 -0
  22. package/public/documentation/formvalues.md +26 -0
  23. package/public/documentation/functional_requirements.md +8 -0
  24. package/public/documentation/history.md +436 -0
  25. package/public/documentation/magic.md +29 -0
  26. package/public/documentation/observables.md +107 -0
  27. package/public/documentation/rnd0023-1.jpg +0 -0
  28. package/public/documentation/rnd0023-2.jpg +0 -0
  29. package/public/documentation/rnd0023-3.jpg +0 -0
  30. package/public/documentation/tB_react.md +207 -0
  31. package/public/documentation/usage.md +142 -0
  32. package/public/documentation/why.md +25 -0
  33. package/public/elements.d.ts +5 -0
  34. package/public/elements.d.ts.map +1 -0
  35. package/public/elements.ts +76 -0
  36. package/public/fiddle/MicroComponents.svg +15 -0
  37. package/public/fiddle/fiddle.css +0 -0
  38. package/public/fiddle/fiddle.js +0 -0
  39. package/public/fiddle/index.html +0 -0
  40. package/public/formValues.d.ts +3 -0
  41. package/public/formValues.d.ts.map +1 -0
  42. package/public/formValues.ts +233 -0
  43. package/public/helpers.d.ts +14 -0
  44. package/public/helpers.d.ts.map +1 -0
  45. package/public/helpers.ts +149 -0
  46. package/public/microcomponents.d.ts +7 -0
  47. package/public/microcomponents.d.ts.map +1 -0
  48. package/public/microcomponents.es3.d.ts +2 -0
  49. package/public/microcomponents.es3.d.ts.map +1 -0
  50. package/public/microcomponents.js +0 -1
  51. package/public/microcomponents.js.map +1 -1
  52. package/public/microcomponents.min.js +1 -1
  53. package/public/microcomponents.ts +8 -0
  54. package/public/observables.d.ts +10 -0
  55. package/public/observables.d.ts.map +1 -0
  56. package/public/observables.ts +401 -0
  57. package/public/selectors.d.ts +108 -0
  58. package/public/selectors.d.ts.map +1 -0
  59. package/public/selectors.ts +1634 -0
  60. package/public/src/MC.ts +605 -0
  61. package/public/src/McEvent.ts +96 -0
  62. package/public/src/elements.ts +76 -0
  63. package/public/src/formValues.ts +233 -0
  64. package/public/src/helpers.ts +149 -0
  65. package/public/src/microcomponents.ts +8 -0
  66. package/public/src/observables.ts +401 -0
  67. package/public/src/selectors.ts +1634 -0
  68. package/public/src/test.ts +193 -0
  69. package/public/test/MC.d.ts +36 -0
  70. package/public/test/MC.d.ts.map +1 -0
  71. package/public/test/MC.js +378 -0
  72. package/public/test/MC.js.map +1 -0
  73. package/public/test/MC.ts +605 -0
  74. package/public/test/McEvent.d.ts +15 -0
  75. package/public/test/McEvent.d.ts.map +1 -0
  76. package/public/test/McEvent.js +71 -0
  77. package/public/test/McEvent.js.map +1 -0
  78. package/public/test/McEvent.ts +96 -0
  79. package/public/test/MicroComponents.svg +15 -0
  80. package/public/test/elements.d.ts +5 -0
  81. package/public/test/elements.d.ts.map +1 -0
  82. package/public/test/elements.js +48 -0
  83. package/public/test/elements.js.map +1 -0
  84. package/public/test/elements.ts +76 -0
  85. package/public/test/formValues.d.ts +3 -0
  86. package/public/test/formValues.d.ts.map +1 -0
  87. package/public/test/formValues.js +168 -0
  88. package/public/test/formValues.js.map +1 -0
  89. package/public/test/formValues.ts +233 -0
  90. package/public/test/grandparent.css +13 -0
  91. package/public/test/helpers.d.ts +14 -0
  92. package/public/test/helpers.d.ts.map +1 -0
  93. package/public/test/helpers.js +113 -0
  94. package/public/test/helpers.js.map +1 -0
  95. package/public/test/helpers.ts +149 -0
  96. package/public/test/microcomponents.d.ts +7 -0
  97. package/public/test/microcomponents.d.ts.map +1 -0
  98. package/public/test/microcomponents.es3.d.ts +2 -0
  99. package/public/test/microcomponents.es3.d.ts.map +1 -0
  100. package/public/test/microcomponents.es3.js +303 -0
  101. package/public/test/microcomponents.es3.js.map +1 -0
  102. package/public/test/microcomponents.js +8 -0
  103. package/public/test/microcomponents.js.map +1 -0
  104. package/public/test/microcomponents.min.js +1 -0
  105. package/public/test/microcomponents.ts +8 -0
  106. package/public/test/my-tag.css +19 -0
  107. package/public/test/observables.d.ts +10 -0
  108. package/public/test/observables.d.ts.map +1 -0
  109. package/public/test/observables.js +250 -0
  110. package/public/test/observables.js.map +1 -0
  111. package/public/test/observables.ts +401 -0
  112. package/public/test/selectors.d.ts +108 -0
  113. package/public/test/selectors.d.ts.map +1 -0
  114. package/public/test/selectors.js +971 -0
  115. package/public/test/selectors.js.map +1 -0
  116. package/public/test/selectors.ts +1634 -0
  117. package/public/test/test/child.d.ts +2 -0
  118. package/public/test/test/child.d.ts.map +1 -0
  119. package/public/test/test/child.js +15 -0
  120. package/public/test/test/child.js.map +1 -0
  121. package/public/test/test/grandchild.d.ts +2 -0
  122. package/public/test/test/grandchild.d.ts.map +1 -0
  123. package/public/test/test/grandchild.js +15 -0
  124. package/public/test/test/grandchild.js.map +1 -0
  125. package/public/test/test/grandparent.d.ts +2 -0
  126. package/public/test/test/grandparent.d.ts.map +1 -0
  127. package/public/test/test/grandparent.js +1 -0
  128. package/public/test/test/grandparent.js.map +1 -0
  129. package/public/test/test/greatgrandchild.d.ts +2 -0
  130. package/public/test/test/greatgrandchild.d.ts.map +1 -0
  131. package/public/test/test/greatgrandchild.js +29 -0
  132. package/public/test/test/greatgrandchild.js.map +1 -0
  133. package/public/test/test/parent.d.ts +2 -0
  134. package/public/test/test/parent.d.ts.map +1 -0
  135. package/public/test/test/parent.js +15 -0
  136. package/public/test/test/parent.js.map +1 -0
  137. package/public/test/test/testform.d.ts +2 -0
  138. package/public/test/test/testform.d.ts.map +1 -0
  139. package/public/test/test/testform.js +1 -0
  140. package/public/test/test/testform.js.map +1 -0
  141. package/public/test/test/testmc.d.ts +6 -0
  142. package/public/test/test/testmc.d.ts.map +1 -0
  143. package/public/test/test/testmc.js +10 -0
  144. package/public/test/test/testmc.js.map +1 -0
  145. package/public/test/test.d.ts +2 -0
  146. package/public/test/test.d.ts.map +1 -0
  147. package/public/test/test.html +27 -0
  148. package/public/test/test.ts +193 -0
  149. package/public/typedoc/.nojekyll +1 -0
  150. package/public/{classes → typedoc/classes}/MC.DC.html +2 -2
  151. package/public/{classes → typedoc/classes}/MC.MC.html +2 -2
  152. package/public/{classes → typedoc/classes}/MC.McBase.html +2 -2
  153. package/public/{classes → typedoc/classes}/MC.McEvent.html +2 -2
  154. package/public/{functions → typedoc/functions}/MC.addListener.html +1 -1
  155. package/public/{functions → typedoc/functions}/MC.autoload.html +1 -1
  156. package/public/{functions → typedoc/functions}/MC.removeListener.html +1 -1
  157. package/public/{functions → typedoc/functions}/elements.defineCE.html +1 -1
  158. package/public/{functions → typedoc/functions}/elements.getCE.html +1 -1
  159. package/public/{functions → typedoc/functions}/formValues.formValues.html +1 -1
  160. package/public/{functions → typedoc/functions}/observables.observe.html +1 -1
  161. package/public/{functions → typedoc/functions}/selectors._d.html +1 -1
  162. package/public/{functions → typedoc/functions}/selectors._t.html +1 -1
  163. package/public/{index.html → typedoc/index.html} +9 -7
  164. package/public/{interfaces → typedoc/interfaces}/MC.HTMLMcElement.html +2 -2
  165. package/public/{interfaces → typedoc/interfaces}/selectors.DSelector.html +2 -2
  166. package/public/{interfaces → typedoc/interfaces}/selectors.TSelector.html +2 -2
  167. package/public/typedoc/media/MicroComponents.svg +15 -0
  168. package/public/{modules → typedoc/modules}/MC.html +1 -1
  169. package/public/{modules → typedoc/modules}/elements.html +1 -1
  170. package/public/{modules → typedoc/modules}/formValues.html +1 -1
  171. package/public/{modules → typedoc/modules}/observables.html +1 -1
  172. package/public/{modules → typedoc/modules}/selectors.html +1 -1
  173. package/public/{types → typedoc/types}/observables.Observable.html +1 -1
  174. package/public/{types → typedoc/types}/selectors.selection.html +1 -1
  175. package/public/{types → typedoc/types}/selectors.singleSelector.html +1 -1
  176. package/src/microcomponents.ts +1 -1
  177. package/src/test/{child.ts → test/child.ts} +1 -1
  178. package/src/test/{grandchild.ts → test/grandchild.ts} +1 -1
  179. package/src/test/{grandparent.ts → test/grandparent.ts} +1 -1
  180. package/src/test/{greatgrandchild.ts → test/greatgrandchild.ts} +1 -1
  181. package/src/test/{parent.ts → test/parent.ts} +1 -1
  182. package/src/test/{testform.ts → test/testform.ts} +1 -1
  183. package/src/test/test/testmc.ts +12 -0
  184. package/tsconfig.json +2 -2
  185. package/typedoc.json +1 -1
  186. package/webpack.config.js +4 -4
  187. package/assets/3d.html +0 -42
  188. package/src/test/testmc.ts +0 -18
  189. /package/{public/media → assets/test}/MicroComponents.svg +0 -0
  190. /package/assets/{grandparent.css → test/grandparent.css} +0 -0
  191. /package/assets/{my-tag.css → test/my-tag.css} +0 -0
  192. /package/public/{test.js → test/test.js} +0 -0
  193. /package/public/{test.js.map → test/test.js.map} +0 -0
  194. /package/public/{assets → typedoc/assets}/highlight.css +0 -0
  195. /package/public/{assets → typedoc/assets}/icons.js +0 -0
  196. /package/public/{assets → typedoc/assets}/icons.svg +0 -0
  197. /package/public/{assets → typedoc/assets}/main.js +0 -0
  198. /package/public/{assets → typedoc/assets}/navigation.js +0 -0
  199. /package/public/{assets → typedoc/assets}/search.js +0 -0
  200. /package/public/{assets → typedoc/assets}/style.css +0 -0
  201. /package/public/{hierarchy.html → typedoc/hierarchy.html} +0 -0
  202. /package/public/{modules.html → typedoc/modules.html} +0 -0
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ var isBrowser=new Function("try {return this===window;}catch(e){ return false;}");
4
+
5
+ let nativeEventNames: Set<string> = new Set();
6
+
7
+ // HTMLElement native event names
8
+ if ( isBrowser() ){
9
+ Object.keys( HTMLElement.prototype ).filter( key => /^on/.test(key) ).forEach( eventName => nativeEventNames.add( eventName ) );
10
+ }
11
+
12
+ function isDOM(element: { [key:string]: any } ) {
13
+ return element && element instanceof HTMLElement;
14
+ }
15
+
16
+ interface McEvent {
17
+ data: any;
18
+ bubble: string;
19
+ }
20
+
21
+ class McEvent{
22
+
23
+ #type: string;
24
+ #stopped: boolean = false;
25
+ #immediateStopped: boolean = false;
26
+
27
+ constructor( type: string, data: any = {}, bubble: string = 'l' ){
28
+ // bubble standard = local only, options = any combination of'uld' characters: up to root, local, down
29
+ this.#type = type;
30
+ this.data = data;
31
+ this.bubble = bubble;
32
+ }
33
+
34
+ get type(): string {
35
+ return this.#type;
36
+ }
37
+
38
+ get stopped(): boolean {
39
+ return this.#stopped;
40
+ }
41
+
42
+ get immediateStopped(): boolean {
43
+ return this.#immediateStopped;
44
+ }
45
+
46
+ stopPropagation(){
47
+ this.#stopped = true;
48
+ }
49
+
50
+ stopImmediatePropagation(){
51
+ this.stopPropagation();
52
+ this.#immediateStopped = true;
53
+ }
54
+
55
+ }
56
+
57
+ // get all microcomponents from a list of HTMLElements
58
+ function getMicroComponents( elements: any[], search: string): any[] {
59
+ let mcValues: any[] = [];
60
+
61
+ elements.forEach( e => {
62
+ if (e) {
63
+ if (e instanceof HTMLElement) {
64
+ const _mc = ( e as any )?._mc;
65
+ if ( !search && _mc ) {
66
+ mcValues = mcValues.concat(Object.values(( e as any )._mc));
67
+ }
68
+ else if ( search && _mc && _mc[search] ) {
69
+ mcValues.push( _mc[search] );
70
+ }
71
+ }
72
+ }
73
+ });
74
+ return mcValues;
75
+ }
76
+
77
+ // get all ancestor DOM nodes up to the document root
78
+ function traverseNodes(node: { [key:string]: any }, search: string, firstonly = true): any[] {
79
+ let mcValues: any[] = [];
80
+ while (node) {
81
+ if (node._mc) {
82
+ if (!search) {
83
+ mcValues = mcValues.concat(Object.values(node._mc));
84
+ }
85
+ else if (search && node._mc[search]) {
86
+ mcValues.push(node._mc[search]);
87
+ }
88
+ if (firstonly)
89
+ return mcValues;
90
+ }
91
+ node = node.parentNode;
92
+ }
93
+ return mcValues;
94
+ }
95
+
96
+ export { McEvent };
@@ -0,0 +1,15 @@
1
+ <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
2
+ <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
3
+ <title>test</title>
4
+ <rect fill="transparent" x="0" y="0" width="200" height="200" fill-opacity="1"/>
5
+ <g style="fill:transparent">
6
+ <title>first bird</title>
7
+ <path fill="#18829c" d="
8
+ m15,160
9
+ a30,75 45 1 0 160,-150
10
+ a20,50 225 1 1 -110,100
11
+ a12,25 45 1 1 60,-50
12
+ a20,50 225 1 0 -110,100
13
+ " />
14
+ </g>
15
+ </svg>
@@ -0,0 +1,588 @@
1
+ ![logo](./MicroComponents.svg)
2
+
3
+ # Micro Component Documentation
4
+
5
+ This document details a lightweight JavaScript framework for creating and managing "microcomponents" (MCs) associated with DOM elements or other microcomponents. It allows you to define reusable components and attach them to build complex functionalities.
6
+
7
+ The framework exports 2 main classes:
8
+
9
+ - **MC**: Micro Component. The base component class, suitable for logic that doesn't directly interact with the DOM or for use on the server.
10
+ - **DC**: DOM Component. Extends `MC` and adds DOM-specific methods (like traversal). Use this for components that need to interact with the browser's DOM.
11
+
12
+ ## Table of Contents
13
+
14
+ - [Usage Guidance](#usage-guidance)
15
+ - [Core Concepts](#core-concepts)
16
+ - [Microcomponents (MCs)](#microcomponents-mcs)
17
+ - [`_mc` Property](#_mc-property)
18
+ - [DOM Association & `target`](#dom-association--target)
19
+ - [Event System](#event-system)
20
+ - [Observables (Mention)](#observables-mention)
21
+ - [DOM Traversal](#dom-traversal)
22
+ - [Autoloading Concept](#autoloading-concept)
23
+ - [Important Rule](#important-rule)
24
+ - [Classes](#classes)
25
+ - [Not Exported: `McBase` (Superclass)](#not-exported-mcbase-superclass)
26
+ - [Exported: `MC` Class](#exported-mc-class)
27
+ - [Exported: `DC` Class](#exported-dc-class)
28
+ - [Examples](#examples)
29
+ - [Typical Workflow](#typical-workflow)
30
+ - [Attach Micro Components (`DC.add`)](#attach-micro-components-dcadd)
31
+ - [Remove Micro Components (`DC.remove`)](#remove-micro-components-dcremove)
32
+ - [Retrieve Micro Components](#retrieve-micro-components)
33
+ - [Triggering Events](#triggering-events)
34
+ - [Retrieving Inner Micro Components](#retrieving-inner-micro-components)
35
+ - [Navigating Parent/Ancestor MCs](#navigating-parentancestor-mcs)
36
+ - [Retrieving Child/Descendant MCs](#retrieving-childdescendant-mcs)
37
+ - [Auto-attaching DOM Listeners](#auto-attaching-dom-listeners)
38
+ - [Lifecycle Events](#lifecycle-events)
39
+ - [Init](#init)
40
+ - [Interfaces and Types](#interfaces-and-types)
41
+ - [`LooseObject`](#looseobject)
42
+ - [`McEvent`](#mcevent)
43
+ - [`HTMLMcElement`](#htmlmcelement)
44
+ - [Autoloading](#auto-loading)
45
+
46
+ ## Usage Guidance
47
+
48
+ - **If your component needs to access or manipulate the DOM** (e.g., find other components in the DOM, attach listeners directly, read element properties), **your class MUST extend `DC`**. This is client-side only.
49
+ - **Otherwise** (e.g., for pure logic, state management, or server-side usage), **your class should extend `MC`**.
50
+
51
+ Think **DRY** (Don't Repeat Yourself) and **SOLID** principles when designing your components.
52
+
53
+ ## Core Concepts
54
+
55
+ Fundamental ideas behind the framework.
56
+
57
+ ### Microcomponents (MCs)
58
+
59
+ Plain JavaScript objects or class instances (extending `MC` or `DC`) that encapsulate behavior and state. They can be attached to DOM elements (via `DC`) or nested within other MCs.
60
+
61
+ ### `_mc` Property
62
+
63
+ A special property attached to `HTMLElement` instances (when using `DC.add`) or parent `MC`/`DC` instances. It acts as an object container (`{ key: mcInstance, ... }`) holding one or more named microcomponent instances.
64
+
65
+ - On HTMLElements, the presence of attached components is also marked by a `_mc` attribute for CSS selection and discovery. This attribute is added by `DC.add` and removed by `DC.remove` when the `_mc` object becomes empty.
66
+
67
+ ### DOM Association & `target`
68
+
69
+ Each microcomponent instance has a read-only `target` property providing a back-reference:
70
+
71
+ - If the instance extends **`DC`** and was added via `DC.add`, `target` refers to the **DOM HTMLElement** it's attached to.
72
+ - If the instance extends **`MC`** (or `DC`) and is nested within another component's `_mc` property, `target` refers to the **parent MC/DC instance**.
73
+
74
+ This reference is managed using `WeakRef` internally by `McBase` to prevent circular references and potential memory leaks.
75
+
76
+ ### Event System
77
+
78
+ A custom event system using the `McEvent` class allows components to communicate.
79
+
80
+ - **Triggering:** Events are dispatched using the `trigger` method:
81
+ - `DC.trigger(targetElement, ...)`: Dispatches to all MCs on a specific DOM element. Handles bubbling (`u`/`d`).
82
+ - `this.trigger(...)`: Dispatches within the current MC instance and its potential nested MCs. Can also initiate bubbling if the instance is a `DC`.
83
+ - **Handling:**
84
+ - Components listen for events by defining methods named `onEventName` or `oneEventName` (for once-off handling).
85
+ - These are automatically detected by `autoAttachListeners`.
86
+ - Native DOM events matching this pattern on `DC` instances are also automatically bound to the target element.
87
+ - **Bubbling:** Events triggered via `DC.trigger` or `dcInstance.trigger` can bubble:
88
+ - `'l'`: Local only (default). Handlers on the target element's/instance's MCs are executed.
89
+ - `'u'`: Upwards. Event propagates to MCs on parent elements (DOM tree).
90
+ - `'d'`: Downwards. Event propagates to MCs on descendant elements (DOM tree).
91
+ - Combinations are possible (e.g., `'lu'`, `'ldu'`). Bubbling (`u`/`d`) occurs asynchronously via `setTimeout`.
92
+
93
+ ### Observables (Mention)
94
+
95
+ - MCs can utilize observables (implementation not detailed here) for data-driven workflows.
96
+ - `DC` components can leverage observables and potentially form values for 1-way or 2-way data binding with the DOM.
97
+
98
+ ### DOM Traversal
99
+
100
+ The `DC` class provides methods (`parent`, `ancestors`, `children`, `descendants`) to find related `DC` instances attached to elements elsewhere in the DOM hierarchy, skipping over elements without attached microcomponents.
101
+
102
+ ### Autoloading Concept
103
+
104
+ An optional feature (`autoload()`) that automatically detects specific HTML tags (e.g., `<my-component>`) or elements with an `_mc="MyComponent"` attribute and attempts to dynamically load the corresponding JavaScript module (e.g., `/my/Component.js`).
105
+
106
+ ### Important Rule
107
+
108
+ **Your own application code MUST NOT overwrite the core properties and methods provided by the `McBase`, `MC`, and `DC` classes (like `_mc`, `target`, `trigger`, `parent`, etc.). Doing so may break framework functionality.**
109
+
110
+ ## Classes
111
+
112
+ ### Not Exported: `McBase` (Superclass)
113
+
114
+ The internal base class for `MC` and `DC`. You don't extend this directly.
115
+
116
+ - **Instance Properties**:
117
+ - `.target`: Read-only getter returning the containing DOM element (for DCs added via `DC.add`) or parent MC/DC instance (for nested MCs). Uses `WeakRef`.
118
+ - **Constructor Behavior**:
119
+ - Initializes the `.target` reference.
120
+ - Calls `init(this)`: Triggers the 'Init' lifecycle event asynchronously.
121
+ - Calls `autoAttachListeners(this)`: Sets up automatic event listeners based on method names (`onEventName`, `oneEventName`).
122
+ - **Instance Methods**:
123
+ - `.trigger(eventName, data)`: Base trigger logic used by `MC` and `DC`.
124
+
125
+ ### Exported: `MC` Class
126
+
127
+ Base class for non-DOM or server-side components. Extends `McBase`.
128
+
129
+ - **Instance Properties**:
130
+ - `.target`: (Inherited from `McBase`) Refers to the parent MC/DC instance it's nested within.
131
+ - `._mc`: An object `{}` to hold nested microcomponent instances.
132
+ - **Instance Methods**:
133
+ - `.trigger(eventName, data)`: (Inherited) Triggers events locally within the instance and its nested `_mc` structure. Does not handle DOM bubbling.
134
+
135
+ ### Exported: `DC` Class
136
+
137
+ Class for DOM-aware components. Extends `McBase`. **Client-side only.**
138
+
139
+ - **Static Methods**:
140
+ - `DC.add(domElement, key, mcInstance)`: Attaches an `mcInstance` (should be a `DC` or `MC`) to a `domElement` under the specified `key`. Creates `domElement._mc` if needed and sets the `_mc` attribute.
141
+ - `DC.remove(domElement, key)`: Removes the MC instance associated with `key` from `domElement._mc`. Removes the `_mc` attribute if the collection becomes empty.
142
+ - `DC.trigger(domElement, eventName, data, bubble)`: Triggers an event on all MCs attached to `domElement`. Handles DOM bubbling (`'l'`, `'u'`, `'d'`).
143
+ - **Instance Properties**:
144
+ - `.target`: (Inherited from `McBase`) Refers to the DOM HTMLElement this `DC` instance is attached to (via `DC.add`).
145
+ - `._mc`: An object `{}` to hold nested microcomponent instances.
146
+ - **Instance Methods**:
147
+ - `.parent(search?)`: Gets MC instances from the closest parent element that has attached MCs. Optionally filters by `search` key.
148
+ - `.ancestors(search?)`: Gets MC instances from all ancestor elements with attached MCs. Optionally filters by `search` key.
149
+ - `.children(search?)`: Gets MC instances from direct child elements with attached MCs. Optionally filters by `search` key.
150
+ - `.descendants(search?)`: Gets MC instances from all descendant elements with attached MCs. Optionally filters by `search` key.
151
+ - `.trigger(eventName, data, bubble)`: Triggers an event on this `DC` instance and its nested `_mc` structure. Can initiate DOM bubbling (`'l'`, `'u'`, `'d'`).
152
+
153
+ ## Examples
154
+
155
+ ### Typical Workflow
156
+
157
+ 1. Define your component classes extending `MC` or `DC`.
158
+ 2. Attach top-level `DC` instances to HTMLElements using `DC.add()`.
159
+ 3. Nest `MC` or `DC` instances within others using the `._mc` property if needed.
160
+ 4. Access other components via the `_mc` property, DOM traversal methods (`ancestors()`, `children()`, etc.), or event communication (`trigger`).
161
+
162
+ ### Attach Micro Components (`DC.add`)
163
+
164
+ Use the static `DC.add()` method to attach a component (usually a `DC` instance) to an HTMLElement.
165
+
166
+ ```javascript
167
+ const myElement = document.getElementById('myDiv');
168
+
169
+ // Define an inner MC (doesn't need DOM access itself)
170
+ class MyInnerClass extends MC {
171
+ constructor(target) {
172
+ // target will be the parent MyPublicClass instance
173
+ super(target);
174
+ console.log('Inner MC target:', this.target);
175
+ }
176
+ doSomethingInner() {
177
+ console.log('Inner action!');
178
+ }
179
+ }
180
+
181
+ // Define an outer DC (needs DOM access or is top-level)
182
+ class MyPublicClass extends DC {
183
+ constructor(target) {
184
+ // target will be myElement
185
+ super(target);
186
+ console.log('Outer DC target:', this.target);
187
+ // Add an inner MC instance, nested within this DC instance
188
+ this._mc.myInner = new MyInnerClass(this); // 'this' becomes the target for MyInnerClass
189
+ }
190
+ doSomethingOuter() {
191
+ console.log('Outer action!');
192
+ }
193
+
194
+ // Example DOM event handler
195
+ onClick(event) {
196
+ console.log('Div clicked!', event);
197
+ // Access nested MC
198
+ this._mc.myInner?.doSomethingInner();
199
+ }
200
+ }
201
+
202
+ // Attach the outer DC instance to the DOM element
203
+ // The key 'myComponent' is used to access it later via myElement._mc.myComponent
204
+ DC.add(myElement, 'myComponent', new MyPublicClass(myElement));
205
+
206
+ // Hint: After adding, myElement now has:
207
+ // 1. A property: myElement._mc = { myComponent: instanceOfMyPublicClass }
208
+ // 2. An attribute: <div id="myDiv" _mc>...</div>
209
+ ```
210
+
211
+ ### Remove Micro Components (DC.remove)
212
+
213
+ Use the static DC.remove() method to detach a top-level component.
214
+
215
+ ```javascript
216
+ const myElement = document.getElementById('myDiv');
217
+
218
+ DC.remove(myElement, 'myComponent');
219
+
220
+ // If myElement._mc is now empty, the '_mc' attribute is also removed.
221
+ ```
222
+
223
+ ### Retrieve Micro Components
224
+
225
+ Access attached components via the \_mc property of the target element or parent component.
226
+
227
+ ```javascript
228
+ // Get the container object for all MCs on an element
229
+ const myElement = document.getElementById('myDiv');
230
+ const mcContainer = myElement._mc;
231
+ console.log(mcContainer); // { myComponent: instanceOfMyPublicClass, ... }
232
+
233
+ // Get a specific instance by its key
234
+ const specificInstance = myElement._mc?.myComponent;
235
+ if (specificInstance) {
236
+ specificInstance.doSomethingOuter();
237
+ }
238
+ ```
239
+
240
+ ### Triggering Events
241
+
242
+ 1. Triggering from outside onto components on an element:
243
+
244
+ ```javascript
245
+ const targetElement = document.getElementById('myDiv');
246
+ const eventData = { message: 'Hello from outside!' };
247
+
248
+ // Trigger 'customEvent' locally on MCs attached to targetElement
249
+ DC.trigger(targetElement, 'customEvent', eventData, 'l');
250
+
251
+ // Trigger 'customEvent' and bubble up the DOM tree
252
+ DC.trigger(targetElement, 'customEvent', eventData, 'u');
253
+
254
+ // Trigger 'customEvent' locally, bubble up, and bubble down
255
+ DC.trigger(targetElement, 'customEvent', eventData, 'lud');
256
+ ```
257
+
258
+ 2. Triggering from inside a component instance:
259
+
260
+ ```javascript
261
+ class MyTriggeringComponent extends DC {
262
+ // ... constructor ...
263
+
264
+ someMethod() {
265
+ const eventData = { source: 'MyTriggeringComponent' };
266
+
267
+ // Trigger 'internalEvent' only on this instance and its nested _mc components
268
+ this.trigger('internalEvent', eventData); // Default bubble 'l'
269
+
270
+ // Trigger 'externalEvent' on sibling components attached to the same target element
271
+ // (and potentially bubble up/down based on the 'bubble' parameter)
272
+ DC.trigger(this.target, 'externalEvent', eventData, 'l'); // Trigger locally on siblings
273
+
274
+ // Trigger 'downwardEvent' on components attached to descendant elements
275
+ this.trigger('downwardEvent', eventData, 'd'); // Bubbles down from this component's target
276
+ }
277
+
278
+ // Event handler methods
279
+ onCustomEvent(event) {
280
+ console.log(`[${this.constructor.name}] received customEvent:`, event.data);
281
+ }
282
+
283
+ onInternalEvent(event) {
284
+ console.log(
285
+ `[${this.constructor.name}] received internalEvent:`,
286
+ event.data
287
+ );
288
+ }
289
+
290
+ onExternalEvent(event) {
291
+ console.log(
292
+ `[${this.constructor.name}] received externalEvent:`,
293
+ event.data
294
+ );
295
+ }
296
+
297
+ onDownwardEvent(event) {
298
+ console.log(
299
+ `[${this.constructor.name}] received downwardEvent:`,
300
+ event.data
301
+ );
302
+ }
303
+ }
304
+ ```
305
+
306
+ ### Retrieving Inner Micro Components
307
+
308
+ Access nested components directly via the parent's `_mc` property.
309
+
310
+ ```javascript
311
+ class Inner extends MC {
312
+ constructor(target) {
313
+ super(target);
314
+ }
315
+ someInnerMethod() {
316
+ console.log('Inner method called!');
317
+ }
318
+ }
319
+
320
+ class Outer extends DC {
321
+ constructor(target) {
322
+ super(target);
323
+ this._mc.inner = new Inner(this);
324
+ }
325
+
326
+ accessInner() {
327
+ this._mc?.inner.someInnerMethod();
328
+ }
329
+ }
330
+ ```
331
+
332
+ ### Navigating Parent/Ancestor MCs
333
+
334
+ Use .parent() and .ancestors() (available on DC instances) to find components attached to elements higher up the DOM tree.
335
+
336
+ These methods skip elements that don't have any MCs attached.
337
+
338
+ ```javascript
339
+ // Inside a method of a DC instance:
340
+ const parentMcs = this.parent(); // Array of MC instances on the first parent element with MCs
341
+ console.log(parentMcs);
342
+
343
+ const allAncestorMcs = this.ancestors(); // Array of all MC instances on all ancestor elements with MCs
344
+ console.log(allAncestorMcs);
345
+
346
+ // Find a specific component type/key among ancestors
347
+ const specificAncestor = this.ancestors('someSpecificKey')?.[0];
348
+
349
+ if (specificAncestor) {
350
+ console.log('Found specific ancestor:', specificAncestor);
351
+ }
352
+ ```
353
+
354
+ ### Retrieving Child/Descendant MCs
355
+
356
+ Use .children() and .descendants() (available on DC instances) to find components attached to elements lower down the DOM tree.
357
+
358
+ ```javascript
359
+ // Inside a method of a DC instance:
360
+ const childMcs = this.children(); // Array of MCs on direct child elements with MCs
361
+ console.log(childMcs);
362
+
363
+ const descendantMcs = this.descendants(); // Array of all MCs on all descendant elements with MCs
364
+ console.log(descendantMcs);
365
+
366
+ // Find specific components among descendants
367
+ const specificDescendants = this.descendants('someSpecificKey');
368
+ console.log(specificDescendants); // Array of matching descendant MCs
369
+ ```
370
+
371
+ ### Auto-attaching DOM Listeners
372
+
373
+ Methods defined in your DC class starting with `on` or `one` followed by a capital letter, which also match native DOM event names (case-insensitive check), will automatically have listeners attached to the target DOM element:
374
+
375
+ ```javascript
376
+ class McWithHandlers extends DC {
377
+ constructor(target) {
378
+ super(target);
379
+ }
380
+
381
+ // This method name matches the native 'click' event.
382
+ // A listener will be automatically added to this.target.
383
+ onClick(domEvent) {
384
+ console.log('Target element clicked!', domEvent);
385
+ // 'this' refers to the MyClickHandler instance
386
+ this.target.style.backgroundColor = 'lightblue';
387
+ }
388
+
389
+ // This method name matches the native 'mouseover' event.
390
+ // 'one' means the listener will be removed after the first trigger.
391
+ oneMouseover(domEvent) {
392
+ console.log('Mouseover detected once!', domEvent);
393
+ this.target.style.border = '2px solid red';
394
+ }
395
+
396
+ // This is a custom event handler, not a native DOM event.
397
+ // It will be triggered by this.trigger('CustomAction') or DC.trigger(this.target, 'CustomAction')
398
+ onCustomAction(mcEvent) {
399
+ console.log('Custom action triggered!', mcEvent.data);
400
+ }
401
+ }
402
+
403
+ const myDiv = document.getElementById('clickableDiv');
404
+ DC.add(myDiv, 'clickHandler', new McWithHandlers(myDiv));
405
+ // Now clicking or mousing over 'clickableDiv' will trigger the methods.
406
+ ```
407
+
408
+ ### Lifecycle Events
409
+
410
+ The framework defines specific methods that are called at certain points in a component's life.
411
+
412
+ #### Init
413
+
414
+ Method Name: oneInit(event)
415
+
416
+ Triggered: Asynchronously (setTimeout(0)) after the component instance is created (called from McBase constructor via the init helper).
417
+
418
+ Execution Order:
419
+
420
+ The component's oneInit is called first, then it recursively calls oneInit on any components nested within its `_mc` property.
421
+
422
+ Requirement:
423
+
424
+ - You must define a method named `oneInit` in your class for it to be called.
425
+ - The `one` prefix implies it's intended to run once during initialization.
426
+
427
+ ```javascript
428
+ class MyLifecycleComponent extends DC {
429
+ constructor(target) {
430
+ super(target);
431
+ console.log('Constructor finished');
432
+ this._mc.nested = new NestedComponent(this);
433
+ }
434
+
435
+ oneInit(event) {
436
+ // This code runs shortly after the constructor finishes.
437
+ console.log('MyLifecycleComponent oneInit called!', event);
438
+ // Safe to access this.target or setup things that depend on the instance being fully constructed.
439
+ }
440
+ }
441
+ ```
442
+ ## Auto Loading
443
+
444
+ `autoload()` provides a **progressive enhancement** strategy. It allows you to declare custom elements or special micro components in your HTML without needing to include all their JavaScript code upfront.
445
+
446
+ The necessary code is fetched and executed only when these components actually appear in the DOM, potentially improving initial page load time and resource usage.
447
+
448
+ ### Enabling
449
+
450
+ - If you call **`autoload(true)`** (or just **`autoload()`** as true is the parameter default), it initializes a MutationObserver.
451
+ This observer watches the document.body for any changes to its child elements or subtree (e.g., when new elements are added to the page).
452
+
453
+ **./autoload.html**
454
+ ```html
455
+ <!DOCTYPE html>
456
+ <html lang="en">
457
+ <head>
458
+ <meta charset="UTF-8" />
459
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
460
+ <title>Humble Beginnings</title>
461
+ <script src="./microcomponents.min.js"></script>
462
+ <script>
463
+ Object.assign(window, microcomponents); // make system global
464
+ autoload(true); // automatically load web components that are undefined
465
+ </script>
466
+ </head>
467
+ <body>
468
+ <div _mc="test-mymc"></div> <!--- example of a auto-loading micro component --->
469
+ <test-component></test-component> <!--- example of a auto-loading custom element --->
470
+ </body>
471
+ </html>
472
+
473
+ ```
474
+
475
+ **./test/mymc.ts**
476
+ ```typescript
477
+ 'use strict';
478
+ import { DC, LooseObject } from '../microcomponents.js';
479
+
480
+ export default class myMC extends DC {
481
+ constructor(target: HTMLElement) {
482
+ super(target);
483
+ delete this._mc; // not needed
484
+ }
485
+
486
+ oneInit() {
487
+ // console.log('testmc connected to DOM');
488
+ }
489
+ }
490
+ ```
491
+
492
+ **./test/component.ts**
493
+ ```typescript
494
+ 'use strict';
495
+ import { DC, defineCE } from '.././microcomponents.js';
496
+
497
+ defineCE(
498
+ 'test-component',
499
+
500
+ class extends DC {
501
+ data: unknown;
502
+
503
+ constructor(target: HTMLElement) {
504
+ super(target);
505
+
506
+ const that = this; // to help minification, you can use 'this' throughout the constructor as well
507
+
508
+ delete that._mc; // no inner classes needed
509
+
510
+ let shadow = (that as DC).target.attachShadow({ mode: 'open' });
511
+ shadow.innerHTML = `<style>${that.css}</style>${that.template}`;
512
+
513
+ }
514
+
515
+ get template() {
516
+ return /*html*/ `
517
+ <span>Hello World</span>
518
+ `;
519
+ }
520
+
521
+ get css() {
522
+ return /*css*/ `
523
+ div {
524
+ background-color: cyan;
525
+ }`;
526
+ }
527
+ }
528
+ );
529
+ ```
530
+
531
+ **Page HTML after loading**
532
+ ```HTML
533
+ (...)
534
+ <div _mc=""></div> <!--- _mc component definition now empty to avoid re-loading --->
535
+ <test-component></test-component> <!--- the custom element is initialized now --->
536
+ (...)
537
+ ```
538
+
539
+ ### Disabling:
540
+
541
+ - If you call **`autoload(false)`**, it disconnects the observer, stopping the automatic loading feature:
542
+
543
+ ```typescript
544
+ autoload(false);
545
+ ```
546
+
547
+ ### Loading Mechanism:
548
+
549
+ When enabled and changes are detected in the DOM, the a callback function is triggered. This function scans the DOM for two types of components that might not yet be defined:
550
+
551
+ - **Undefined Custom Elements:** It looks for HTML elements whose tag names haven't been defined via customElements.define() (e.g., `<my-custom-tag>`).
552
+
553
+ For an element like `<my-custom-tag>`, it will attempt to load a script from a path derived from the tag name, such as **`./my/custom/tag.js`**. The loaded script is expected to then define this custom element.
554
+
555
+ - **Undefined Micro Components:** It looks for HTML elements that have an **`_mc`** attribute that is not empty, e.g., **`<div _mc="my-component my-other">`**.
556
+
557
+ **For each component name** listed in the **`_mc`** attribute (like **`my-component`**), it will attempt to load a script from a path like **`./my/component.js`**.
558
+
559
+ **These scripts are expected to `export default class {...}`** (usually extending DC from your code) that defines the behavior of this micro component.
560
+
561
+ **In general:**
562
+
563
+ - For each undefined component found, it dynamically creates a **`<script type="module">`** tag in the **`head`** of the document, and sets its src attribute to the appropriate path.
564
+
565
+ - The src of the script is determined by converting the component name (e.g., **`my-component`**) into a path (e.g., **`./my/component.js`**).
566
+
567
+ - These script tags are appended to the `<head>` of the document to initiate loading.
568
+
569
+ ### Internal Callbacks and Instantiation:
570
+
571
+ - **Custom Elements:**
572
+ Once the script for a custom element loads and defines the element (using customElements.define()), the browser automatically handles upgrading any existing instances of that tag.
573
+
574
+ - **Micro Components:**
575
+ When a script for an _mc component loads successfully, a another callback is executed. This callback:
576
+ - Stores the loaded component class in an internal repository to avoid unnecessary reloading.
577
+ - Finds all HTMLElement in the DOM that declared this _mc component.
578
+ - Instantiates the component class and adds it to the HTMLElement.
579
+ - Removes the component's name from the _mc attribute to prevent re-processing.
580
+
581
+ ### State Tracking:
582
+
583
+ `autoload()` internally uses two Map() objects:
584
+
585
+ - **`CEs`** (for Custom Elements that are currently loading)
586
+ - **`MCs`** (for Micro Components), to keep track of components that are currently loading or have been loaded. This prevents attempting to load the same component multiple times.
587
+
588
+ These maps are exposed for debugging purposes. You can access them via `autoload.state` which returns an object with `CEs` and `MCs` properties.