@twobirds/microcomponents 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -38
- package/dist/MC.min.js +1 -1
- package/dist/autoload.min.js +1 -1
- package/dist/core/MC.d.ts +5 -2
- package/dist/core/MC.d.ts.map +1 -1
- package/dist/core/MC.js +52 -4
- package/dist/core/elements.d.ts +186 -100
- package/dist/core/elements.d.ts.map +1 -1
- package/dist/core/elements.js +186 -17
- package/dist/core/mcGuards.d.ts +1 -0
- package/dist/core/mcGuards.d.ts.map +1 -1
- package/dist/core/mcGuards.js +0 -10
- package/dist/elements.min.js +1 -1
- package/dist/events.min.js +1 -1
- package/dist/formValues.min.js +1 -1
- package/dist/microcomponents.d.ts +2 -2
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.js +2 -2
- package/dist/microcomponents.min.js +1 -1
- package/dist/observables.min.js +1 -1
- package/dist/state/formValues.d.ts +111 -66
- package/dist/state/formValues.d.ts.map +1 -1
- package/dist/state/formValues.js +132 -68
- package/dist/state/observables.d.ts +68 -83
- package/dist/state/observables.d.ts.map +1 -1
- package/dist/state/observables.js +124 -84
- package/dist/utils/autoload.d.ts.map +1 -0
- package/dist/{core → utils}/autoload.js +2 -2
- package/dist/{core → utils}/events.d.ts +22 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/{core → utils}/events.js +26 -1
- package/dist/utils/helpers.d.ts +105 -20
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/utils/helpers.js +210 -25
- package/package.json +12 -11
- package/dist/core/autoload.d.ts.map +0 -1
- package/dist/core/events.d.ts.map +0 -1
- /package/dist/{core → utils}/autoload.d.ts +0 -0
package/README.md
CHANGED
|
@@ -18,21 +18,22 @@ The distinguishing feature - compared to other frameworks - is its **minimalism*
|
|
|
18
18
|
|
|
19
19
|
## Status
|
|
20
20
|
|
|
21
|
-
Automatic tests added.
|
|
21
|
+
Automatic tests added. Typedoc needs some work still.
|
|
22
22
|
|
|
23
|
-
[
|
|
23
|
+
[demo shop (on-demand loading)](https://twobirds.gitlab.io/microcomponents/shop/index.html) loads every component as a single file. Nice to check the code, it is readable.
|
|
24
24
|
|
|
25
|
-
[demo shop (
|
|
25
|
+
[demo shop (bundled in one JS file)](https://twobirds.gitlab.io/microcomponents/shop/index-bundled.html). 9 kB including the library.
|
|
26
26
|
|
|
27
27
|
Check the [typedoc API reference](https://twobirds.gitlab.io/microcomponents/typedoc/index.html) if you want to drill into the source code.
|
|
28
28
|
|
|
29
29
|
The progress of coding is being streamed on [twitch](https://twitch.tv/frankieth) and [youtube](https://www.youtube.com/@FrankieThu).
|
|
30
30
|
|
|
31
|
-
##
|
|
31
|
+
## Usage Examples
|
|
32
32
|
|
|
33
|
-
### **
|
|
33
|
+
### **Functional Principle**
|
|
34
|
+
---
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
*The following code only shows how Micro Components are stored in the DOM.*
|
|
36
37
|
|
|
37
38
|
html before:
|
|
38
39
|
|
|
@@ -57,13 +58,14 @@ class MyMC extends DC {
|
|
|
57
58
|
// get some element from the DOM
|
|
58
59
|
const element = document.querySelector('div');
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const host = DC.add(element, 'myMC', new MyMC(element));
|
|
61
|
+
// Add the custom micro component instance to the target element programatically
|
|
62
|
+
DC.add(element, 'myMC', new MyMC(element));
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
// Hello World
|
|
65
|
+
element
|
|
66
|
+
._mc
|
|
67
|
+
.myMC
|
|
68
|
+
.helloWorld();
|
|
67
69
|
```
|
|
68
70
|
|
|
69
71
|
html after:
|
|
@@ -77,8 +79,9 @@ The `data-mc` **attribute** indicates that this **HTML element contains at least
|
|
|
77
79
|
The `_mc` **property** is the only interference with the DOM, so any other system should be happy to work with this.
|
|
78
80
|
|
|
79
81
|
### **Web Components / Custom Elements**
|
|
82
|
+
---
|
|
80
83
|
|
|
81
|
-
You can define a custom element using a micro component as its primary functional instance.
|
|
84
|
+
You can define a custom element using a micro component as its primary functional instance. You are still free to add many more micro components to the custom element at runtime.
|
|
82
85
|
|
|
83
86
|
code:
|
|
84
87
|
|
|
@@ -114,7 +117,12 @@ const body = document.body;
|
|
|
114
117
|
body.append(new myComponentClass());
|
|
115
118
|
```
|
|
116
119
|
|
|
120
|
+
For more examples regarding web components, go to the [docs](https://twobirds.gitlab.io/microcomponents/typedoc/modules/core_elements.html)
|
|
121
|
+
and check the loaded sources from the [demo shop](https://twobirds.gitlab.io/microcomponents/shop/index.html).
|
|
122
|
+
There is a lot to see...
|
|
123
|
+
|
|
117
124
|
### **State and 1-way-binding**
|
|
125
|
+
---
|
|
118
126
|
|
|
119
127
|
Declare states and directly connect them to DOM placeholders:
|
|
120
128
|
code:
|
|
@@ -150,6 +158,7 @@ class MyMC extends DC {
|
|
|
150
158
|
```
|
|
151
159
|
|
|
152
160
|
### **Forms and 2-way-binding**
|
|
161
|
+
---
|
|
153
162
|
|
|
154
163
|
Similar to observables above, you can 2-way-bind an observable object to form input fields. Changes in the object properties trigger input field changes and vice versa.
|
|
155
164
|
|
|
@@ -176,6 +185,7 @@ class MyMC extends DC {
|
|
|
176
185
|
```
|
|
177
186
|
|
|
178
187
|
### **Event Handlers**
|
|
188
|
+
---
|
|
179
189
|
|
|
180
190
|
#### Native Events
|
|
181
191
|
|
|
@@ -256,31 +266,6 @@ Most of all this system by design **allows for outsourcing without sharing the m
|
|
|
256
266
|
|
|
257
267
|
Finally it allows for a clear distinction between `Junior`, `Senior` and `Architect` roles in a project. No more hiring junior programmers with 5 years experience in 50 tools under their belt. A junior in this system is somebody who likes HTML, CSS and JS/TS - nothing more. Teaching all of this system starting at zero takes 1 day. The rest is finger muscle memory and learning/knowing web standards.
|
|
258
268
|
|
|
259
|
-
## Documentation
|
|
260
|
-
|
|
261
|
-
This repository exports 4 independent functionalities:
|
|
262
|
-
|
|
263
|
-
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.
|
|
264
|
-
|
|
265
|
-
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).
|
|
266
|
-
|
|
267
|
-
- [Basic Usage](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/elements.md?ref_type=heads#create-a-custom-element).
|
|
268
|
-
- [Live Example](https://twobirds.gitlab.io/microcomponents/test/test.html)
|
|
269
|
-
|
|
270
|
-
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.
|
|
271
|
-
|
|
272
|
-
- [Basic Usage](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/observables.md?ref_type=heads#example)
|
|
273
|
-
- [Live Example](https://twobirds.gitlab.io/microcomponents/test/test.html)
|
|
274
|
-
|
|
275
|
-
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).
|
|
276
|
-
|
|
277
|
-
- [Example Code](https://gitlab.com/twoBirds/microcomponents/-/blob/main/src/test/test/testform.ts?ref_type=heads)
|
|
278
|
-
- [Live Example](https://twobirds.gitlab.io/microcomponents/test/test.html)
|
|
279
|
-
|
|
280
|
-
Also there is a bundle file that contains all of the above.
|
|
281
|
-
|
|
282
|
-
To see what can be done, go to [additional concepts](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/magic.md?ref_type=heads) which shows what it already has been used for.
|
|
283
|
-
|
|
284
269
|
## Distinguishing Features
|
|
285
270
|
|
|
286
271
|
There are a few potentially unique aspects of this micro components system that might differentiate it from other framework or library:
|
|
@@ -312,3 +297,7 @@ I decided to extract this functionality from the [twoBirds framework](https://ww
|
|
|
312
297
|
Later this will be backported into twoBirds as a dependency, and twoBirds will become completely modularized on the base of MCs.
|
|
313
298
|
|
|
314
299
|
**This is a massive undertaking.** MCs are just the foundation of what I do. I am about to dump [20+ years of javascript programming](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/magic.md?ref_type=heads#things-i-already-did-with-this-in-my-own-framework-twobirds-or-in-apps-that-i-developed-with-it) into open source libraries, so feel free to [fund this](https://www.gofundme.com/f/nachhaltige-programmierung). The better this is funded, the faster it will be done. Given enough resources, I will start a crowdfunded company that will develop a new open source ecosystem for javascript/typescript programming.
|
|
300
|
+
|
|
301
|
+
## Deprecated test:
|
|
302
|
+
|
|
303
|
+
[manual testing](https://twobirds.gitlab.io/microcomponents/test/index.html) ( also a speed test).
|
package/dist/MC.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(n,r,i){if(n){if(typeof n==`string`){document.querySelectorAll(n).forEach(e=>t(e,r,i));return}n.removeEventListener(e(n,r),i)}}function n(t,r,i,a=!1){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>n(e,r,i));return}return t.addEventListener(e(t,r),i,{capture:!1,once:a}),i}}function r(e,t,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,t,i));return}return n(e,t,i,!0),i}}var i=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function a(t,n,r={},o=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>a(e,n,r,o));return}let c=/^_mc/.test(n)?n:e(t,n),l=s||new i,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(o)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(o)&&queueMicrotask(()=>{if(o.indexOf(`u`)>-1&&a(t.parentElement,c,r,`lu`,l),o.indexOf(`d`)>-1)for(let e of t.children)a(e,c,r,`ld`)})}function o(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function
|
|
1
|
+
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(n,r,i){if(n){if(typeof n==`string`){document.querySelectorAll(n).forEach(e=>t(e,r,i));return}n.removeEventListener(e(n,r),i)}}function n(t,r,i,a=!1){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>n(e,r,i));return}return t.addEventListener(e(t,r),i,{capture:!1,once:a}),i}}function r(e,t,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,t,i));return}return n(e,t,i,!0),i}}var i=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function a(t,n,r={},o=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>a(e,n,r,o));return}let c=/^_mc/.test(n)?n:e(t,n),l=s||new i,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(o)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(o)&&queueMicrotask(()=>{if(o.indexOf(`u`)>-1&&a(t.parentElement,c,r,`lu`,l),o.indexOf(`d`)>-1)for(let e of t.children)a(e,c,r,`ld`)})}function o(e,t){let n=new CSSStyleSheet;return n.replaceSync(t),e.adoptedStyleSheets.push(n),n}function s(e,t){let n=e.target,r=n.shadowRoot;!r&&Object.keys(t).some(e=>e.indexOf(`shadow`)===0)&&(r=n.attachShadow({mode:`open`})),Object.keys(t).forEach(i=>{switch(i){case`css`:o(document,t[i]);break;case`targetCss`:o(document,t[i]);break;case`shadowCss`:o(r,t[i]);break;case`shadowHtml`:r.innerHTML=t[i],l(r,e);break;case`targetHtml`:n.innerHTML=t[i],l(n,e);break}})}function c(e,t){let n=t=>{let n=t.match(/^([a-z]+)([A-Z][a-z]*)$/);return n?`./${e.replace(/-/g,`/`)}.${n[1]}.${n[2].toLowerCase()}`:`./${e.replace(/-/g,`/`)}.${t.toLowerCase()}`};return Promise.all(t.map(async e=>{let t=n(e);try{let n=await fetch(t);if(!n.ok)return`file not found: ${t}`;if(t.endsWith(`json`)){let r=await n.text();try{return JSON.parse(r)}catch{return console.error(`could not JSON.parse() asset '${e}' from ${t}:\n`,r),null}}return n.text()}catch{return`file not found: ${t}`}}))}function l(e,t){let r=[...e.querySelectorAll(`*`)];for(let e of r){if(!(e instanceof HTMLElement))continue;let r=[...e.attributes];for(let i of r){let r=i.name.toLowerCase();if(!(`on`+r in e))continue;let o=i.value.trim().split(`|`);if(o.length<2)continue;let[s,c,l]=o;e.removeAttribute(i.name);let u;if(l)try{u=JSON.parse(l)}catch{u=l}s===`this`||s===``?n(e,r,()=>t.trigger(c,u)):n(e,r,()=>a(s,c,u))}}}function u(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function d(e,t,n={}){let r=e;`_mc`in e||(r._mc=new y(e));let i=r._mc;return t.length&&!i[t]&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function f(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function p(e){queueMicrotask(()=>{e.trigger(`Init`,null)})}function m(e){let t=e.target,r=Object.getPrototypeOf(e),i=e,a=Object.getOwnPropertyNames(r).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof r[e]==`function`);for(let e of a){let r=/^one/.test(e);n(t,e.replace(/^one?/,``),t=>i[e](t),r)}}function h(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function g(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var _=class{_target;constructor(e){if(this._target=new WeakRef(e||{}),this instanceof y&&this.constructor!==y){m(this);let e=this.constructor.tag;(e===void 0||e.toUpperCase()===this.target.tagName)&&p(this)}}get target(){return this._target.deref()}},v=class extends _{constructor(e){super(e)}},y=class e extends _{static tag;static assets=[];static attributes={};constructor(t){super(t);let n=this.constructor,r=this.target.tagName.toLowerCase();if(n.tag!==void 0&&r!==n.tag&&n.tag!==``){let t=document.createElement(n.tag);t.classList.add(r),e.add(t,n.name,new n(t)),r.includes(`-`)&&!this.target.isConnected?this.on(`connected`,()=>this.target.replaceWith(t)):this.target.replaceWith(t);return}if(n.assets.length&&c(r,n.assets).then(e=>{let t=Object.fromEntries(n.assets.map((t,n)=>[t,e[n]]));s(this,t),this.trigger(`assets`,t)}),n.tag===``){this.target.remove();return}let i=n.attributes;n.attributes&&Object.keys(n.attributes).forEach(e=>{this.target.setAttribute(e,i[e])})}static add(e,t,n={}){return d(e,t,n)}static remove(e,t){f(e,t)}static get(e,t,n){if(!u(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,n){t(this.target,e,n)}on(e,t){return n(this.target,e,t)}one(e,t){return r(this.target,e,t)}trigger(e,t={},n=`l`,r){a(this.target,e,t,n,r)}ancestors(e){let t=[],n=g(this.target,e);for(;n;)t.push(n),n=g(n.target,e);return t}closest(e){return g(this.target,e)}children(e){let t=h(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};export{y as DC,v as MC,_ as McBase,u as isMcDOM};
|
package/dist/autoload.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(n,r,i){if(n){if(typeof n==`string`){document.querySelectorAll(n).forEach(e=>t(e,r,i));return}n.removeEventListener(e(n,r),i)}}function n(t,r,i,a=!1){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>n(e,r,i));return}return t.addEventListener(e(t,r),i,{capture:!1,once:a}),i}}function r(e,t,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,t,i));return}return n(e,t,i,!0),i}}var i=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function a(t,n,r={},o=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>a(e,n,r,o));return}let c=/^_mc/.test(n)?n:e(t,n),l=s||new i,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(o)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(o)&&queueMicrotask(()=>{if(o.indexOf(`u`)>-1&&a(t.parentElement,c,r,`lu`,l),o.indexOf(`d`)>-1)for(let e of t.children)a(e,c,r,`ld`)})}function o(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function
|
|
1
|
+
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(n,r,i){if(n){if(typeof n==`string`){document.querySelectorAll(n).forEach(e=>t(e,r,i));return}n.removeEventListener(e(n,r),i)}}function n(t,r,i,a=!1){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>n(e,r,i));return}return t.addEventListener(e(t,r),i,{capture:!1,once:a}),i}}function r(e,t,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,t,i));return}return n(e,t,i,!0),i}}var i=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function a(t,n,r={},o=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>a(e,n,r,o));return}let c=/^_mc/.test(n)?n:e(t,n),l=s||new i,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(o)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(o)&&queueMicrotask(()=>{if(o.indexOf(`u`)>-1&&a(t.parentElement,c,r,`lu`,l),o.indexOf(`d`)>-1)for(let e of t.children)a(e,c,r,`ld`)})}function o(e,t){let n=new CSSStyleSheet;return n.replaceSync(t),e.adoptedStyleSheets.push(n),n}function s(e,t){let n=e.target,r=n.shadowRoot;!r&&Object.keys(t).some(e=>e.indexOf(`shadow`)===0)&&(r=n.attachShadow({mode:`open`})),Object.keys(t).forEach(i=>{switch(i){case`css`:o(document,t[i]);break;case`targetCss`:o(document,t[i]);break;case`shadowCss`:o(r,t[i]);break;case`shadowHtml`:r.innerHTML=t[i],l(r,e);break;case`targetHtml`:n.innerHTML=t[i],l(n,e);break}})}function c(e,t){let n=t=>{let n=t.match(/^([a-z]+)([A-Z][a-z]*)$/);return n?`./${e.replace(/-/g,`/`)}.${n[1]}.${n[2].toLowerCase()}`:`./${e.replace(/-/g,`/`)}.${t.toLowerCase()}`};return Promise.all(t.map(async e=>{let t=n(e);try{let n=await fetch(t);if(!n.ok)return`file not found: ${t}`;if(t.endsWith(`json`)){let r=await n.text();try{return JSON.parse(r)}catch{return console.error(`could not JSON.parse() asset '${e}' from ${t}:\n`,r),null}}return n.text()}catch{return`file not found: ${t}`}}))}function l(e,t){let r=[...e.querySelectorAll(`*`)];for(let e of r){if(!(e instanceof HTMLElement))continue;let r=[...e.attributes];for(let i of r){let r=i.name.toLowerCase();if(!(`on`+r in e))continue;let o=i.value.trim().split(`|`);if(o.length<2)continue;let[s,c,l]=o;e.removeAttribute(i.name);let u;if(l)try{u=JSON.parse(l)}catch{u=l}s===`this`||s===``?n(e,r,()=>t.trigger(c,u)):n(e,r,()=>a(s,c,u))}}}function u(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function d(e,t,n={}){let r=e;`_mc`in e||(r._mc=new v(e));let i=r._mc;return t.length&&!i[t]&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function f(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function p(e){queueMicrotask(()=>{e.trigger(`Init`,null)})}function m(e){let t=e.target,r=Object.getPrototypeOf(e),i=e,a=Object.getOwnPropertyNames(r).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof r[e]==`function`);for(let e of a){let r=/^one/.test(e);n(t,e.replace(/^one?/,``),t=>i[e](t),r)}}function h(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function g(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var _=class{_target;constructor(e){if(this._target=new WeakRef(e||{}),this instanceof v&&this.constructor!==v){m(this);let e=this.constructor.tag;(e===void 0||e.toUpperCase()===this.target.tagName)&&p(this)}}get target(){return this._target.deref()}},v=class e extends _{static tag;static assets=[];static attributes={};constructor(t){super(t);let n=this.constructor,r=this.target.tagName.toLowerCase();if(n.tag!==void 0&&r!==n.tag&&n.tag!==``){let t=document.createElement(n.tag);t.classList.add(r),e.add(t,n.name,new n(t)),r.includes(`-`)&&!this.target.isConnected?this.on(`connected`,()=>this.target.replaceWith(t)):this.target.replaceWith(t);return}if(n.assets.length&&c(r,n.assets).then(e=>{let t=Object.fromEntries(n.assets.map((t,n)=>[t,e[n]]));s(this,t),this.trigger(`assets`,t)}),n.tag===``){this.target.remove();return}let i=n.attributes;n.attributes&&Object.keys(n.attributes).forEach(e=>{this.target.setAttribute(e,i[e])})}static add(e,t,n={}){return d(e,t,n)}static remove(e,t){f(e,t)}static get(e,t,n){if(!u(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,n){t(this.target,e,n)}on(e,t){return n(this.target,e,t)}one(e,t){return r(this.target,e,t)}trigger(e,t={},n=`l`,r){a(this.target,e,t,n,r)}ancestors(e){let t=[],n=g(this.target,e);for(;n;)t.push(n),n=g(n.target,e);return t}closest(e){return g(this.target,e)}children(e){let t=h(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}},y=new Set;function b(e){return()=>{let t=Array.from(document.querySelectorAll(`[data-mc*="${e}"]`)).filter(e=>e instanceof HTMLElement);if(t.length===0)return;let n=customElements.get(e);for(let r of t){try{v.add(r,n.name,new n(r))}catch{if(`create`in n&&typeof n.create==`function`){let e=n.create(r);if(!u(e))throw`create() of Class ${n.name} did not return a McHost.`;e.setAttribute(`data-mc`,r.getAttribute(`data-mc`)??``),r.replaceWith(e),r=e}else throw`Class ${n.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`}let t=r.getAttribute(`data-mc`).split(` `).filter(t=>t!==e&&t!==``).join(` `).trim();r.setAttribute(`data-mc`,t)}}}function x(e){if(y.has(e))return;y.add(e);let t=`./`+e.split(`-`).join(`/`)+`.js`,n=document.createElement(`script`);n.setAttribute(`src`,t),n.setAttribute(`blocking`,`render`),n.async=!0,n.setAttribute(`type`,`module`),n.setAttribute(`data-name`,e),n.setAttribute(`loading`,``),customElements.whenDefined(e).then(b(e)),n.addEventListener(`load`,t=>{y.delete(e),document.head.querySelector(`script[data-name="${e}"]`)?.remove()}),n.addEventListener(`error`,e=>{console.error(`could not load DC code from`,t)}),document.head.append(n)}function S(){[...document.querySelectorAll(`:not(:defined)`)].filter(e=>!!e&&!customElements.get(e.tagName.toLowerCase())&&!y.has(e.tagName.toLowerCase())).forEach(e=>{x(e.tagName.toLowerCase())}),[...document.querySelectorAll(`[data-mc]:not([data-mc=""]`)].filter(e=>!!e).forEach(e=>{(e.getAttribute(`data-mc`)?.split(` `).filter(e=>e!==``))?.forEach(e=>{let t=customElements.get(e);!y.has(e)&&!t?x(e):t&&b(e)()})})}var C=null,w=function(e=!0){return e?C?!0:(C=new MutationObserver(S),setTimeout(()=>{C.observe(document.body,{childList:!0,subtree:!0}),S()},5),window.autoload??=w,!0):(C&&=(C.disconnect(),null),!1)};w.state=y;export{w as autoload};
|
package/dist/core/MC.d.ts
CHANGED
|
@@ -154,7 +154,7 @@ The `data-mc` attribute indicates that this element contains at least one microc
|
|
|
154
154
|
---
|
|
155
155
|
|
|
156
156
|
*/
|
|
157
|
-
import { BubbleController } from '
|
|
157
|
+
import { BubbleController } from '../utils/events.js';
|
|
158
158
|
import type { BubbleDirections, McController, McEntriesOf, McEventHandler, McHost, McHostOf, McMerged, McPayloadOf, McRemoved } from '../types/mcTypes.js';
|
|
159
159
|
export type { McBubbleController, McController, McDetail, McEntriesOf, McEvent, McEventHandler, McHost, McHostOf, McMerged, McNativeEventHandlers, McPayloadOf, McRemoved, } from '../types/mcTypes.js';
|
|
160
160
|
export { isMcDOM } from './mcGuards.js';
|
|
@@ -232,6 +232,9 @@ class Cart extends DC<HTMLFormElement, { CartUpdate: { count: number } }> {
|
|
|
232
232
|
```
|
|
233
233
|
*/
|
|
234
234
|
declare class DC<T extends HTMLElement = HTMLElement, EM extends Record<string, unknown> = Record<never, never>> extends McBase<T> implements McController<T> {
|
|
235
|
+
static tag?: string;
|
|
236
|
+
static assets: string[];
|
|
237
|
+
static attributes?: object;
|
|
235
238
|
constructor(target: T);
|
|
236
239
|
/**
|
|
237
240
|
Static helper to attach a microcomponent instance to a DOM element.
|
|
@@ -338,7 +341,7 @@ declare class DC<T extends HTMLElement = HTMLElement, EM extends Record<string,
|
|
|
338
341
|
@param bubble - Bubbling direction ('l', 'u', 'd' or combinations).
|
|
339
342
|
@param controller - Optional controller for managing bubbling behavior.
|
|
340
343
|
*/
|
|
341
|
-
trigger<D = unknown, K extends string = string>(eventName: K, data?: McPayloadOf<EM, K, D>, bubble?: BubbleDirections, controller?: BubbleController): void;
|
|
344
|
+
trigger<D = unknown, K extends string = keyof EM & string>(eventName: K, data?: McPayloadOf<EM, K, D>, bubble?: BubbleDirections, controller?: BubbleController): void;
|
|
342
345
|
/**
|
|
343
346
|
Returns an array of all ancestor microcomponents up the DOM tree.
|
|
344
347
|
|
package/dist/core/MC.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MC.d.ts","sourceRoot":"","sources":["../../src/core/MC.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2JE;
|
|
1
|
+
{"version":3,"file":"MC.d.ts","sourceRoot":"","sources":["../../src/core/MC.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2JE;AAKF,OAAO,EAAyB,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC7E,OAAO,KAAK,EACX,gBAAgB,EAChB,YAAY,EACZ,WAAW,EAEX,cAAc,EACd,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,WAAW,EACX,SAAS,EACT,MAAM,qBAAqB,CAAC;AAG7B,YAAY,EACX,kBAAkB,EAClB,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,OAAO,EACP,cAAc,EACd,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,qBAAqB,EACrB,WAAW,EACX,SAAS,GACT,MAAM,qB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MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI;IAKjG;;;;;;;;;MASE;IACF,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,EAAE;IAC1F,gEAAgE;IAChE,QAAQ,CAAC,CAAC,SAAS,MAAM,UAAU,GAAG,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE;IAM7F;;;;;;;;;MASE;IACF,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,EAAE;IAC7F,qEAAqE;IACrE,WAAW,CAAC,CAAC,SAAS,MAAM,UAAU,GAAG,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE;CAShG;AAED,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/core/MC.js
CHANGED
|
@@ -155,7 +155,8 @@ The `data-mc` attribute indicates that this element contains at least one microc
|
|
|
155
155
|
|
|
156
156
|
*/
|
|
157
157
|
'use strict';
|
|
158
|
-
import {
|
|
158
|
+
import { loadAssets, processAssets } from '../utils/helpers.js';
|
|
159
|
+
import { trigger, on, one, off } from '../utils/events.js';
|
|
159
160
|
import { isMcDOM } from './mcGuards.js';
|
|
160
161
|
export { isMcDOM } from './mcGuards.js';
|
|
161
162
|
function add(targetHTMLElement, key, element = {}) {
|
|
@@ -222,7 +223,7 @@ signature encodes that invariant instead of re-checking it at runtime.
|
|
|
222
223
|
*/
|
|
223
224
|
function init(element) {
|
|
224
225
|
queueMicrotask(() => {
|
|
225
|
-
element.trigger('Init');
|
|
226
|
+
element.trigger('Init', null);
|
|
226
227
|
});
|
|
227
228
|
}
|
|
228
229
|
/**
|
|
@@ -304,9 +305,13 @@ class McBase {
|
|
|
304
305
|
*/
|
|
305
306
|
constructor(target) {
|
|
306
307
|
this._target = new WeakRef(target || {});
|
|
307
|
-
if (this instanceof DC) {
|
|
308
|
+
if (this instanceof DC && this.constructor !== DC) {
|
|
308
309
|
autoAttachListeners(this);
|
|
309
|
-
|
|
310
|
+
const tag = this.constructor.tag;
|
|
311
|
+
if (tag === undefined || tag.toUpperCase() === this.target.tagName) {
|
|
312
|
+
init(this);
|
|
313
|
+
}
|
|
314
|
+
;
|
|
310
315
|
}
|
|
311
316
|
}
|
|
312
317
|
/**
|
|
@@ -369,8 +374,51 @@ class Cart extends DC<HTMLFormElement, { CartUpdate: { count: number } }> {
|
|
|
369
374
|
```
|
|
370
375
|
*/
|
|
371
376
|
class DC extends McBase {
|
|
377
|
+
static tag;
|
|
378
|
+
static assets = [];
|
|
379
|
+
static attributes = {};
|
|
372
380
|
constructor(target) {
|
|
373
381
|
super(target);
|
|
382
|
+
const ctor = this.constructor;
|
|
383
|
+
const tagName = this.target.tagName.toLowerCase();
|
|
384
|
+
// work on tag:
|
|
385
|
+
if (ctor['tag'] !== undefined && tagName !== ctor.tag && ctor.tag !== '') {
|
|
386
|
+
const element = document.createElement(ctor.tag);
|
|
387
|
+
element.classList.add(tagName);
|
|
388
|
+
DC.add(element, ctor.name, new ctor(element));
|
|
389
|
+
if (tagName.includes('-') && !this.target.isConnected) {
|
|
390
|
+
this.on('connected', () => this.target.replaceWith(element));
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
this.target.replaceWith(element);
|
|
394
|
+
}
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
// work on assets:
|
|
398
|
+
if (ctor.assets.length) {
|
|
399
|
+
// - initiate loading...
|
|
400
|
+
loadAssets(tagName, ctor.assets)
|
|
401
|
+
// - returns a promise, .then( ()=>{ this.trigger('render') } )
|
|
402
|
+
.then((res) => {
|
|
403
|
+
const assets = Object.fromEntries(ctor.assets.map((key, i) => [key, res[i]]));
|
|
404
|
+
processAssets(this, assets);
|
|
405
|
+
this.trigger('assets', assets);
|
|
406
|
+
});
|
|
407
|
+
// - in the component, have a method onRender(){ ... }
|
|
408
|
+
// - no explicit async needed in component code
|
|
409
|
+
}
|
|
410
|
+
if (ctor.tag === '') {
|
|
411
|
+
this.target.remove();
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
;
|
|
415
|
+
// work on attributes
|
|
416
|
+
const attributes = ctor.attributes;
|
|
417
|
+
if (ctor.attributes) {
|
|
418
|
+
Object.keys(ctor.attributes).forEach((key) => {
|
|
419
|
+
this.target.setAttribute(key, attributes[key]);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
374
422
|
}
|
|
375
423
|
static add(target, key, element = {}) {
|
|
376
424
|
return add(target, key, element);
|
package/dist/core/elements.d.ts
CHANGED
|
@@ -1,48 +1,217 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* # Custom Elements
|
|
3
|
+
* ---
|
|
4
4
|
* Defines micro component integration with native web components and the CustomElementRegistry.
|
|
5
|
-
*
|
|
6
5
|
* This module provides utilities for defining custom HTML elements using the native Web Components API.
|
|
7
6
|
*
|
|
8
7
|
* ---
|
|
9
|
-
*
|
|
10
8
|
* The {@link defineCE} function handles registration of custom elements with the CustomElementRegistry,
|
|
11
9
|
* automatically attaching the provided class as a DC instance ( micro component ) to the native web component created.
|
|
12
10
|
* This class and any DC instances added to the web component later will receive all web component native events.
|
|
13
11
|
* Only the class that defines the WC also defines its observed attributes, but all inner DCs get the changed event.
|
|
14
|
-
*
|
|
15
12
|
* The {@link getCE} function is just a wrapper for customElements.get()
|
|
16
13
|
*
|
|
17
|
-
*
|
|
14
|
+
* ## Hints:
|
|
15
|
+
* - Your micro component must extend DC, since it is a DOM element
|
|
16
|
+
* - Method names that look like event handlers are DOM - or custom event handlers ( 'onClick()', 'oneSomething()' )
|
|
17
|
+
* - Event handlers defined as methods will be assigned to the DOM automatically
|
|
18
|
+
* - Micro components trigger an `init` event on themself after the constructor has run. The method `.oneInit()` catches this lifecycle event.
|
|
18
19
|
*
|
|
19
|
-
*
|
|
20
|
-
* ```javascript
|
|
21
|
-
* // create a custom micro component class
|
|
22
|
-
* class MyComponent extends DC{
|
|
20
|
+
* ---
|
|
23
21
|
*
|
|
22
|
+
* @module elements
|
|
23
|
+
*
|
|
24
|
+
* ## Examples:
|
|
25
|
+
*
|
|
26
|
+
* @example define a custom component, minimum code
|
|
27
|
+
*
|
|
28
|
+
* javascript:
|
|
29
|
+
* ```javascript
|
|
30
|
+
* class MyElement extends DC {
|
|
24
31
|
* constructor( target ){
|
|
25
|
-
* super(
|
|
32
|
+
* super(target);
|
|
26
33
|
* }
|
|
27
|
-
*
|
|
28
34
|
* }
|
|
29
35
|
*
|
|
30
|
-
* defineCE( 'my-
|
|
36
|
+
* defineCE( 'my-element', MyElement );
|
|
31
37
|
* ```
|
|
32
38
|
*
|
|
33
39
|
* afterwards you can use ...
|
|
40
|
+
*
|
|
41
|
+
* html:
|
|
34
42
|
* ```HTML
|
|
35
|
-
* <my-
|
|
43
|
+
* <my-element></my-element>
|
|
36
44
|
* ```
|
|
37
45
|
* ... in your template.
|
|
38
46
|
*
|
|
39
|
-
* Or you can construct it programmatically:
|
|
47
|
+
* Or you can construct it programmatically using {@link getCE}:
|
|
48
|
+
*
|
|
49
|
+
* javascript:
|
|
40
50
|
* ```javascript
|
|
41
|
-
* const
|
|
51
|
+
* const MyElementClass = getCE('my-element');
|
|
42
52
|
* const body = document.body;
|
|
43
|
-
* body.append( new
|
|
53
|
+
* body.append( new MyElementClass() );
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* ---
|
|
57
|
+
* @example define a custom component with standard and/or custom event handlers
|
|
58
|
+
*
|
|
59
|
+
* javascript:
|
|
60
|
+
* ```javascript
|
|
61
|
+
* class MyElement extends DC {
|
|
62
|
+
*
|
|
63
|
+
* constructor( target ){
|
|
64
|
+
* super(target);
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* // DOM web component event handler
|
|
68
|
+
* onConnected() {
|
|
69
|
+
* this.innerHTML = '<p>Hi!</p>';
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* // DOM standard event handler
|
|
73
|
+
* onClick(){
|
|
74
|
+
* this.trigger('something', { data: 'do something' } );
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* // DOM custom event handler
|
|
78
|
+
* oneSomething(ev){
|
|
79
|
+
* // do only once! Subsequent clicks will not trigger this handler
|
|
80
|
+
* console.log('something happens here:', ev.detail.data );
|
|
81
|
+
* }
|
|
82
|
+
* }
|
|
83
|
+
* defineCE( 'my-element', MyElement );
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* ## Static properties
|
|
87
|
+
*
|
|
88
|
+
* DC classes support some static properties that configure the custom element
|
|
89
|
+
* before any instance is constructed. These are applied automatically by `defineCE`
|
|
90
|
+
* during element creation.
|
|
91
|
+
*
|
|
92
|
+
* ---
|
|
93
|
+
* @example static tag
|
|
94
|
+
* - replaces the default `target` element with a semantic HTML element:
|
|
95
|
+
*
|
|
96
|
+
* html before:
|
|
97
|
+
* ```html
|
|
98
|
+
* <shop-header></shop-header>
|
|
99
|
+
* ```
|
|
100
|
+
* javascript:
|
|
101
|
+
* ```javascript
|
|
102
|
+
* class ShopHeader extends DC {
|
|
103
|
+
* static tag = 'header';
|
|
104
|
+
* constructor(target) {
|
|
105
|
+
* super(target);
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* defineCE('shop-header', ShopHeader);
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* html after:
|
|
112
|
+
* ```html
|
|
113
|
+
* <header></header>
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* The resulting DOM node is a `<header>` element instead of a generic template.
|
|
117
|
+
*
|
|
118
|
+
* ---
|
|
119
|
+
* @example static assets
|
|
120
|
+
* - declares companion files for auto-load and bundling by convention:
|
|
121
|
+
*
|
|
122
|
+
* javascript:
|
|
123
|
+
* ```javascript
|
|
124
|
+
* class CartContainerCheckout extends DC {
|
|
125
|
+
* static assets = ['targetHtml', 'targetCss'];
|
|
126
|
+
* constructor(target) {
|
|
127
|
+
* super(target);
|
|
128
|
+
* }
|
|
129
|
+
* oneAssets() {
|
|
130
|
+
* this.setupBehaviour();
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
133
|
+
* defineCE('cart-container-checkout', CartContainerCheckout);
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* The framework resolves `targetHtml` → `checkout.target.html` and
|
|
137
|
+
* `targetCss` → `checkout.target.css`, injects them into the element, and fires
|
|
138
|
+
* the `_mcAssets` event so that `oneAssets()` runs.
|
|
139
|
+
*
|
|
140
|
+
* ---
|
|
141
|
+
* @example static attributes
|
|
142
|
+
* - sets HTML attributes on every instance:
|
|
143
|
+
*
|
|
144
|
+
* javascript:
|
|
145
|
+
* ```javascript
|
|
146
|
+
* class ProductCard extends DC {
|
|
147
|
+
* static attributes = { role: 'listitem' };
|
|
148
|
+
* constructor(target, data) {
|
|
149
|
+
* super(target);
|
|
150
|
+
* }
|
|
151
|
+
* }
|
|
152
|
+
* defineCE('product-card', ProductCard);
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* Every `<product-card>` element carries the attribute `role="listitem"` automatically:
|
|
156
|
+
*
|
|
157
|
+
* html after:
|
|
158
|
+
* ```html
|
|
159
|
+
* <product-card role="listitem"></product-card>
|
|
44
160
|
* ```
|
|
161
|
+
*
|
|
45
162
|
* ---
|
|
163
|
+
* @example
|
|
164
|
+
* - All three can be combined in one class:
|
|
165
|
+
*
|
|
166
|
+
* javascript:
|
|
167
|
+
* ```javascript
|
|
168
|
+
* class ShopCatalog extends DC {
|
|
169
|
+
* static tag = 'section';
|
|
170
|
+
* static attributes = { role: 'list', 'aria-label': 'products' };
|
|
171
|
+
* static assets = ['targetHtml', 'targetCss'];
|
|
172
|
+
* constructor(target) {
|
|
173
|
+
* super(target);
|
|
174
|
+
* }
|
|
175
|
+
* }
|
|
176
|
+
* defineCE('shop-catalog', ShopCatalog);
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* Produces a `<section role="list" aria-label="products">` host element.
|
|
180
|
+
*
|
|
181
|
+
* ---
|
|
182
|
+
* @example Creating a user defined built-in element (customized built-in element)
|
|
183
|
+
* **Hint:**
|
|
184
|
+
* - This is **DEPRECATED** in the standard. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements#customized_built-in_elements). Use this only if you know what you are doing.
|
|
185
|
+
* - This can be archieved in a safe way using the `static tag: 'aside';` property on your class.
|
|
186
|
+
*
|
|
187
|
+
* javascript:
|
|
188
|
+
* ```javascript
|
|
189
|
+
* class MyButton extends DC {
|
|
190
|
+
* constructor(target) {
|
|
191
|
+
* super(target);
|
|
192
|
+
* }
|
|
193
|
+
* onConnected() {
|
|
194
|
+
* this.textContent = 'Click me!';
|
|
195
|
+
* }
|
|
196
|
+
* }
|
|
197
|
+
* defineCE('my-button', MyButton, [], 'button');
|
|
198
|
+
* ```
|
|
199
|
+
*
|
|
200
|
+
* afterwards you can use it in your template with the `is` attribute:
|
|
201
|
+
*
|
|
202
|
+
* html:
|
|
203
|
+
* ```html
|
|
204
|
+
* <button is="my-button"></button>
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
*
|
|
208
|
+
* Or you can construct it programmatically using {@link getCE}:
|
|
209
|
+
* javascript:
|
|
210
|
+
* ```javascript
|
|
211
|
+
* const MyButtonClass = getCE('my-button');
|
|
212
|
+
* const body = document.body;
|
|
213
|
+
* body.append( new MyButtonClass() );
|
|
214
|
+
* ```
|
|
46
215
|
*/
|
|
47
216
|
import { type McHost } from './MC.js';
|
|
48
217
|
/**
|
|
@@ -120,89 +289,6 @@ the element instance with DC (DomComponent).
|
|
|
120
289
|
tag name was already defined. Feed it (or an equivalent `McElementConstructor`) to
|
|
121
290
|
the global {@link McElementTagNameMap} to make `getCE()` return it typed by string.
|
|
122
291
|
|
|
123
|
-
@example define a custom component
|
|
124
|
-
|
|
125
|
-
---
|
|
126
|
-
|
|
127
|
-
javascript:
|
|
128
|
-
```javascript
|
|
129
|
-
class MyElement extends DC {
|
|
130
|
-
|
|
131
|
-
constructor( target ){
|
|
132
|
-
super(target);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// DOM web component event handler
|
|
136
|
-
onConnected() {
|
|
137
|
-
this.innerHTML = '<p>Hi!</p>';
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// DOM standard event handler
|
|
141
|
-
onClick(){
|
|
142
|
-
this.trigger('something', { data: 'do something' } );
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// DOM custom event handler
|
|
146
|
-
oneSomething(ev){
|
|
147
|
-
// do only once! Subsequent clicks will not trigger this handler
|
|
148
|
-
console.log('something happens here:', ev.detail.data );
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
defineCE( 'my-element', MyElement );
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
afterwards you can use ...
|
|
156
|
-
html:
|
|
157
|
-
```HTML
|
|
158
|
-
<my-element></my-element>
|
|
159
|
-
```
|
|
160
|
-
... in your template.
|
|
161
|
-
|
|
162
|
-
Or you can construct it programmatically using {@link getCE}:
|
|
163
|
-
javascript:
|
|
164
|
-
```javascript
|
|
165
|
-
const MyElementClass = getCE('my-element');
|
|
166
|
-
const body = document.body;
|
|
167
|
-
body.append( new MyElementClass() );
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
@example Extending a built-in element (customized built-in element)
|
|
171
|
-
|
|
172
|
-
---
|
|
173
|
-
|
|
174
|
-
javascript:
|
|
175
|
-
```javascript
|
|
176
|
-
class MyButton extends DC {
|
|
177
|
-
constructor(target) {
|
|
178
|
-
super(target);
|
|
179
|
-
}
|
|
180
|
-
onConnected() {
|
|
181
|
-
this.textContent = 'Click me!';
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
defineCE('my-button', MyButton, [], 'button');
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
afterwards you can use it in your template with the `is` attribute:
|
|
189
|
-
```HTML
|
|
190
|
-
<button is="my-button"></button>
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
Or you can construct it programmatically using {@link getCE}:
|
|
195
|
-
javascript:
|
|
196
|
-
```javascript
|
|
197
|
-
const MyButtonClass = getCE('my-button');
|
|
198
|
-
const body = document.body;
|
|
199
|
-
body.append( new MyButtonClass() );
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
Hints:
|
|
203
|
-
- Your micro component must extend DC, since it is a DOM element
|
|
204
|
-
- method names that look like event handlers are DOM - or custom event handlers ( 'onClick()', 'oneSomething()' )
|
|
205
|
-
- event handlers defined as methods will be assigned to the DOM automatically
|
|
206
292
|
*/
|
|
207
293
|
export declare function defineCE<Tag extends `${string}-${string}`, T extends object, D, E extends keyof HTMLElementTagNameMap | undefined = undefined>(tagName: Tag, DefinitionClass: new (target: TargetOf<E>, data?: D) => T, observedAttributes?: string[], extend?: E): McElementConstructor<T, D, E, McPascalCase<Tag>> | undefined;
|
|
208
294
|
export declare function defineCE<Tag extends `${string}-${string}`, T extends object, E extends keyof HTMLElementTagNameMap | undefined = undefined>(tagName: Tag, DefinitionClass: new (target: TargetOf<E>) => T, observedAttributes?: string[], extend?: E): McElementConstructor<T, undefined, E, McPascalCase<Tag>> | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../../src/core/elements.ts"],"names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