@twobirds/microcomponents 0.0.10 → 0.0.14
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 +27 -47
- package/assets/documentation/ajaxian-1.png +0 -0
- package/assets/documentation/ajaxian-2.png +0 -0
- package/assets/documentation/ajaxian-3.png +0 -0
- package/assets/documentation/history.md +436 -0
- package/assets/documentation/how_tos.md +0 -0
- package/assets/documentation/introduction.md +0 -0
- package/{microcomponents_magic.md → assets/documentation/magic.md} +1 -1
- package/assets/documentation/rnd0023-1.jpg +0 -0
- package/assets/documentation/rnd0023-2.jpg +0 -0
- package/assets/documentation/rnd0023-3.jpg +0 -0
- package/assets/documentation/tB_react.md +369 -0
- package/assets/test.html +11 -0
- package/dist/microcomponents.d.ts +15 -231
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.es3.d.ts +5 -0
- package/dist/microcomponents.es3.d.ts.map +1 -1
- package/dist/microcomponents.js +143 -334
- package/dist/microcomponents.min.js +32 -29
- package/dist/microcomponents.ts +209 -395
- package/package.json +3 -4
- package/public/{docs/assets → assets}/highlight.css +29 -22
- package/public/assets/search.js +1 -0
- package/public/{docs/classes → classes}/microcomponents.MC.html +78 -241
- package/public/{docs/classes → classes}/microcomponents_es3.MC.html +12 -12
- package/public/documentation/ajaxian-1.png +0 -0
- package/public/documentation/ajaxian-2.png +0 -0
- package/public/documentation/ajaxian-3.png +0 -0
- package/public/documentation/functional_requirements.md +8 -0
- package/public/documentation/history.md +436 -0
- package/public/documentation/how_tos.md +0 -0
- package/public/documentation/introduction.md +0 -0
- package/public/documentation/magic.md +87 -0
- package/public/documentation/proposal_draft.md +37 -0
- package/public/documentation/rnd0023-1.jpg +0 -0
- package/public/documentation/rnd0023-2.jpg +0 -0
- package/public/documentation/rnd0023-3.jpg +0 -0
- package/public/documentation/tB_react.md +369 -0
- package/public/documentation/usage.md +142 -0
- package/public/documentation/why.md +25 -0
- package/public/{docs/functions → functions}/microcomponents_es3.__isEmpty.html +1 -1
- package/public/index.html +101 -11
- package/public/microcomponents.js +176 -0
- package/public/{docs/modules → modules}/microcomponents.html +2 -7
- package/public/test.html +11 -0
- package/public/test.js +216 -0
- package/src/microcomponents.ts +209 -395
- package/src/test.js +247 -0
- package/typedoc.json +1 -1
- package/assets/index.html +0 -11
- package/jsdoc.conf.json +0 -19
- package/public/docs/assets/search.js +0 -1
- package/public/docs/index.html +0 -97
- package/public/docs/interfaces/microcomponents.HTMLElementMc.html +0 -3605
- package/public/jsdoc/es3/files.html +0 -216
- package/public/jsdoc/es3/index.html +0 -210
- package/public/jsdoc/es3/symbols/_global_.html +0 -368
- package/public/jsdoc/es3/symbols/src/build_microcomponents.es3.js.html +0 -104
- package/public/jsdoc/es5+/files.html +0 -218
- package/public/jsdoc/es5+/index.html +0 -218
- package/public/jsdoc/es5+/symbols/MC.html +0 -621
- package/public/jsdoc/es5+/symbols/_global_.html +0 -720
- package/public/jsdoc/es5+/symbols/microcomponents.MC.html +0 -323
- package/public/jsdoc/es5+/symbols/src/build_microcomponents.js.html +0 -375
- /package/{microcomponents_functional_requirements.md → assets/documentation/functional_requirements.md} +0 -0
- /package/{MicroComponentProposal_draft.md → assets/documentation/proposal_draft.md} +0 -0
- /package/{microcomponents_usage.md → assets/documentation/usage.md} +0 -0
- /package/{microcomponents_why.md → assets/documentation/why.md} +0 -0
- /package/public/{docs/.nojekyll → .nojekyll} +0 -0
- /package/public/{docs/assets → assets}/main.js +0 -0
- /package/public/{docs/assets → assets}/style.css +0 -0
- /package/public/{docs/modules → modules}/microcomponents_es3.html +0 -0
- /package/public/{docs/modules.html → modules.html} +0 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
### RFC Proposal to WHATWG
|
|
2
|
+
|
|
3
|
+
**Title:** Proposal for a **Micro Component Interface** in the DOM
|
|
4
|
+
|
|
5
|
+
**Abstract:**
|
|
6
|
+
|
|
7
|
+
This proposal introduces a standardized interface for managing micro components within HTML elements, enhancing modularity and reusability in web development.
|
|
8
|
+
|
|
9
|
+
**Introduction:**
|
|
10
|
+
|
|
11
|
+
Microcomponents provide a way to encapsulate functionality and state within a target DOM element. This proposal outlines methods for adding, removing, and querying micro components.
|
|
12
|
+
|
|
13
|
+
**Proposed API:**
|
|
14
|
+
|
|
15
|
+
- **Static Methods:**
|
|
16
|
+
- `MC.add( target, key, element)`: Attach a top level micro component to an HTMLElement.
|
|
17
|
+
- `MC.remove( target, key )`: Detach a top level micro component from an HTMLElement.
|
|
18
|
+
- `MC.siblings( target )`: get top level micro components from an HTMLElement.
|
|
19
|
+
|
|
20
|
+
- **Instance Methods:**
|
|
21
|
+
- `add( key, element )`: Attach a private Micro Component to a Micro Component.
|
|
22
|
+
- `remove( key )`: Detach a private Micro Component from a Micro Component.
|
|
23
|
+
- `siblings(search?)`: Retrieve sibling micro components.
|
|
24
|
+
- `parent(search?)`: Get parent top level micro components.
|
|
25
|
+
- `parents(search?)`: Get all ancestor top level micro components.
|
|
26
|
+
- `children(search?)`: Retrieve child top level micro components.
|
|
27
|
+
- `descendants(search?)`: Get all descendant top level micro components.
|
|
28
|
+
|
|
29
|
+
**Use Cases:**
|
|
30
|
+
1. Modular web applications.
|
|
31
|
+
2. Enhancing component-based architecture.
|
|
32
|
+
|
|
33
|
+
**Conclusion:**
|
|
34
|
+
Implementing this interface will streamline microcomponent management, fostering cleaner code and improved performance.
|
|
35
|
+
|
|
36
|
+
**References:**
|
|
37
|
+
- [Microcomponents Repository](https://gitlab.com/twoBirds/microcomponents) for a polyfill implementation and usage examples.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
# Discussing React Prop Drilling with ChatGPT
|
|
2
|
+
|
|
3
|
+
This is an interesting read where I wanted ChatGPT to come up with a Micro Component solution for a React problem.
|
|
4
|
+
|
|
5
|
+
To be honest, I knew MCs can solve it. But I wanted to know what ChatGPT can make of it. Spoiler: it got remarkably close on the first try. The second try was spot on.
|
|
6
|
+
|
|
7
|
+
Please mind I have a trained session with a backlog. So you wont get the same answers.
|
|
8
|
+
|
|
9
|
+
***( Cut outs and comments are in parentheses...)***
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Disclaimer:
|
|
13
|
+
|
|
14
|
+
The answers partly reflect on the twoBirds framework instead of Micro Components. twoBirds converts all props into convenience observables automatically, which MCs don't do.
|
|
15
|
+
|
|
16
|
+
The code ChatGPT generated first isn't quite right therefore. Also since I am not a react specialist I cannot guarantee for that part. In general, this is close enough though.
|
|
17
|
+
|
|
18
|
+
I edited the code to reflect the right solution and annotated my changes.
|
|
19
|
+
|
|
20
|
+
I put that into the first answer and asked ChatGPT again.
|
|
21
|
+
|
|
22
|
+
The second answer was spot on, assuming some magic for the convenience observables.
|
|
23
|
+
|
|
24
|
+
Bonus: during this process I got the inspiration that there should be several kinds of state objects:
|
|
25
|
+
|
|
26
|
+
- readonlyState: exports some inner props to the DOM for others to read, but the external state cannot be modified. Not observable.
|
|
27
|
+
- observableState: observable readonlyState. You can attach callbacks that fire on changes.
|
|
28
|
+
- writeableState: observableState that you can modify, typesafe at runtime. Inner methods will do further work probably.
|
|
29
|
+
- hardenedState: writableState (typesafe) that also does sanity checks before inner processing and throws on check failure.
|
|
30
|
+
|
|
31
|
+
This will be a complete public state model in the DOM.
|
|
32
|
+
|
|
33
|
+
And since it is needed for the solution below, that will be the first reference implementation using Micro Components. I hope the React world will be happy.
|
|
34
|
+
|
|
35
|
+
Working with AI can be wonderful...
|
|
36
|
+
|
|
37
|
+
## Start Session
|
|
38
|
+
|
|
39
|
+
Me:
|
|
40
|
+
**Do you remember our chat regarding Micro Components vs. existing frameworks?**
|
|
41
|
+
|
|
42
|
+
ChatGPT:
|
|
43
|
+
|
|
44
|
+
Yes, I remember our discussion! We talked about your approach to microcomponents as distinct from established frameworks, with a focus on smaller, modular, data-driven components that are stored in a hash attached to DOM elements (specifically in a _tb property in twoBirds or _mc for microcomponents). Your system emphasizes manual wiring, observability, and ease of interaction across components, making it different from React, Vue, or other component libraries that depend heavily on virtual DOMs, JSX, or hooks.
|
|
45
|
+
|
|
46
|
+
Your components follow principles of separation of concerns and DRY by keeping interactions modular but flexible, and they use observables to trigger functions in other components, which can be linked in chains. Your approach allows a component-based design that integrates deeply into the DOM without the overhead of larger frameworks. And while existing frameworks typically follow a top-down data flow, your system allows bi-directional, observable properties that automatically update the DOM when data changes.
|
|
47
|
+
|
|
48
|
+
Would you like to revisit any specific aspect, or compare further?
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## React
|
|
52
|
+
|
|
53
|
+
Me:
|
|
54
|
+
**There was something about React specifically?**
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
ChatGPT:
|
|
58
|
+
|
|
59
|
+
(...)
|
|
60
|
+
|
|
61
|
+
### Manual Wiring vs. React’s Context and Hooks:
|
|
62
|
+
React uses Context and hooks (like useState and useEffect) to manage data flow and lifecycle.
|
|
63
|
+
|
|
64
|
+
Your approach leans on manual wiring for connecting data across microcomponents, avoiding the abstraction of hooks and allowing direct, observable-driven updates. This can make the flow of data more transparent and easier to trace in complex applications, whereas hooks can sometimes add complexity or obscurity when trying to understand data dependencies.
|
|
65
|
+
|
|
66
|
+
### Component Reusability and Scaling:
|
|
67
|
+
Both React and twoBirds support component-based architecture.
|
|
68
|
+
|
|
69
|
+
However, twoBirds allows multiple microcomponents on a single DOM element, and each microcomponent is responsible for specific tasks, making scaling in large applications more modular and straightforward. React encourages separation at the component level, whereas twoBirds does so at the microcomponent level, with direct chaining possible between components.
|
|
70
|
+
|
|
71
|
+
(...)
|
|
72
|
+
|
|
73
|
+
Each of these aspects underlines twoBirds’ focus on minimalism, modularity, and developer control. These are different from React's abstraction and tooling-heavy approach, making your framework potentially more transparent and lightweight for specific use cases.
|
|
74
|
+
|
|
75
|
+
Would you like to dive into any of these differences in more detail?
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## Prop Drilling
|
|
79
|
+
|
|
80
|
+
Me:
|
|
81
|
+
**In general there was a problem regarding carrying properties from a component close to the root of the document to a component that is much deeper into the DOM tree, do you remember that?**
|
|
82
|
+
|
|
83
|
+
ChatGPT:
|
|
84
|
+
|
|
85
|
+
Yes, I remember!
|
|
86
|
+
|
|
87
|
+
We discussed the challenges in carrying properties or state from a root-level component down to deeply nested components. In React, this kind of deep property sharing can become cumbersome and often requires additional tools or patterns, such as:
|
|
88
|
+
|
|
89
|
+
### Prop Drilling:
|
|
90
|
+
React requires passing data through each level of components in the tree via props, a process known as prop drilling. This can lead to complex and hard-to-manage code, especially when the data isn’t relevant to every component in the chain.
|
|
91
|
+
|
|
92
|
+
(...)
|
|
93
|
+
|
|
94
|
+
In contrast, your twoBirds microcomponents have an alternative approach that makes it easier to pass properties throughout the DOM tree:
|
|
95
|
+
|
|
96
|
+
### Direct Microcomponent Selectors:
|
|
97
|
+
twoBirds includes selector methods (like .parents() and .descendants()) that allow components to access any microcomponent within the tree without explicit data passing. This capability enables deeply nested components to retrieve properties from higher-level components directly, bypassing the need to route data through intermediary components.
|
|
98
|
+
|
|
99
|
+
### Observables for State Propagation:
|
|
100
|
+
Since twoBirds uses observables, properties from a root component can be shared across components by observing changes directly. When the observable changes, all relevant components are notified and updated automatically. This is a natural data propagation method that doesn’t require explicit context or external state management.
|
|
101
|
+
|
|
102
|
+
***( ...this part was wrong, as it reflected on tB convenience observables, instead of pure micro components ... )***
|
|
103
|
+
|
|
104
|
+
### Manual Wiring for Control:
|
|
105
|
+
|
|
106
|
+
**Because twoBirds relies on manual wiring, you can set up specific connections between components as needed.** This gives you fine-grained control over where and how data flows through the app without involving a top-level state or intermediary components, making it more scalable for larger apps without the need for a Context API or state library.
|
|
107
|
+
|
|
108
|
+
(...)
|
|
109
|
+
|
|
110
|
+
This structure offers a highly flexible, context-free solution for deep property sharing, making it an efficient alternative to prop drilling or global state libraries in many scenarios. Would you like more details on how twoBirds selectors work with observables to achieve this?
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
## Generate Code
|
|
114
|
+
|
|
115
|
+
Me:
|
|
116
|
+
**Can you give an example of 2 react components that interact with each other using micro components? Give me the code of both components, using microomponents to exchange data**
|
|
117
|
+
|
|
118
|
+
***( The first result as shown below is edited and fed back to outline errors ChatGPT made, so this is not the original answer but my corrected feed back )***
|
|
119
|
+
|
|
120
|
+
***( ChatGPTs errors were based on the assumption that MCs behave like twoBirds micro components, which turn properties in convenience observables automatically. These can be accessed like simple properties, but still work as observables. )***
|
|
121
|
+
|
|
122
|
+
Skip to [Final Version](#final-version) if you dont want to see my intermediate corrections.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
ChatGPT:
|
|
126
|
+
|
|
127
|
+
Here’s an example of two React components interacting with each other using the Micro Components (MC) approach as you’ve described. In this example, the microcomponents are responsible for sharing and updating data across the two React components without relying on React's props or context.
|
|
128
|
+
|
|
129
|
+
In this scenario, we’ll have two React components:
|
|
130
|
+
|
|
131
|
+
ParentComponent: Contains a microcomponent managing shared state.
|
|
132
|
+
ChildComponent: Retrieves and observes updates to that shared state through the microcomponent system.
|
|
133
|
+
|
|
134
|
+
The Micro Component framework will handle state observability, allowing ChildComponent to respond to changes in ParentComponent.
|
|
135
|
+
|
|
136
|
+
### Step 1: Set Up Micro Components for State Sharing
|
|
137
|
+
|
|
138
|
+
First, let’s create a microcomponent to hold shared data. This MC will be instantiated in ParentComponent and observed in ChildComponent.
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
|
|
142
|
+
// SharedStateMC.js
|
|
143
|
+
import {MC} from '@twobirds/microcomponents';
|
|
144
|
+
|
|
145
|
+
class SharedStateMC extends MC {
|
|
146
|
+
constructor(target) {
|
|
147
|
+
super(target);
|
|
148
|
+
|
|
149
|
+
// Define an observable property for shared data
|
|
150
|
+
// this part is wrong: ChatGPT assumed this property is a twoBirds convenience observable property, which MCs dont do automatically.
|
|
151
|
+
// lets for now assume it is
|
|
152
|
+
this.sharedData = "Initial data from Parent"; // this in fact is assumed to be an observable in tB, as any property defined in the constructor.
|
|
153
|
+
// Also, this observable property would be runtime type safe, BTW.
|
|
154
|
+
// This is important for public MCs, as they expose a PUBLIC interface to internal properties for EVERYBODY to use at runtime. So:
|
|
155
|
+
// document.getElementById("parent-root")._mc.sharedState.sharedData = 'My hilarious string';
|
|
156
|
+
// ... this will trigger the callbacks associated with it
|
|
157
|
+
// MCs ACT AS INTERFACES!
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export default SharedStateMC;
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Step 2: Create ParentComponent with Micro Component for Shared State
|
|
166
|
+
|
|
167
|
+
In ParentComponent, we’ll create an instance of SharedStateMC and attach it as a microcomponent on the main DOM element of this component.
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
|
|
171
|
+
// ParentComponent.js
|
|
172
|
+
import React, { useEffect } from 'react';
|
|
173
|
+
import SharedStateMC from './SharedStateMC';
|
|
174
|
+
|
|
175
|
+
function ParentComponent() {
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
// Attach SharedStateMC to the main component div
|
|
178
|
+
const parentElement = document.getElementById("parent-root");
|
|
179
|
+
const sharedState = MC.add( parentElement, 'sharedState', new SharedStateMC( parentElement ) );
|
|
180
|
+
|
|
181
|
+
// Simulate updating data after 3 seconds
|
|
182
|
+
// changing a tB convenience observable really is as easy as this, also it is type-safe at runtime
|
|
183
|
+
setTimeout(() => {
|
|
184
|
+
sharedState.sharedData = "Updated data from Parent";
|
|
185
|
+
}, 3000);
|
|
186
|
+
}, []);
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<div id="parent-root">
|
|
190
|
+
<h1>Parent Component</h1>
|
|
191
|
+
<p>Check ChildComponent for updated data.</p>
|
|
192
|
+
<ChildComponent />
|
|
193
|
+
</div>
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export default ParentComponent;
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Step 3: Create ChildComponent to Observe and Display Shared Data
|
|
201
|
+
|
|
202
|
+
In ChildComponent, we’ll access the SharedStateMC instance created by ParentComponent using DOM selectors and observe the sharedData property.
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
|
|
206
|
+
// ChildComponent.js
|
|
207
|
+
import React, { useEffect, useState } from 'react';
|
|
208
|
+
|
|
209
|
+
function ChildComponent() {
|
|
210
|
+
const [data, setData] = useState("");
|
|
211
|
+
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
// Access the microcomponent instance on the parent's DOM element
|
|
214
|
+
const parentElement = document.getElementById("parent-root");
|
|
215
|
+
const sharedStateMC = parentElement._mc.sharedState;
|
|
216
|
+
|
|
217
|
+
// Initial data load
|
|
218
|
+
setData(sharedStateMC.sharedData);
|
|
219
|
+
|
|
220
|
+
// Observe changes to shared data in SharedStateMC
|
|
221
|
+
// this isn't quite right, for reasons mentioned above, but close
|
|
222
|
+
sharedStateMC.observe((newData) => {
|
|
223
|
+
setData(newData);
|
|
224
|
+
});
|
|
225
|
+
}, []);
|
|
226
|
+
|
|
227
|
+
return (
|
|
228
|
+
<div>
|
|
229
|
+
<h2>Child Component</h2>
|
|
230
|
+
<p>Data from Parent: {data}</p>
|
|
231
|
+
</div>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export default ChildComponent;
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### How It Works
|
|
239
|
+
|
|
240
|
+
In ParentComponent: An instance of SharedStateMC is created and attached to the main DOM element with an ID parent-root.
|
|
241
|
+
After 3 seconds, it updates sharedData to a new value.
|
|
242
|
+
|
|
243
|
+
In ChildComponent: We access the SharedStateMC instance via the _mc property of the parent element and observe sharedData.
|
|
244
|
+
When sharedData changes, ChildComponent re-renders with the updated data.
|
|
245
|
+
|
|
246
|
+
### Explanation
|
|
247
|
+
|
|
248
|
+
Observable Property: sharedData is an observable property within SharedStateMC.
|
|
249
|
+
This allows the ChildComponent to subscribe and react to changes without using React's props or context.
|
|
250
|
+
Direct DOM Access:
|
|
251
|
+
The ChildComponent directly accesses ParentComponent's microcomponent via document.getElementById("parent-root")._mc.
|
|
252
|
+
|
|
253
|
+
This pattern effectively mimics cross-component communication in React but using the microcomponents as an intermediary for data sharing.
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
## Final Version
|
|
257
|
+
|
|
258
|
+
Me:
|
|
259
|
+
**assume this revised markup... can you spot the difference? ( My feedback code from above was added )**
|
|
260
|
+
|
|
261
|
+
ChatGPT:
|
|
262
|
+
|
|
263
|
+
You’ve provided a revised example that incorporates microcomponents with clearer runtime type safety and public interfaces. Let’s review the differences and corrections made in this version:
|
|
264
|
+
|
|
265
|
+
***( ChatGPT explains my changes )***
|
|
266
|
+
|
|
267
|
+
Here’s the final, corrected code with these changes fully implemented:
|
|
268
|
+
|
|
269
|
+
Final Revised Code
|
|
270
|
+
SharedStateMC.js
|
|
271
|
+
|
|
272
|
+
```javascript
|
|
273
|
+
import { MC } from '@twobirds/microcomponents';
|
|
274
|
+
|
|
275
|
+
class SharedStateMC extends MC {
|
|
276
|
+
constructor(target) {
|
|
277
|
+
super(target);
|
|
278
|
+
// Define an observable property for shared data with runtime type safety
|
|
279
|
+
this.sharedData = "Initial data from Parent";
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export default SharedStateMC;
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
ParentComponent.js
|
|
288
|
+
|
|
289
|
+
```javascript
|
|
290
|
+
import React, { useEffect } from 'react';
|
|
291
|
+
import SharedStateMC from './SharedStateMC';
|
|
292
|
+
import { MC } from '@twobirds/microcomponents';
|
|
293
|
+
|
|
294
|
+
function ParentComponent() {
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
const parentElement = document.getElementById("parent-root");
|
|
297
|
+
|
|
298
|
+
// Attach SharedStateMC instance to parent element with 'sharedState' key
|
|
299
|
+
const sharedState = MC.add(parentElement, 'sharedState', new SharedStateMC(parentElement));
|
|
300
|
+
|
|
301
|
+
// Updating shared data after 3 seconds with observable property syntax
|
|
302
|
+
setTimeout(() => {
|
|
303
|
+
sharedState.sharedData = "Updated data from Parent";
|
|
304
|
+
}, 3000);
|
|
305
|
+
}, []);
|
|
306
|
+
|
|
307
|
+
return (
|
|
308
|
+
<div id="parent-root">
|
|
309
|
+
<h1>Parent Component</h1>
|
|
310
|
+
<p>Check ChildComponent for updated data.</p>
|
|
311
|
+
<ChildComponent />
|
|
312
|
+
</div>
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export default ParentComponent;
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
ChildComponent.js
|
|
321
|
+
|
|
322
|
+
```javascript
|
|
323
|
+
import React, { useEffect, useState } from 'react';
|
|
324
|
+
|
|
325
|
+
function ChildComponent() {
|
|
326
|
+
const [data, setData] = useState("");
|
|
327
|
+
|
|
328
|
+
useEffect(() => {
|
|
329
|
+
const parentElement = document.getElementById("parent-root");
|
|
330
|
+
const sharedStateMC = parentElement._mc.sharedState;
|
|
331
|
+
|
|
332
|
+
// Initial data load
|
|
333
|
+
setData(sharedStateMC.sharedData);
|
|
334
|
+
|
|
335
|
+
// Observing updates to sharedData property
|
|
336
|
+
sharedStateMC.sharedData.observe((newData) => {
|
|
337
|
+
setData(newData);
|
|
338
|
+
});
|
|
339
|
+
}, []);
|
|
340
|
+
|
|
341
|
+
return (
|
|
342
|
+
<div>
|
|
343
|
+
<h2>Child Component</h2>
|
|
344
|
+
<p>Data from Parent: {data}</p>
|
|
345
|
+
</div>
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export default ChildComponent;
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Summary of Changes**
|
|
353
|
+
|
|
354
|
+
Observables: The sharedData property in SharedStateMC is now noted to be observable and accessible as a public interface.
|
|
355
|
+
|
|
356
|
+
Microcomponent Attaching: Using MC.add clarifies the attachment of the SharedStateMC instance to parentElement.
|
|
357
|
+
|
|
358
|
+
Runtime Type Safety: The property remains observable, allowing callbacks on sharedData in ChildComponent.
|
|
359
|
+
|
|
360
|
+
This implementation accurately reflects the design principles and interfaces for twoBirds MCs, allowing cross-component data sharing while ensuring type safety and a clean observable setup.
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
**ChatGPT can produce errors. Check the information.**
|
|
364
|
+
|
|
365
|
+
## ... yes I do :-)
|
|
366
|
+
|
|
367
|
+
***( ... but it was right on the money in the end. I love AI, albeit with a healthy grain of sceptic salt. )***
|
|
368
|
+
|
|
369
|
+
***( ... and I asked about MCs and Angular likewise, and it directly pointed me to a NG module problem that can also be solved. It even came up with sample code. )***
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Micro Component Management
|
|
2
|
+
|
|
3
|
+
This system allows you to attach, manage, and interact with micro components.
|
|
4
|
+
|
|
5
|
+
**Micro Components** can literally be anything you can put into an object as a property.
|
|
6
|
+
|
|
7
|
+
Also it has selectors to find micro components in the DOM. It consists of a single class `MC`.
|
|
8
|
+
|
|
9
|
+
## Summary of MC() Methods
|
|
10
|
+
|
|
11
|
+
- **Static Methods:**
|
|
12
|
+
- `MC.add( target, key, element)`: Attach a top level micro component to an HTMLElement.
|
|
13
|
+
- `MC.remove( target, key )`: Detach a top level micro component from an HTMLElement.
|
|
14
|
+
- `MC.siblings( target )`: get top level micro components from an HTMLElement.
|
|
15
|
+
|
|
16
|
+
- **Instance Methods:**
|
|
17
|
+
- `add( key, element )`: Attach a private Micro Component to a Micro Component.
|
|
18
|
+
- `remove( key )`: Detach a private Micro Component from a Micro Component.
|
|
19
|
+
- `siblings(search?)`: Retrieve sibling micro components.
|
|
20
|
+
- `parent(search?)`: Get parent top level micro components.
|
|
21
|
+
- `parents(search?)`: Get all ancestor top level micro components.
|
|
22
|
+
- `children(search?)`: Retrieve child top level micro components.
|
|
23
|
+
- `descendants(search?)`: Get all descendant top level micro components.
|
|
24
|
+
|
|
25
|
+
**Micro Components** will most likely be **class instances** that expose inner methods **or an observable collection** for data driven workflows, e.g. business process logic.
|
|
26
|
+
|
|
27
|
+
## Typical Workflow
|
|
28
|
+
|
|
29
|
+
1. **Attach micro components** to elements using `MC.add()`.
|
|
30
|
+
2. **Access** micro components by their property key or navigating through DOM (e.g., `siblings()`, `parents()`, `children()`, etc.) **and use their inner functionality**.
|
|
31
|
+
3. **Manipulate** micro components by adding or removing them as needed or change their properties/call their methods.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
## Hints
|
|
35
|
+
|
|
36
|
+
- **All static methods handle top level Micro Components. These are PUBLIC, that is they are visible in the DOM elements _mc property and usually have an interface.**
|
|
37
|
+
|
|
38
|
+
- **All MC instance methods handle inner Micro Components. These are PRIVATE, that is they are not visible in the DOM elements _mc property.**
|
|
39
|
+
|
|
40
|
+
- **Micro Components can recursively contain Micro Components. Just like npm and the node_modules directory.**
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## Attaching a top level Micro Component to a DOM element
|
|
44
|
+
|
|
45
|
+
To attach a micro component to an HTML element, use the `MC.add()` static function.
|
|
46
|
+
|
|
47
|
+
### Example Usage:
|
|
48
|
+
```javascript
|
|
49
|
+
const myElement = document.getElementById('myDiv');
|
|
50
|
+
|
|
51
|
+
// Attach a micro component (most often a class instance, here a simple object for simplicity) with a specific key
|
|
52
|
+
MC.add( myElement, 'myComponent', { name: 'Mike' } );
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- After you added a micro component using this function, the HTMLElement will contain a property `_mc` which is a MC() class instance that offers some methods.
|
|
56
|
+
- Also the HTMLElement then has an attribute _mc which will indicate that at least one micro component is attached to this dom element.
|
|
57
|
+
|
|
58
|
+
## Removing a top level Micro Component from a DOM element
|
|
59
|
+
|
|
60
|
+
Micro components can be removed using the `MC.remove()` static method.
|
|
61
|
+
|
|
62
|
+
### Removing a Micro component:
|
|
63
|
+
```javascript
|
|
64
|
+
MC.remove( myDomElement, 'myComponent');
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**When all micro components are removed** (the _mc object has no more properties), the `_mc` **attribute is also removed** from the HTML element. The `_mc` **property persists**.
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
## Retrieve all top level Micro Components from a DOM element
|
|
71
|
+
|
|
72
|
+
The `MC.siblings()` method retrieves all top level micro components attached to the same DOM element.
|
|
73
|
+
|
|
74
|
+
### Example:
|
|
75
|
+
```javascript
|
|
76
|
+
const target = document.querySelector( 'div' );
|
|
77
|
+
const siblings = MC.siblings( target );
|
|
78
|
+
console.log(siblings); // Logs an array of top level sibling micro components
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
To retrieve a specific micro component by its key:
|
|
82
|
+
```javascript
|
|
83
|
+
const target = document.querySelector( 'div' );
|
|
84
|
+
const siblings = MC.siblings( target, 'myOtherComponent' );
|
|
85
|
+
console.log(siblings); // Logs an array containing a top level sibling micro component, if its key matches the search string
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## Retrieving Sibling Micro components
|
|
90
|
+
|
|
91
|
+
The `.siblings()` method retrieves all micro components attached to the same DOM element.
|
|
92
|
+
|
|
93
|
+
### Example:
|
|
94
|
+
```javascript
|
|
95
|
+
const siblings = myElement._mc.siblings();
|
|
96
|
+
console.log(siblings); // Logs an array of sibling micro components
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
To retrieve a specific micro component by its key:
|
|
100
|
+
```javascript
|
|
101
|
+
const specificSibling = myElement._mc.siblings('anotherComponent');
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
- **The following methods hop over HTMLElements that contain no micro component.**
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
## 4. Navigating Parent and Ancestor Micro components
|
|
109
|
+
|
|
110
|
+
The `_mc.parent()` and `_mc.parents()` methods allow you to retrieve micro components attached to parent elements.
|
|
111
|
+
|
|
112
|
+
### Example:
|
|
113
|
+
```javascript
|
|
114
|
+
const parentMc = myElement._mc.parent();
|
|
115
|
+
console.log(parentMc); // Logs micro components closest nachestor that has microcomponents
|
|
116
|
+
|
|
117
|
+
const allAncestorsMc = myElement._mc.parents();
|
|
118
|
+
console.log(allAncestorsMc); // Logs all ancestor micro components up to document
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
To retrieve a specific micro component attached to the parent or ancestors:
|
|
122
|
+
```javascript
|
|
123
|
+
const specificAncestorMc = myElement._mc.parents('myComponent');
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## 5. Retrieving Child and Descendant Micro components
|
|
127
|
+
|
|
128
|
+
The `_mc.children()` and `_mc.descendants()` methods retrieve micro components attached to child elements or any descendants.
|
|
129
|
+
|
|
130
|
+
### Example:
|
|
131
|
+
```javascript
|
|
132
|
+
const childMicro components = myElement._mc.children();
|
|
133
|
+
console.log(childMicro components); // Logs an array of nearest child micro components
|
|
134
|
+
|
|
135
|
+
const descendantMicro components = myElement._mc.descendants();
|
|
136
|
+
console.log(descendantMicro components); // Logs micro components attached to all descendants
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
To search for a specific micro component by key:
|
|
140
|
+
```javascript
|
|
141
|
+
const specificMc = myElement._mc.<someComponent>;
|
|
142
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
## Why would I give this a try?
|
|
3
|
+
|
|
4
|
+
It is lightweight, proven and can yield positive results immediatly.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
### **As the CTO**:
|
|
8
|
+
|
|
9
|
+
- **Reusability**: this works with every Javascript framework that allows for DOM access, and it will work as long as the DOM exists in browsers. That is, forever.
|
|
10
|
+
- **Scalability**: scales perfectly by design.
|
|
11
|
+
- **Interoperability**: where many teams in different projects should share specific business or UI logic, you want to have something that works in any JS framework you use.
|
|
12
|
+
|
|
13
|
+
### **As a Project Manager**:
|
|
14
|
+
|
|
15
|
+
- **Separation of Concerns**: since this is a modular approach you can **split UI logic from Business logic** and let diffent teams work on each.
|
|
16
|
+
- using this, you can **work with external developers without the need to give them access to your projects code repository**.
|
|
17
|
+
- you will find out that this allows your project to **become independent from the "tools hell"**. You tend to use **DIY** modules where you had tools before.
|
|
18
|
+
|
|
19
|
+
### **As a Javascript Developer**:
|
|
20
|
+
|
|
21
|
+
- The learning curve to use this is more than shallow. 1 hour should be enough.
|
|
22
|
+
- This is based on **plain Javascript/Typescript, HTML and CSS**. There is no abstraction layer.
|
|
23
|
+
- From the very start **it is fun to work with**. You are back in control of your code without bigger frameworks, but you are able to interact with them.
|
|
24
|
+
- You can **work for any customer right away**. **No training on their system required**, just fulfill their micro component requirements.
|
|
25
|
+
- Over time, you can **build your own framework or functional libraries** from scratch.
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
<h5>obj: <span class="tsd-signature-type">any</span></h5></li></ul></div>
|
|
28
28
|
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">boolean</span></h4><aside class="tsd-sources">
|
|
29
29
|
<ul>
|
|
30
|
-
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/
|
|
30
|
+
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/f72dc84/src/microcomponents.es3.js#L3">microcomponents.es3.js:3</a></li></ul></aside></li></ul></section></div>
|
|
31
31
|
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
|
|
32
32
|
<div class="tsd-navigation settings">
|
|
33
33
|
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
|