@twobirds/microcomponents 0.0.72 → 0.0.73
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 +24 -14
- package/assets/documentation/MC.md +1 -1
- package/assets/documentation/MicroComponents.svg +15 -0
- package/assets/documentation/elements.md +18 -16
- package/assets/documentation/formvalues.md +5 -26
- package/assets/documentation/observables.md +18 -24
- package/package.json +1 -1
- package/public/documentation/MC.md +1 -1
- package/public/documentation/MicroComponents.svg +15 -0
- package/public/documentation/elements.md +18 -16
- package/public/documentation/formvalues.md +5 -26
- package/public/documentation/klemmstein.png +0 -0
- package/public/documentation/observables.md +18 -24
- package/public/typedoc/classes/MC.DC.html +2 -2
- package/public/typedoc/classes/MC.MC.html +2 -2
- package/public/typedoc/classes/MC.McBase.html +2 -2
- package/public/typedoc/classes/MC.McEvent.html +2 -2
- package/public/typedoc/functions/MC.addListener.html +1 -1
- package/public/typedoc/functions/MC.autoload.html +1 -1
- package/public/typedoc/functions/MC.removeListener.html +1 -1
- package/public/typedoc/functions/elements.defineCE.html +1 -1
- package/public/typedoc/functions/elements.getCE.html +1 -1
- package/public/typedoc/functions/formValues.formValues.html +1 -1
- package/public/typedoc/functions/observables.observe.html +1 -1
- package/public/typedoc/functions/selectors._d.html +1 -1
- package/public/typedoc/functions/selectors._t.html +1 -1
- package/public/typedoc/index.html +31 -17
- package/public/typedoc/interfaces/MC.HTMLMcElement.html +2 -2
- package/public/typedoc/interfaces/selectors.DSelector.html +2 -2
- package/public/typedoc/interfaces/selectors.TSelector.html +2 -2
- package/public/typedoc/modules/MC.html +1 -1
- package/public/typedoc/modules/elements.html +1 -1
- package/public/typedoc/modules/formValues.html +1 -1
- package/public/typedoc/modules/observables.html +1 -1
- package/public/typedoc/modules/selectors.html +1 -1
- package/public/typedoc/types/observables.Observable.html +1 -1
- package/public/typedoc/types/selectors.selection.html +1 -1
- package/public/typedoc/types/selectors.singleSelector.html +1 -1
- /package/{klemmstein.png → assets/documentation/klemmstein.png} +0 -0
package/README.md
CHANGED
|
@@ -4,19 +4,27 @@
|
|
|
4
4
|
|
|
5
5
|
**This code aims at improving Javascript modularity and encapsulation**.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Micro Components are built on **OOP** for structure, so you should embrace **DRY, KISS and SOLID** principles.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Its basic principle is **composition over inheritance**. It is strongly advised you do not extend your own classes, as it is not needed.
|
|
10
|
+
|
|
11
|
+
Also it helps to embrace **DIY** (Do It Yourself) **over Toolchains**, but this is up to your personal preference.
|
|
12
|
+
|
|
13
|
+
That said, feel free to code **functional**, or *4.5 dimensional ;-)* or any other approach I may have forgot or don't know. This system doesn't cage you.
|
|
14
|
+
|
|
15
|
+
You can use **Vanilla JS or Typescript** or both in your project. For example you could use plain JS for rapid prototyping functionalities, then convert to TS for production.
|
|
10
16
|
|
|
11
17
|
## Think **Building Blocks**
|
|
12
18
|
|
|
13
19
|
In general this is a **HTML / Web Standards first** system. There is almost no abstraction, especially not for templating, except state binding to the DOM via simple placeholders.
|
|
14
20
|
|
|
15
|
-
When coding with this system, you create classes that extend either the **[MC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MC.md?ref_type=heads#mc-class-extends-mcbase)** or **[DC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MC.md?ref_type=heads#dc-class-extends-mcbase)** superclasses provided here. These classes provide the structure you need to build your wonderland.
|
|
21
|
+
When coding with this system, you create classes that extend either the **[MC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MC.md?ref_type=heads#mc-class-extends-mcbase)** or **[DC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MC.md?ref_type=heads#dc-class-extends-mcbase)** superclasses provided here. These classes provide the structure you need to build your wonderland.
|
|
16
22
|
|
|
17
|
-
In an almost literal sense they work like childrens building blocks, where a chest full of unassuming parts lets you compose whatever functionality lies in your imagination.
|
|
23
|
+
You **attach instances of your classes to any HTMLElement**, and there can be as many as you want connected to a single HTML tag. In an almost literal sense they work like childrens building blocks, where a chest full of unassuming parts lets you compose whatever functionality lies in your imagination.
|
|
18
24
|
|
|
19
|
-
Also you can easily define
|
|
25
|
+
Also you can easily **define JS custom elements**. Since these are just another instance of a DC internally, you can enhance them even at runtime, as they are displayed in the browser.
|
|
26
|
+
|
|
27
|
+
## Principles
|
|
20
28
|
|
|
21
29
|
This systems **modular** approach gives you full control over your software, without the need to rely on other frameworks.
|
|
22
30
|
|
|
@@ -24,20 +32,22 @@ On the other hand, its **minimalistic** stance enables you to use the resulting
|
|
|
24
32
|
|
|
25
33
|
## Status
|
|
26
34
|
|
|
27
|
-
|
|
35
|
+
In [manual testing](https://twobirds.gitlab.io/microcomponents/test/test.html), also working a lot on the documentation.
|
|
28
36
|
|
|
29
37
|
## General
|
|
30
38
|
|
|
31
|
-
|
|
32
|
-
- You can use it **Vanilla JS or Typescript**.
|
|
33
|
-
|
|
34
|
-
This repository exports 5 independent functionalities:
|
|
39
|
+
This repository exports 4 independent functionalities:
|
|
35
40
|
|
|
36
41
|
1. **[Micro Components](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MC.md?ref_type=heads)** contains the **MC** and **DC** classes you can use as super classes.
|
|
37
|
-
2. **[Elements](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/elements.md?ref_type=heads)** lets you define and use your own native web components. Uses (1).
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
2. **[Elements](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/elements.md?ref_type=heads)** lets you define and use your own native web components. Uses (1).
|
|
43
|
+
- [Basic Usage](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/elements.md?ref_type=heads#create-a-custom-element).
|
|
44
|
+
- [Live Example](https://twobirds.gitlab.io/microcomponents/test/test.html)
|
|
45
|
+
3. **[Observables](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/observables.md?ref_type=heads)** add **observability** and **run time type safety** to object properties. Also they provide **1-way binding** to placeholders in your template HTML.
|
|
46
|
+
- [Basic Usage](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/observables.md?ref_type=heads#example)
|
|
47
|
+
- [Live Example](https://twobirds.gitlab.io/microcomponents/test/test.html)
|
|
48
|
+
4. **[Formvalues](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/formvalues.md?ref_type=heads)** gives you **2-way binding** between a HTML form and an observable object. Uses (3).
|
|
49
|
+
- [Example Code](https://gitlab.com/twoBirds/microcomponents/-/blob/main/src/test/test/testform.ts?ref_type=heads)
|
|
50
|
+
- [Live Example](https://twobirds.gitlab.io/microcomponents/test/test.html)
|
|
41
51
|
|
|
42
52
|
Also there is a bundle file that contains all of the above.
|
|
43
53
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
# Micro
|
|
3
|
+
# Micro Components
|
|
4
4
|
|
|
5
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
6
|
|
|
@@ -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>
|
|
@@ -21,10 +21,10 @@ Before:
|
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
```javascript
|
|
24
|
+
// customElementRegistry.define() wrapper
|
|
24
25
|
defineCE(
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
class extends DC {
|
|
26
|
+
"my-tag", // tag name
|
|
27
|
+
class MyTag extends DC { // definition class
|
|
28
28
|
|
|
29
29
|
constructor(target, data = {} ) {
|
|
30
30
|
super(target);
|
|
@@ -54,7 +54,7 @@ After:
|
|
|
54
54
|
</body>
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
**Hints**
|
|
58
58
|
- Your code extends the **[DC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MicroComponents.md?ref_type=heads#dc-class-extends-mcbase)** superclass. This superclass provides methods to traverse the DOM and select other micro components contained in HTML elements.
|
|
59
59
|
- it is essential to understand that your web component code is only one of the numerous micro components that can reside inside `<my-tag>`
|
|
60
60
|
- After you created a custom element, the custom element will contain a **property `<my-tag>._mc`** which contains your class instance.
|
|
@@ -62,10 +62,10 @@ After:
|
|
|
62
62
|
- Note that you can hand over data natively via parameters, when handling custom elements in above way.
|
|
63
63
|
- The native way of usage, simply adding the tag to the template, is still possible. You have to use attributes then to hand in data.
|
|
64
64
|
|
|
65
|
-
|
|
66
65
|
## Web Component Lifecycle Events
|
|
67
66
|
|
|
68
67
|
```javascript
|
|
68
|
+
// customElementRegistry.define() wrapper
|
|
69
69
|
defineCE(
|
|
70
70
|
"my-tag",
|
|
71
71
|
class MyTag extends DC {
|
|
@@ -98,12 +98,14 @@ defineCE(
|
|
|
98
98
|
['my-attribute'] // DOM node attributes to observe
|
|
99
99
|
);
|
|
100
100
|
|
|
101
|
-
let MyTag = getCE("my-tag"),
|
|
102
|
-
myTag = new MyTag();
|
|
101
|
+
let MyTag = getCE("my-tag"), // customElemtentRegistry.get("my-tag")
|
|
102
|
+
myTag = new MyTag(); // programmatically create a custom element instance
|
|
103
103
|
|
|
104
104
|
document.body.append( myTag );
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
## Composition with Micro Components
|
|
108
|
+
|
|
107
109
|
You can use your Web Component constructor to add other modules to the `<my-tag>` DOM node:
|
|
108
110
|
|
|
109
111
|
```javascript
|
|
@@ -142,12 +144,12 @@ let MyTag = getCE("my-tag");
|
|
|
142
144
|
document.body.append( new MyTag() );
|
|
143
145
|
```
|
|
144
146
|
|
|
145
|
-
|
|
147
|
+
**Hints**
|
|
146
148
|
- You can add as many other micro components to the DOM node as you like.
|
|
147
149
|
- The native web component lifecycle events will be triggered on **all** micro components.
|
|
148
150
|
- The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
|
|
149
151
|
|
|
150
|
-
## Native Event Handlers
|
|
152
|
+
## Native HTMLElement Event Handlers
|
|
151
153
|
|
|
152
154
|
Native event handlers will be auto-attached, when the method name follows one of these two schemes:
|
|
153
155
|
|
|
@@ -178,11 +180,11 @@ let MyTag = getCE("my-tag");
|
|
|
178
180
|
document.body.append( new MyTag() );
|
|
179
181
|
```
|
|
180
182
|
|
|
181
|
-
|
|
183
|
+
**Hints**
|
|
182
184
|
- The native web component lifecycle events will be triggered on **all** micro components contained in the custom element, provided the MC contains a matching named handler.
|
|
183
|
-
- The event parameter handed over is an internal event instance, that contains the original native event in its
|
|
185
|
+
- The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
|
|
184
186
|
|
|
185
|
-
|
|
187
|
+
# Lifecycle Event Handlers
|
|
186
188
|
|
|
187
189
|
Currently there is only one lifecycle event handler: the **oneInit(){ ... }** method you provide with your class.
|
|
188
190
|
|
|
@@ -212,11 +214,11 @@ let MyTag = getCE("my-tag");
|
|
|
212
214
|
document.body.append( new MyTag() );
|
|
213
215
|
```
|
|
214
216
|
|
|
215
|
-
|
|
217
|
+
**Hints**
|
|
216
218
|
- The native web component lifecycle events will be triggered on **all** micro components contained in the custom element, provided the specific micro component contains a matching named handler.
|
|
217
219
|
- The event parameter handed over is an internal event instance that contains no data.
|
|
218
220
|
|
|
219
|
-
|
|
221
|
+
# Custom Events
|
|
220
222
|
|
|
221
223
|
```javascript
|
|
222
224
|
defineCE(
|
|
@@ -274,5 +276,5 @@ sendSpecialEvent(){
|
|
|
274
276
|
}
|
|
275
277
|
```
|
|
276
278
|
|
|
277
|
-
|
|
278
|
-
- The event parameter handed over is an internal event instance, that contains the payload in its
|
|
279
|
+
**Hints**
|
|
280
|
+
- The event parameter handed over is an internal event instance, that contains the payload in its `data` property.
|
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Key Components
|
|
6
|
-
|
|
7
|
-
### InputBase (Abstract Class)
|
|
8
|
-
- Base class for all input handlers
|
|
9
|
-
- Defines `val` getter/setter interface
|
|
10
|
-
|
|
11
|
-
### Input Handlers
|
|
12
|
-
- **RadioHandler**: Manages radio button groups
|
|
13
|
-
- **CheckboxHandler**: Handles checkbox state
|
|
14
|
-
- **InputHandler**: For standard text inputs
|
|
15
|
-
- **SelectHandler**: Manages select dropdowns (single/multi-select)
|
|
16
|
-
- **TextAreaHandler**: For textarea elements
|
|
3
|
+
# Form Values
|
|
17
4
|
|
|
18
|
-
|
|
19
|
-
- Scans DOM for form elements
|
|
20
|
-
- Returns object with name-to-handler mappings
|
|
21
|
-
- Automatically creates appropriate handler type
|
|
5
|
+
This module provides a way to bind form input elements (text, radio, checkbox, select, textarea) to a JavaScript object, enabling two-way data binding and change observation.
|
|
22
6
|
|
|
23
|
-
|
|
7
|
+
## formValues Function
|
|
24
8
|
Binds form elements to a target object:
|
|
25
9
|
```ts
|
|
26
10
|
formValues(targetObject: LooseObject, propertyName: string, dom: HTMLElement | ShadowRoot)
|
|
@@ -31,16 +15,11 @@ Example code in constructor
|
|
|
31
15
|
constructor(target: HTMLElement) {
|
|
32
16
|
super(target);
|
|
33
17
|
|
|
34
|
-
const that = this;
|
|
35
|
-
|
|
36
|
-
delete that._mc; // not needed
|
|
37
|
-
|
|
38
18
|
let shadow = (that as DC).target.attachShadow({ mode: 'open' });
|
|
39
|
-
shadow.innerHTML = `<style>${
|
|
19
|
+
shadow.innerHTML = `<style>${this.css}</style>${this.template}`;
|
|
40
20
|
|
|
41
21
|
formValues(that, 'data', shadow); // create this.data formdata observable
|
|
42
22
|
|
|
43
|
-
(that.data as Observable).bind(shadow); // 1 way bind formdata to the form DOM
|
|
44
23
|
}
|
|
45
24
|
```
|
|
46
25
|
Full example [here](https://gitlab.com/twoBirds/microcomponents/-/blob/main/src/test/test/testform.ts?ref_type=heads).
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
# Observables
|
|
3
|
+
# Observables
|
|
4
4
|
|
|
5
5
|
This module provides utilities for observing and binding data to DOM elements using placeholders. It supports reactive updates when data changes.
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ This module provides utilities for observing and binding data to DOM elements us
|
|
|
9
9
|
- [Overview](#overview)
|
|
10
10
|
- [Functions](#functions)
|
|
11
11
|
- [Types](#types)
|
|
12
|
-
- [Example
|
|
12
|
+
- [Example](#example)
|
|
13
13
|
|
|
14
14
|
---
|
|
15
15
|
|
|
@@ -32,7 +32,7 @@ Registers a distinct property or all object properties as an observable, making
|
|
|
32
32
|
Those properties are dual use:
|
|
33
33
|
|
|
34
34
|
- **Direct Assign Native Values**:
|
|
35
|
-
- Directly assign native values to the property, and the observable will be notified of the change.
|
|
35
|
+
- Directly assign native values to the property, and the observable will be notified of the change and its callbacks will be run.
|
|
36
36
|
|
|
37
37
|
- **Use as an Instance of Observable**
|
|
38
38
|
- Use the property as an instance of the `Observable` class. This allows you to attach callbacks and react to changes. You can bind the property to DOM elements.
|
|
@@ -41,7 +41,7 @@ Those properties are dual use:
|
|
|
41
41
|
|
|
42
42
|
### `Observable`
|
|
43
43
|
|
|
44
|
-
**
|
|
44
|
+
**Type**
|
|
45
45
|
```typescript
|
|
46
46
|
export type Observable = {
|
|
47
47
|
observe: (cb: Callback) => void; // attach a callback to be notified of changes
|
|
@@ -59,47 +59,41 @@ export type Observable = {
|
|
|
59
59
|
- Enables / Disables notifications.
|
|
60
60
|
- By default, notifications are enabled.
|
|
61
61
|
- If `enable` is `false`, notifications are disabled. You need to explicitly call `notify(true)` to re-enable notifications.
|
|
62
|
-
-
|
|
62
|
+
- Otherwise calls all registered callbacks with the current data.
|
|
63
63
|
|
|
64
64
|
- `bind(target: HTMLElement | DocumentFragment | ShadowRoot)`
|
|
65
65
|
Binds the observable to a DOM element, replacing placeholders with data values.
|
|
66
66
|
|
|
67
67
|
|
|
68
|
-
## Example
|
|
68
|
+
## Example
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
HTML:
|
|
71
|
+
```html
|
|
72
|
+
<div id="user-card">
|
|
73
|
+
<p>Hello, {name}</p>
|
|
74
|
+
<p>Age: {age}</p>
|
|
75
|
+
</div>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
JavaScript:
|
|
79
|
+
```javascript
|
|
80
|
+
import { observe } from './observables.js';
|
|
72
81
|
|
|
73
82
|
const data = {
|
|
74
83
|
name: 'John',
|
|
75
84
|
age: 30,
|
|
76
85
|
};
|
|
77
86
|
|
|
78
|
-
observe(data);
|
|
87
|
+
observe(data); // all properties of the data object will be observed.
|
|
79
88
|
|
|
80
89
|
const element = document.getElementById('user-card');
|
|
81
90
|
data.bind(element);
|
|
82
91
|
```
|
|
83
92
|
|
|
84
|
-
In HTML:
|
|
85
|
-
```html
|
|
86
|
-
<div id="user-card">
|
|
87
|
-
<p>Hello, {name}</p>
|
|
88
|
-
<p>Age: {age}</p>
|
|
89
|
-
</div>
|
|
90
|
-
```
|
|
91
|
-
|
|
92
93
|
When `data.name` or `data.age` changes, the DOM updates automatically.
|
|
93
94
|
|
|
94
95
|
---
|
|
95
96
|
|
|
96
|
-
## Dependencies
|
|
97
|
-
|
|
98
|
-
- `helpers.js` - Provides utilities like `deepEqual`, `debounce`, `flattenObject`, etc.
|
|
99
|
-
- `MC.js` - Contains `DC`, `HTMLMcElement`, and `McEvent` for DOM manipulation and event handling.
|
|
100
|
-
|
|
101
|
-
---
|
|
102
|
-
|
|
103
97
|
## Notes
|
|
104
98
|
|
|
105
99
|
- Only objects can be bound to the DOM.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
# Micro
|
|
3
|
+
# Micro Components
|
|
4
4
|
|
|
5
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
6
|
|
|
@@ -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>
|
|
@@ -21,10 +21,10 @@ Before:
|
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
```javascript
|
|
24
|
+
// customElementRegistry.define() wrapper
|
|
24
25
|
defineCE(
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
class extends DC {
|
|
26
|
+
"my-tag", // tag name
|
|
27
|
+
class MyTag extends DC { // definition class
|
|
28
28
|
|
|
29
29
|
constructor(target, data = {} ) {
|
|
30
30
|
super(target);
|
|
@@ -54,7 +54,7 @@ After:
|
|
|
54
54
|
</body>
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
**Hints**
|
|
58
58
|
- Your code extends the **[DC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MicroComponents.md?ref_type=heads#dc-class-extends-mcbase)** superclass. This superclass provides methods to traverse the DOM and select other micro components contained in HTML elements.
|
|
59
59
|
- it is essential to understand that your web component code is only one of the numerous micro components that can reside inside `<my-tag>`
|
|
60
60
|
- After you created a custom element, the custom element will contain a **property `<my-tag>._mc`** which contains your class instance.
|
|
@@ -62,10 +62,10 @@ After:
|
|
|
62
62
|
- Note that you can hand over data natively via parameters, when handling custom elements in above way.
|
|
63
63
|
- The native way of usage, simply adding the tag to the template, is still possible. You have to use attributes then to hand in data.
|
|
64
64
|
|
|
65
|
-
|
|
66
65
|
## Web Component Lifecycle Events
|
|
67
66
|
|
|
68
67
|
```javascript
|
|
68
|
+
// customElementRegistry.define() wrapper
|
|
69
69
|
defineCE(
|
|
70
70
|
"my-tag",
|
|
71
71
|
class MyTag extends DC {
|
|
@@ -98,12 +98,14 @@ defineCE(
|
|
|
98
98
|
['my-attribute'] // DOM node attributes to observe
|
|
99
99
|
);
|
|
100
100
|
|
|
101
|
-
let MyTag = getCE("my-tag"),
|
|
102
|
-
myTag = new MyTag();
|
|
101
|
+
let MyTag = getCE("my-tag"), // customElemtentRegistry.get("my-tag")
|
|
102
|
+
myTag = new MyTag(); // programmatically create a custom element instance
|
|
103
103
|
|
|
104
104
|
document.body.append( myTag );
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
## Composition with Micro Components
|
|
108
|
+
|
|
107
109
|
You can use your Web Component constructor to add other modules to the `<my-tag>` DOM node:
|
|
108
110
|
|
|
109
111
|
```javascript
|
|
@@ -142,12 +144,12 @@ let MyTag = getCE("my-tag");
|
|
|
142
144
|
document.body.append( new MyTag() );
|
|
143
145
|
```
|
|
144
146
|
|
|
145
|
-
|
|
147
|
+
**Hints**
|
|
146
148
|
- You can add as many other micro components to the DOM node as you like.
|
|
147
149
|
- The native web component lifecycle events will be triggered on **all** micro components.
|
|
148
150
|
- The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
|
|
149
151
|
|
|
150
|
-
## Native Event Handlers
|
|
152
|
+
## Native HTMLElement Event Handlers
|
|
151
153
|
|
|
152
154
|
Native event handlers will be auto-attached, when the method name follows one of these two schemes:
|
|
153
155
|
|
|
@@ -178,11 +180,11 @@ let MyTag = getCE("my-tag");
|
|
|
178
180
|
document.body.append( new MyTag() );
|
|
179
181
|
```
|
|
180
182
|
|
|
181
|
-
|
|
183
|
+
**Hints**
|
|
182
184
|
- The native web component lifecycle events will be triggered on **all** micro components contained in the custom element, provided the MC contains a matching named handler.
|
|
183
|
-
- The event parameter handed over is an internal event instance, that contains the original native event in its
|
|
185
|
+
- The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
|
|
184
186
|
|
|
185
|
-
|
|
187
|
+
# Lifecycle Event Handlers
|
|
186
188
|
|
|
187
189
|
Currently there is only one lifecycle event handler: the **oneInit(){ ... }** method you provide with your class.
|
|
188
190
|
|
|
@@ -212,11 +214,11 @@ let MyTag = getCE("my-tag");
|
|
|
212
214
|
document.body.append( new MyTag() );
|
|
213
215
|
```
|
|
214
216
|
|
|
215
|
-
|
|
217
|
+
**Hints**
|
|
216
218
|
- The native web component lifecycle events will be triggered on **all** micro components contained in the custom element, provided the specific micro component contains a matching named handler.
|
|
217
219
|
- The event parameter handed over is an internal event instance that contains no data.
|
|
218
220
|
|
|
219
|
-
|
|
221
|
+
# Custom Events
|
|
220
222
|
|
|
221
223
|
```javascript
|
|
222
224
|
defineCE(
|
|
@@ -274,5 +276,5 @@ sendSpecialEvent(){
|
|
|
274
276
|
}
|
|
275
277
|
```
|
|
276
278
|
|
|
277
|
-
|
|
278
|
-
- The event parameter handed over is an internal event instance, that contains the payload in its
|
|
279
|
+
**Hints**
|
|
280
|
+
- The event parameter handed over is an internal event instance, that contains the payload in its `data` property.
|
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Key Components
|
|
6
|
-
|
|
7
|
-
### InputBase (Abstract Class)
|
|
8
|
-
- Base class for all input handlers
|
|
9
|
-
- Defines `val` getter/setter interface
|
|
10
|
-
|
|
11
|
-
### Input Handlers
|
|
12
|
-
- **RadioHandler**: Manages radio button groups
|
|
13
|
-
- **CheckboxHandler**: Handles checkbox state
|
|
14
|
-
- **InputHandler**: For standard text inputs
|
|
15
|
-
- **SelectHandler**: Manages select dropdowns (single/multi-select)
|
|
16
|
-
- **TextAreaHandler**: For textarea elements
|
|
3
|
+
# Form Values
|
|
17
4
|
|
|
18
|
-
|
|
19
|
-
- Scans DOM for form elements
|
|
20
|
-
- Returns object with name-to-handler mappings
|
|
21
|
-
- Automatically creates appropriate handler type
|
|
5
|
+
This module provides a way to bind form input elements (text, radio, checkbox, select, textarea) to a JavaScript object, enabling two-way data binding and change observation.
|
|
22
6
|
|
|
23
|
-
|
|
7
|
+
## formValues Function
|
|
24
8
|
Binds form elements to a target object:
|
|
25
9
|
```ts
|
|
26
10
|
formValues(targetObject: LooseObject, propertyName: string, dom: HTMLElement | ShadowRoot)
|
|
@@ -31,16 +15,11 @@ Example code in constructor
|
|
|
31
15
|
constructor(target: HTMLElement) {
|
|
32
16
|
super(target);
|
|
33
17
|
|
|
34
|
-
const that = this;
|
|
35
|
-
|
|
36
|
-
delete that._mc; // not needed
|
|
37
|
-
|
|
38
18
|
let shadow = (that as DC).target.attachShadow({ mode: 'open' });
|
|
39
|
-
shadow.innerHTML = `<style>${
|
|
19
|
+
shadow.innerHTML = `<style>${this.css}</style>${this.template}`;
|
|
40
20
|
|
|
41
21
|
formValues(that, 'data', shadow); // create this.data formdata observable
|
|
42
22
|
|
|
43
|
-
(that.data as Observable).bind(shadow); // 1 way bind formdata to the form DOM
|
|
44
23
|
}
|
|
45
24
|
```
|
|
46
25
|
Full example [here](https://gitlab.com/twoBirds/microcomponents/-/blob/main/src/test/test/testform.ts?ref_type=heads).
|
|
Binary file
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
-
# Observables
|
|
3
|
+
# Observables
|
|
4
4
|
|
|
5
5
|
This module provides utilities for observing and binding data to DOM elements using placeholders. It supports reactive updates when data changes.
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ This module provides utilities for observing and binding data to DOM elements us
|
|
|
9
9
|
- [Overview](#overview)
|
|
10
10
|
- [Functions](#functions)
|
|
11
11
|
- [Types](#types)
|
|
12
|
-
- [Example
|
|
12
|
+
- [Example](#example)
|
|
13
13
|
|
|
14
14
|
---
|
|
15
15
|
|
|
@@ -32,7 +32,7 @@ Registers a distinct property or all object properties as an observable, making
|
|
|
32
32
|
Those properties are dual use:
|
|
33
33
|
|
|
34
34
|
- **Direct Assign Native Values**:
|
|
35
|
-
- Directly assign native values to the property, and the observable will be notified of the change.
|
|
35
|
+
- Directly assign native values to the property, and the observable will be notified of the change and its callbacks will be run.
|
|
36
36
|
|
|
37
37
|
- **Use as an Instance of Observable**
|
|
38
38
|
- Use the property as an instance of the `Observable` class. This allows you to attach callbacks and react to changes. You can bind the property to DOM elements.
|
|
@@ -41,7 +41,7 @@ Those properties are dual use:
|
|
|
41
41
|
|
|
42
42
|
### `Observable`
|
|
43
43
|
|
|
44
|
-
**
|
|
44
|
+
**Type**
|
|
45
45
|
```typescript
|
|
46
46
|
export type Observable = {
|
|
47
47
|
observe: (cb: Callback) => void; // attach a callback to be notified of changes
|
|
@@ -59,47 +59,41 @@ export type Observable = {
|
|
|
59
59
|
- Enables / Disables notifications.
|
|
60
60
|
- By default, notifications are enabled.
|
|
61
61
|
- If `enable` is `false`, notifications are disabled. You need to explicitly call `notify(true)` to re-enable notifications.
|
|
62
|
-
-
|
|
62
|
+
- Otherwise calls all registered callbacks with the current data.
|
|
63
63
|
|
|
64
64
|
- `bind(target: HTMLElement | DocumentFragment | ShadowRoot)`
|
|
65
65
|
Binds the observable to a DOM element, replacing placeholders with data values.
|
|
66
66
|
|
|
67
67
|
|
|
68
|
-
## Example
|
|
68
|
+
## Example
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
HTML:
|
|
71
|
+
```html
|
|
72
|
+
<div id="user-card">
|
|
73
|
+
<p>Hello, {name}</p>
|
|
74
|
+
<p>Age: {age}</p>
|
|
75
|
+
</div>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
JavaScript:
|
|
79
|
+
```javascript
|
|
80
|
+
import { observe } from './observables.js';
|
|
72
81
|
|
|
73
82
|
const data = {
|
|
74
83
|
name: 'John',
|
|
75
84
|
age: 30,
|
|
76
85
|
};
|
|
77
86
|
|
|
78
|
-
observe(data);
|
|
87
|
+
observe(data); // all properties of the data object will be observed.
|
|
79
88
|
|
|
80
89
|
const element = document.getElementById('user-card');
|
|
81
90
|
data.bind(element);
|
|
82
91
|
```
|
|
83
92
|
|
|
84
|
-
In HTML:
|
|
85
|
-
```html
|
|
86
|
-
<div id="user-card">
|
|
87
|
-
<p>Hello, {name}</p>
|
|
88
|
-
<p>Age: {age}</p>
|
|
89
|
-
</div>
|
|
90
|
-
```
|
|
91
|
-
|
|
92
93
|
When `data.name` or `data.age` changes, the DOM updates automatically.
|
|
93
94
|
|
|
94
95
|
---
|
|
95
96
|
|
|
96
|
-
## Dependencies
|
|
97
|
-
|
|
98
|
-
- `helpers.js` - Provides utilities like `deepEqual`, `debounce`, `flattenObject`, etc.
|
|
99
|
-
- `MC.js` - Contains `DC`, `HTMLMcElement`, and `McEvent` for DOM manipulation and event handling.
|
|
100
|
-
|
|
101
|
-
---
|
|
102
|
-
|
|
103
97
|
## Notes
|
|
104
98
|
|
|
105
99
|
- Only objects can be bound to the DOM.
|