2024-11-22 10:20:59 +01:00
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1" > < meta name = "description" content = "Using custom elements in combinantion with DDE" > < title > `deka-dom-el` — Web Components< / title > <!-- <dde:mark type="component" name="metaAuthor" host="this" ssr/> --> < meta name = "author" content = "Jan Andrle" > < link type = "text/plain" rel = "author" href = "https://jaandrle.github.io/humans.txt" > < meta name = "generator" content = "deka-dom-el" > <!-- <dde:mark type="component" name="metaTwitter" host="this" ssr/> --> < meta name = "twitter:card" content = "summary_large_image" > < meta name = "twitter:url" content = "https://github.com/jaandrle/deka-dom-el" > < meta name = "twitter:title" content = "deka-dom-el" > < meta name = "twitter:description" content = "A low-code library that simplifies the creation of native DOM elements/components using small wrappers and tweaks." > < meta name = "twitter:creator" content = "@jaandrle" > <!-- <dde:mark type="component" name="metaFacebook" host="this" ssr/> --> < meta name = "og:url" content = "https://github.com/jaandrle/deka-dom-el" > < meta name = "og:title" content = "deka-dom-el" > < meta name = "og:description" content = "A low-code library that simplifies the creation of native DOM elements/components using small wrappers and tweaks." > < meta name = "og:creator" content = "@jaandrle" > < script src = "https://cdn.jsdelivr.net/npm/shiki@0.9" defer = "" > < / script > < script type = "module" src = "code.js.js" > < / script > < script src = "https://flems.io/flems.html" type = "text/javascript" charset = "utf-8" > < / script > < link rel = "stylesheet" href = "global.css" > < / head > < body > <!-- <dde:mark type="component" name="page" host="this" ssr/> --> <!-- <dde:mark type="component" name="simplePage" host="this" ssr/> --> <!-- <dde:mark type="component" name="header" host="this" ssr/> --> < header > < h1 > `deka-dom-el` — Web Components< / h1 > < p > Using custom elements in combinantion with DDE< / p > < / header > < nav > < a href = "https://github.com/jaandrle/deka-dom-el" > < svg class = "icon" viewBox = "0 0 32 32" > <!-- <dde:mark type="component" name="iconGitHub" host="parentElement" ssr/> --> < path d = "M 16,0.395c -8.836,0 -16,7.163 -16,16c 0,7.069 4.585,13.067 10.942,15.182c 0.8,0.148 1.094,-0.347 1.094,-0.77c 0,-0.381 -0.015,-1.642 -0.022,-2.979c -4.452,0.968 -5.391,-1.888 -5.391,-1.888c -0.728,-1.849 -1.776,-2.341 -1.776,-2.341c -1.452,-0.993 0.11,-0.973 0.11,-0.973c 1.606,0.113 2.452,1.649 2.452,1.649c 1.427,2.446 3.743,1.739 4.656,1.33c 0.143,-1.034 0.558,-1.74 1.016,-2.14c -3.554,-0.404 -7.29,-1.777 -7.29,-7.907c 0,-1.747 0.625,-3.174 1.649,-4.295c -0.166,-0.403 -0.714,-2.03 0.155,-4.234c 0,0 1.344,-0.43 4.401,1.64c 1.276,-0.355 2.645,-0.532 4.005,-0.539c 1.359,0.006 2.729,0.184 4.008,0.539c 3.054,-2.07 4.395,-1.64 4.395,-1.64c 0.871,2.204 0.323,3.831 0.157,4.234c 1.026,1.12 1.647,2.548 1.647,4.295c 0,6.145 -3.743,7.498 -7.306,7.895c 0.574,0.497 1.085,1.47 1.085,2.963c 0,2.141 -0.019,3.864 -0.019,4.391c 0,0.426 0.288,0.925 1.099,0.768c 6.354,-2.118 10.933,-8.113 10.933,-15.18c 0,-8.837 -7.164,-16 -16,-16Z" > < / path > < / svg > GitHub< / a > < a href = "./" title = "Introducing a library." > 1. Introduction< / a > < a href = "p02-elements" title = "Basic concepts of elements modifications and creations." > 2. Elements< / a > < a href = "p03-events" title = "Using not only events in UI declaratively." > 3. Events and Addons< / a > < a href = "p04-signals" title = "Handling reactivity in UI via signals." > 4. Signals and reactivity< / a > < a href = "p05-scopes" title = "Organizing UI into components" > 5. Scopes and components< / a > < a href = "p06-customElement" title = "Using custom elements in combinantion with DDE" class = "current" > 6. Web Components< / a > < / nav > < main > < h2 > Using web components in combinantion with DDE< / h2 > < p > The DDE library allows for use within < a title = "MDN documentation page for Web Components" href = "https://developer.mozilla.org/en-US/docs/Web/API/Web_components" > < strong > Web Components< / strong > < / a > for dom-tree generation. However, in order to be able to use signals (possibly mapping to registered < a title = "MDN documentation page for observedAttributes" href = "https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using
2024-01-09 21:18:43 +01:00
import {
customElementRender,
customElementWithDDE,
observedAttributes,
} from "deka-dom-el";
/** @type {ddePublicElementTagNameMap} */
2024-05-23 22:18:19 +02:00
import { S } from "deka-dom-el/signals";
2024-05-22 21:43:49 +02:00
S.observedAttributes;
2024-01-09 21:18:43 +01:00
// “internal” utils
2024-02-03 14:36:17 +01:00
import { lifecyclesToEvents } from "deka-dom-el";
2024-11-22 10:20:59 +01:00
< / code > < / div > < h3 id = "h-custom-elements-introduction" > <!-- <dde:mark type="component" name="h3" host="parentElement" ssr/> --> < a href = "#h-custom-elements-introduction" tabindex = "-1" > #< / a > Custom Elements Introduction< / h3 > < p > Web Components, specifically Custom Elements, are a set of web platform APIs that allow you to create new HTML tags with custom functionality encapsulated within them. This allows for the creation of reusable components that can be used across web applications. < / p > < p > To start with, let’ s see how to use native Custom Elements. As starting point please read < a title = "MDN documentation page for Custom Elements" href = "https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements" > < strong > Using Custom Elements< / strong > on MDN< / a > . To sum up and for mnemonic see following code overview:< / p > < div class = "code" data-js = "todo" > <!-- <dde:mark type="component" name="code" host="parentElement" ssr/> --> < code class = "language-js" > export class HTMLCustomElement extends HTMLElement{
static tagName= "custom-element"; // just suggestion, we can use `el(HTMLCustomElement.tagName)`
2024-01-09 21:18:43 +01:00
static observedAttributes= [ "custom-attribute" ];
constructor(){
super();
// nice place to prepare custom element
}
connectedCallback(){
// nice place to render custom element
}
attributeChangedCallback(name, oldValue, newValue){
// listen to attribute changes (see `observedAttributes`)
}
disconnectedCallback(){
// nice place to clean up
}
// for example, we can mirror get/set prop to attribute
get customAttribute(){ return this.getAttribute("custom-attribute"); }
set customAttribute(value){ this.setAttribute("custom-attribute", value); }
}
2024-11-22 10:20:59 +01:00
customElements.define(HTMLCustomElement.tagName, HTMLCustomElement);
< / code > < / div > < p > For more advanced use of Custom Elements, the summary < a title = "Ideas and tips from WebReflection" href = "https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4" > < strong > Handy Custom Elements Patterns< / strong > < / a > may be useful. Especially pay attention to linking HTML attributes and defining setters/getters, this is very helpful to use in combination with the library (< code > el(HTMLCustomElement.tagName, { customAttribute: "new-value" });< / code > ).< / p > < p > Also see the Life Cycle Events sections, very similarly we would like to use < a href = "./p03-events.html" title = "See events part of the library documentation" > DDE events< / a > . To do it, the library provides function < code > customElementWithDDE< / code > …< / p > <!-- <dde:mark type="component" name="example" host="this" ssr/> --> < div id = "code-example-1-n7xtdicpa" class = "example" > <!-- <dde:mark type="component" name="code" host="parentElement" ssr/> --> < code class = "language-js" > import { customElementWithDDE, el, on } from "./esm-with-signals.js";
export class HTMLCustomElement extends HTMLElement{
static tagName= "custom-element";
connectedCallback(){
this.append(
el("p", "Hello from custom element!")
);
}
}
customElementWithDDE(HTMLCustomElement);
customElements.define(HTMLCustomElement.tagName, HTMLCustomElement);
const instance= el(HTMLCustomElement.tagName);
on.connected( // preffered
e=> console.log("Element connected to the DOM (v1):", e)
)(instance);
instance.addEventListener(
"dde:connected",
e=> console.log("Element connected to the DOM (v2):", e)
);
document.body.append(
instance,
);
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - n 7 x t d i c p a " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { c u s t o m E l e m e n t W i t h D D E , e l , o n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n e x p o r t c l a s s H T M L C u s t o m E l e m e n t e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - e l e m e n t \ \ \ " ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t t h i s . a p p e n d ( \ \ n \ \ t \ \ t \ \ t e l ( \ \ \ " p \ \ \ " , \ \ \ " H e l l o f r o m c u s t o m e l e m e n t ! \ \ \ " ) \ \ n \ \ t \ \ t ) ; \ \ n \ \ t } \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( H T M L C u s t o m E l e m e n t ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( H T M L C u s t o m E l e m e n t . t a g N a m e , H T M L C u s t o m E l e m e n t ) ; \ \ n \ \ n c o n s t i n s t a n c e = e l ( H T M L C u s t o m E l e m e n t . t a g N a m e ) ; \ \ n o n . c o n n e c t e d ( / / p r e f f e r e d \ \ n \ \ t e = > c o n s o l e . l o g ( \ \ \ " E l e m e n t c o n n e c t e d t o t h e D O M ( v 1 ) : \ \ \ " , e ) \ \ n ) ( i n s t a n c e ) ; \ \ n i n s t a n c e . a d d E v e n t L i s t e n e r ( \ \ n \ \ t \ \ \ " d d e : c o n n e c t e d \ \ \ " , \ \ n \ \ t e = > c o n s o l e . l o g ( \ \ \ " E l e m e n t c o n n e c t e d t o t h e D O M ( v 2 ) : \ \ \ " , e ) \ \ n ) ; \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t i n s t a n c e , \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ; \ \ n \ \ t } , \ \ n \ \ t g e t s t a t e ( ) { \ \ n \ \ t \ \ t r e t u r n [ . . . s c o p e s ] ; \ \ n \ \ t } , \ \ n \ \ t p u s h ( s = { } ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( O b j e c t . a s s i g n ( { } , t h i s . c u r r e n t , { p r e v e n t : f a l s e } , s ) ) ; \ \ n \ \ t } , \ \ n \ \ t p u s h R o o t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s . p u s h ( s c o p e s [ 0 ] ) ; \ \ n \ \ t } , \ \ n \ \ t p o p ( ) { \ \ n \ \ t \ \ t i f ( s c o p e s . l e n g t h = = = 1 ) r e t u r n ; \ \ n \ \
customElementRender,
customElementWithDDE,
} from "./esm-with-signals.js";
export class HTMLCustomElement extends HTMLElement{
static tagName= "custom-element";
static observedAttributes= [ "attr" ];
connectedCallback(){
customElementRender(
this,
this.attachShadow({ mode: "open" }),
ddeComponent
);
}
set attr(value){ this.setAttribute("attr", value); }
get attr(){ return this.getAttribute("attr"); }
}
import { el, on, scope } from "./esm-with-signals.js";
function ddeComponent({ attr }){
scope.host(
on.connected(e=> console.log(e.target.outerHTML)),
);
return el().append(
el("p", `Hello from Custom Element with attribute '${attr}'`)
);
}
customElementWithDDE(HTMLCustomElement);
customElements.define(HTMLCustomElement.tagName, HTMLCustomElement);
document.body.append(
el(HTMLCustomElement.tagName, { attr: "Attribute" })
);
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - v i v f g y x x 9 k w " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { \ \ n \ \ t c u s t o m E l e m e n t R e n d e r , \ \ n \ \ t c u s t o m E l e m e n t W i t h D D E , \ \ n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n e x p o r t c l a s s H T M L C u s t o m E l e m e n t e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - e l e m e n t \ \ \ " ; \ \ n \ \ t s t a t i c o b s e r v e d A t t r i b u t e s = [ \ \ \ " a t t r \ \ \ " ] ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t c u s t o m E l e m e n t R e n d e r ( \ \ n \ \ t \ \ t \ \ t t h i s , \ \ n \ \ t \ \ t \ \ t t h i s . a t t a c h S h a d o w ( { m o d e : \ \ \ " o p e n \ \ \ " } ) , \ \ n \ \ t \ \ t \ \ t d d e C o m p o n e n t \ \ n \ \ t \ \ t ) ; \ \ n \ \ t } \ \ n \ \ t s e t a t t r ( v a l u e ) { t h i s . s e t A t t r i b u t e ( \ \ \ " a t t r \ \ \ " , v a l u e ) ; } \ \ n \ \ t g e t a t t r ( ) { r e t u r n t h i s . g e t A t t r i b u t e ( \ \ \ " a t t r \ \ \ " ) ; } \ \ n } \ \ n \ \ n i m p o r t { e l , o n , s c o p e } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n f u n c t i o n d d e C o m p o n e n t ( { a t t r } ) { \ \ n \ \ t s c o p e . h o s t ( \ \ n \ \ t \ \ t o n . c o n n e c t e d ( e = > c o n s o l e . l o g ( e . t a r g e t . o u t e r H T M L ) ) , \ \ n \ \ t ) ; \ \ n \ \ t r e t u r n e l ( ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " p \ \ \ " , ` H e l l o f r o m C u s t o m E l e m e n t w i t h a t t r i b u t e ' $ { a t t r } ' ` ) \ \ n \ \ t ) ; \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( H T M L C u s t o m E l e m e n t ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( H T M L C u s t o m E l e m e n t . t a g N a m e , H T M L C u s t o m E l e m e n t ) ; \ \ n \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( H T M L C u s t o m E l e m e n t . t a g N a m e , { a t t r : \ \ \ " A t t r i b u t e \ \ \ " } ) \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n c u r r e n t ;
customElementRender,
customElementWithDDE,
observedAttributes,
el, on, scope
} from "./esm-with-signals.js";
import { S } from "./esm-with-signals.js";
export class HTMLCustomElement extends HTMLElement{
static tagName= "custom-element";
static observedAttributes= [ "attr" ];
connectedCallback(){
console.log(observedAttributes(this));
customElementRender(
this,
this.attachShadow({ mode: "open" }),
ddeComponent,
S.observedAttributes
);
}
set attr(value){ this.setAttribute("attr", value); }
get attr(){ return this.getAttribute("attr"); }
}
/** @param {{ attr: ddeSignal< string, {}> }} props */
function ddeComponent({ attr }){
scope.host(
on.connected(e=> console.log(e.target.outerHTML)),
);
return el().append(
el("p", S(()=> `Hello from Custom Element with attribute '${attr()}'`))
);
}
customElementWithDDE(HTMLCustomElement);
customElements.define(HTMLCustomElement.tagName, HTMLCustomElement);
document.body.append(
el(HTMLCustomElement.tagName, { attr: "Attribute" })
);
setTimeout(
()=> document.querySelector(HTMLCustomElement.tagName).setAttribute("attr", "New Value"),
3*750
);
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - u 3 d l 2 t j m o j 4 " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { \ \ n \ \ t c u s t o m E l e m e n t R e n d e r , \ \ n \ \ t c u s t o m E l e m e n t W i t h D D E , \ \ n \ \ t o b s e r v e d A t t r i b u t e s , \ \ n \ \ t e l , o n , s c o p e \ \ n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n i m p o r t { S } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n e x p o r t c l a s s H T M L C u s t o m E l e m e n t e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - e l e m e n t \ \ \ " ; \ \ n \ \ t s t a t i c o b s e r v e d A t t r i b u t e s = [ \ \ \ " a t t r \ \ \ " ] ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t c o n s o l e . l o g ( o b s e r v e d A t t r i b u t e s ( t h i s ) ) ; \ \ n \ \ t \ \ t c u s t o m E l e m e n t R e n d e r ( \ \ n \ \ t \ \ t \ \ t t h i s , \ \ n \ \ t \ \ t \ \ t t h i s . a t t a c h S h a d o w ( { m o d e : \ \ \ " o p e n \ \ \ " } ) , \ \ n \ \ t \ \ t \ \ t d d e C o m p o n e n t , \ \ n \ \ t \ \ t \ \ t S . o b s e r v e d A t t r i b u t e s \ \ n \ \ t \ \ t ) ; \ \ n \ \ t } \ \ n \ \ t s e t a t t r ( v a l u e ) { t h i s . s e t A t t r i b u t e ( \ \ \ " a t t r \ \ \ " , v a l u e ) ; } \ \ n \ \ t g e t a t t r ( ) { r e t u r n t h i s . g e t A t t r i b u t e ( \ \ \ " a t t r \ \ \ " ) ; } \ \ n } \ \ n \ \ n / * * @ p a r a m { { a t t r : d d e S i g n a l < s t r i n g , { } > } } p r o p s * / \ \ n f u n c t i o n d d e C o m p o n e n t ( { a t t r } ) { \ \ n \ \ t s c o p e . h o s t ( \ \ n \ \ t \ \ t o n . c o n n e c t e d ( e = > c o n s o l e . l o g ( e . t a r g e t . o u t e r H T M L ) ) , \ \ n \ \ t ) ; \ \ n \ \ t r e t u r n e l ( ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " p \ \ \ " , S ( ( ) = > ` H e l l o f r o m C u s t o m E l e m e n t w i t h a t t r i b u t e ' $ { a t t r ( ) } ' ` ) ) \ \ n \ \ t ) ; \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( H T M L C u s t o m E l e m e n t ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( H T M L C u s t o m E l e m e n t . t a g N a m e , H T M L C u s t o m E l e m e n t ) ; \ \ n \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( H T M L C u s t o m E l e m e n t . t a g N a m e , { a t t r : \ \ \ " A t t r i b u t e \ \ \ " } ) \ \ n ) ; \ \ n s e t T i m e o u t ( \ \ n \ \ t ( ) = > d o c u m e n t . q u e r y S e l e c t o r ( H T M L C u s t o m E l e m e n t . t a g N a m e ) . s e t A t t r i b u t e ( \ \ \ " a t t r \ \ \ " , \ \ \ " N e w V a l u e \ \ \ " ) , \ \ n \ \ t 3 * 7 5 0 \ \ n ) ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n
el,
customElementRender,
customElementWithDDE,
} from "./esm-with-signals.js";
function ddeComponent(){
return el().append(
el("style", `
.red{ color: firebrick; }
`),
el("p", { className: "red" }).append(
"Hello from ", el("slot", "Custom Element"), "!"
)
);
}
export class A extends HTMLElement{
static tagName= "custom-element-without";
connectedCallback(){
customElementRender(
this,
this,
ddeComponent
);
}
}
customElementWithDDE(A);
customElements.define(A.tagName, A);
export class B extends HTMLElement{
static tagName= "custom-element-open";
connectedCallback(){
customElementRender(
this,
this.attachShadow({ mode: "open" }),
ddeComponent
);
}
}
customElementWithDDE(B);
customElements.define(B.tagName, B);
export class C extends HTMLElement{
static tagName= "custom-element-closed";
connectedCallback(){
customElementRender(
this,
this.attachShadow({ mode: "closed" }),
ddeComponent
);
}
}
customElementWithDDE(C);
customElements.define(C.tagName, C);
document.body.append(
el(A.tagName).append("Without shadowRoot"),
el("hr"),
el(B.tagName).append("Open shadowRoot"),
el("hr"),
el(C.tagName).append("Closed shadowRoot"),
el("style", `
.red{ color: crimson; }
`),
);
console.log(A.tagName, "expect modifications");
document.body.querySelector(A.tagName).querySelector("p").textContent+= " (editable with JS)";
console.log(B.tagName, "expect modifications");
document.body.querySelector(B.tagName).shadowRoot.querySelector("p").textContent+= " (editable with JS)";
console.log(C.tagName, "expect error ↓");
document.body.querySelector(C.tagName).querySelector("p").textContent+= " (editable with JS)";
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - 1 u j 2 c 0 n m l 4 g w " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { \ \ n \ \ t e l , \ \ n \ \ t c u s t o m E l e m e n t R e n d e r , \ \ n \ \ t c u s t o m E l e m e n t W i t h D D E , \ \ n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n f u n c t i o n d d e C o m p o n e n t ( ) { \ \ n \ \ t r e t u r n e l ( ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " s t y l e \ \ \ " , ` \ \ n \ \ t \ \ t \ \ t . r e d { c o l o r : f i r e b r i c k ; } \ \ n \ \ t \ \ t ` ) , \ \ n \ \ t \ \ t e l ( \ \ \ " p \ \ \ " , { c l a s s N a m e : \ \ \ " r e d \ \ \ " } ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t \ \ \ " H e l l o f r o m \ \ \ " , e l ( \ \ \ " s l o t \ \ \ " , \ \ \ " C u s t o m E l e m e n t \ \ \ " ) , \ \ \ " ! \ \ \ " \ \ n \ \ t \ \ t ) \ \ n \ \ t ) ; \ \ n } \ \ n \ \ n e x p o r t c l a s s A e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - e l e m e n t - w i t h o u t \ \ \ " ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t c u s t o m E l e m e n t R e n d e r ( \ \ n \ \ t \ \ t \ \ t t h i s , \ \ n \ \ t \ \ t \ \ t t h i s , \ \ n \ \ t \ \ t \ \ t d d e C o m p o n e n t \ \ n \ \ t \ \ t ) ; \ \ n \ \ t } \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( A ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( A . t a g N a m e , A ) ; \ \ n e x p o r t c l a s s B e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - e l e m e n t - o p e n \ \ \ " ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t c u s t o m E l e m e n t R e n d e r ( \ \ n \ \ t \ \ t \ \ t t h i s , \ \ n \ \ t \ \ t \ \ t t h i s . a t t a c h S h a d o w ( { m o d e : \ \ \ " o p e n \ \ \ " } ) , \ \ n \ \ t \ \ t \ \ t d d e C o m p o n e n t \ \ n \ \ t \ \ t ) ; \ \ n \ \ t } \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( B ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( B . t a g N a m e , B ) ; \ \ n e x p o r t c l a s s C e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - e l e m e n t - c l o s e d \ \ \ " ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t c u s t o m E l e m e n t R e n d e r ( \ \ n \ \ t \ \ t \ \ t t h i s , \ \ n \ \ t \ \ t \ \ t t h i s . a t t a c h S h a d o w ( { m o d e : \ \ \ " c l o s e d \ \ \ " } ) , \ \ n \ \ t \ \ t \ \ t d d e C o m p o n e n t \ \ n \ \ t \ \ t ) ; \ \ n \ \ t } \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( C ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( C . t a g N a m e , C ) ; \ \ n \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( A . t a g N a m e ) . a p p e n d ( \ \ \ " W i t h o u t s h a d o w R o o t \ \ \ " ) , \ \ n \ \ t e l ( \ \ \ " h r \ \ \ " ) , \ \ n \ \ t e l ( B . t a g N a m e ) . a p p e n d ( \ \ \ " O p e n s h a d o w R o o t \ \ \ " ) , \ \ n \ \ t e l ( \ \ \ " h r \ \ \ " ) , \ \ n \ \ t e l ( C . t a g N a m e ) . a p p e n d ( \ \ \ " C l o s e d s h a d o w R o o t \ \ \ " ) , \ \ n \ \ t e l ( \ \ \ " s t y l e \ \ \ " , ` \ \ n \ \ t \ \ t . r e d { c o l o r : c r i m s o n ; } \ \ n \ \ t ` ) , \ \ n ) ; \ \ n c o n s o l e . l o g ( A . t a g N a m e , \ \ \ " e x p e c t m o d i f i c a t i o n s \ \ \ " ) ; \ \ n d o c u m e n t . b o d y . q u e r y S e l e c t o r ( A . t a g N a m e ) . q u e r y S e l e c t o r ( \ \ \ " p \ \ \ " ) . t e x t C o n t e n t + = \ \ \ " ( e d i t a b l e w i t h J S ) \ \ \ " ; \ \ n c o n s o l e . l o g ( B . t a g N a m e , \ \ \ " e x p e c t m o d i f i c a t i o n s \ \ \ " ) ; \ \ n d o c u m e n t . b o d y . q u e r y S e l e c t o r ( B . t a g N a m e ) . s h a d o w R o o t . q u e r y S e l e c t o r ( \ \ \ " p \ \ \ " ) . t e x t C o n t e n t + = \ \ \ " ( e d i t a b l e w i t h J S ) \ \ \ " ; \ \ n c o n s o l e . l o g ( C . t a g N a m e , \ \ \ " e x p e c t e r r o r ↓ \ \ \ " ) ; \ \ n d o c u m e n t . b o d y . q u e r y S e l e c t o r ( C . t a g N a m e ) . q u e r y S e l e c t o r ( \ \ \ " p \ \ \ " ) . t e x t C o n t e n t + = \ \ \ " ( e d i t a b l e w i t h J S ) \ \ \ " ; \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m
customElementRender,
customElementWithDDE,
el,
simulateSlots
} from "./esm-with-signals.js";
export class HTMLCustomElement extends HTMLElement{
static tagName= "custom-slotting";
connectedCallback(){
const c= ()=> simulateSlots(this, ddeComponent());
customElementRender(this, this, c);
}
}
customElementWithDDE(HTMLCustomElement);
customElements.define(HTMLCustomElement.tagName, HTMLCustomElement);
document.body.append(
el(HTMLCustomElement.tagName),
el(HTMLCustomElement.tagName).append(
"Slot"
),
el(ddeComponentSlot),
el(ddeComponentSlot).append(
"Slot"
),
);
function ddeComponent(){
return el().append(
el("p").append(
"Hello ", el("slot", "World")
)
);
}
function ddeComponentSlot(){
return simulateSlots(el().append(
el("p").append(
"Hello ", el("slot", "World")
)
));
}
< / code > < / div > < script > F l e m s ( d o c u m e n t . g e t E l e m e n t B y I d ( " c o d e - e x a m p l e - 1 - u k 9 z r d g 1 y p s " ) , J S O N . p a r s e ( " { \ " f i l e s \ " : [ { \ " n a m e \ " : \ " . j s \ " , \ " c o n t e n t \ " : \ " i m p o r t { \ \ n \ \ t c u s t o m E l e m e n t R e n d e r , \ \ n \ \ t c u s t o m E l e m e n t W i t h D D E , \ \ n \ \ t e l , \ \ n \ \ t s i m u l a t e S l o t s \ \ n } f r o m \ \ \ " . / e s m - w i t h - s i g n a l s . j s \ \ \ " ; \ \ n e x p o r t c l a s s H T M L C u s t o m E l e m e n t e x t e n d s H T M L E l e m e n t { \ \ n \ \ t s t a t i c t a g N a m e = \ \ \ " c u s t o m - s l o t t i n g \ \ \ " ; \ \ n \ \ t c o n n e c t e d C a l l b a c k ( ) { \ \ n \ \ t \ \ t c o n s t c = ( ) = > s i m u l a t e S l o t s ( t h i s , d d e C o m p o n e n t ( ) ) ; \ \ n \ \ t \ \ t c u s t o m E l e m e n t R e n d e r ( t h i s , t h i s , c ) ; \ \ n \ \ t } \ \ n } \ \ n c u s t o m E l e m e n t W i t h D D E ( H T M L C u s t o m E l e m e n t ) ; \ \ n c u s t o m E l e m e n t s . d e f i n e ( H T M L C u s t o m E l e m e n t . t a g N a m e , H T M L C u s t o m E l e m e n t ) ; \ \ n \ \ n d o c u m e n t . b o d y . a p p e n d ( \ \ n \ \ t e l ( H T M L C u s t o m E l e m e n t . t a g N a m e ) , \ \ n \ \ t e l ( H T M L C u s t o m E l e m e n t . t a g N a m e ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ \ " S l o t \ \ \ " \ \ n \ \ t ) , \ \ n \ \ t e l ( d d e C o m p o n e n t S l o t ) , \ \ n \ \ t e l ( d d e C o m p o n e n t S l o t ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ \ " S l o t \ \ \ " \ \ n \ \ t ) , \ \ n ) ; \ \ n \ \ n f u n c t i o n d d e C o m p o n e n t ( ) { \ \ n \ \ t r e t u r n e l ( ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " p \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t \ \ \ " H e l l o \ \ \ " , e l ( \ \ \ " s l o t \ \ \ " , \ \ \ " W o r l d \ \ \ " ) \ \ n \ \ t \ \ t ) \ \ n \ \ t ) ; \ \ n } \ \ n f u n c t i o n d d e C o m p o n e n t S l o t ( ) { \ \ n \ \ t r e t u r n s i m u l a t e S l o t s ( e l ( ) . a p p e n d ( \ \ n \ \ t \ \ t e l ( \ \ \ " p \ \ \ " ) . a p p e n d ( \ \ n \ \ t \ \ t \ \ t \ \ \ " H e l l o \ \ \ " , e l ( \ \ \ " s l o t \ \ \ " , \ \ \ " W o r l d \ \ \ " ) \ \ n \ \ t \ \ t ) \ \ n \ \ t ) ) ; \ \ n } \ \ n \ " } , { \ " n a m e \ " : \ " e s m - w i t h - s i g n a l s . j s \ " , \ " c o n t e n t \ " : \ " / / s r c / s i g n a l s - c o m m o n . j s \ \ n v a r s i g n a l s _ g l o b a l = { \ \ n \ \ t i s S i g n a l ( a t t r i b u t e s ) { \ \ n \ \ t \ \ t r e t u r n f a l s e ; \ \ n \ \ t } , \ \ n \ \ t p r o c e s s R e a c t i v e A t t r i b u t e ( o b j , k e y , a t t r , s e t ) { \ \ n \ \ t \ \ t r e t u r n a t t r ; \ \ n \ \ t } \ \ n } ; \ \ n f u n c t i o n r e g i s t e r R e a c t i v i t y ( d e f , g l o b a l = t r u e ) { \ \ n \ \ t i f ( g l o b a l ) r e t u r n O b j e c t . a s s i g n ( s i g n a l s _ g l o b a l , d e f ) ; \ \ n \ \ t O b j e c t . s e t P r o t o t y p e O f ( d e f , s i g n a l s _ g l o b a l ) ; \ \ n \ \ t r e t u r n d e f ; \ \ n } \ \ n f u n c t i o n s i g n a l s ( _ t h i s ) { \ \ n \ \ t r e t u r n s i g n a l s _ g l o b a l . i s P r o t o t y p e O f ( _ t h i s ) & & _ t h i s ! = = s i g n a l s _ g l o b a l ? _ t h i s : s i g n a l s _ g l o b a l ; \ \ n } \ \ n \ \ n / / s r c / h e l p e r s . j s \ \ n v a r h a s O w n = ( . . . a ) = > O b j e c t . p r o t o t y p e . h a s O w n P r o p e r t y . c a l l ( . . . a ) ; \ \ n f u n c t i o n i s U n d e f ( v a l u e ) { \ \ n \ \ t r e t u r n t y p e o f v a l u e = = = \ \ \ " u n d e f i n e d \ \ \ " ; \ \ n } \ \ n f u n c t i o n t y p e O f ( v ) { \ \ n \ \ t c o n s t t = t y p e o f v ; \ \ n \ \ t i f ( t ! = = \ \ \ " o b j e c t \ \ \ " ) r e t u r n t ; \ \ n \ \ t i f ( v = = = n u l l ) r e t u r n \ \ \ " n u l l \ \ \ " ; \ \ n \ \ t r e t u r n O b j e c t . p r o t o t y p e . t o S t r i n g . c a l l ( v ) ; \ \ n } \ \ n f u n c t i o n o n A b o r t ( s i g n a l 2 , l i s t e n e r ) { \ \ n \ \ t i f ( ! s i g n a l 2 | | ! ( s i g n a l 2 i n s t a n c e o f A b o r t S i g n a l ) ) \ \ n \ \ t \ \ t r e t u r n t r u e ; \ \ n \ \ t i f ( s i g n a l 2 . a b o r t e d ) \ \ n \ \ t \ \ t r e t u r n ; \ \ n \ \ t s i g n a l 2 . a d d E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t r e t u r n f u n c t i o n c l e a n U p ( ) { \ \ n \ \ t \ \ t s i g n a l 2 . r e m o v e E v e n t L i s t e n e r ( \ \ \ " a b o r t \ \ \ " , l i s t e n e r ) ; \ \ n \ \ t } ; \ \ n } \ \ n f u n c t i o n o b s e r v e d A t t r i b u t e s ( i n s t a n c e , o b s e r v e d A t t r i b u t e 2 ) { \ \ n \ \ t c o n s t { o b s e r v e d A t t r i b u t e s : o b s e r v e d A t t r i b u t e s 3 = [ ] } = i n s t a n c e . c o n s t r u c t o r ; \ \ n \ \ t r e t u r n o b s e r v e d A t t r i b u t e s 3 . r e d u c e ( f u n c t i o n ( o u t , n a m e ) { \ \ n \ \ t \ \ t o u t [ k e b a b T o C a m e l ( n a m e ) ] = o b s e r v e d A t t r i b u t e 2 ( i n s t a n c e , n a m e ) ; \ \ n \ \ t \ \ t r e t u r n o u t ; \ \ n \ \ t } , { } ) ; \ \ n } \ \ n f u n c t i o n k e b a b T o C a m e l ( n a m e ) { \ \ n \ \ t r e t u r n n a m e . r e p l a c e ( / - . / g , ( x ) = > x [ 1 ] . t o U p p e r C a s e ( ) ) ; \ \ n } \ \ n \ \ n / / s r c / d o m - c o m m o n . j s \ \ n v a r e n v i r o m e n t = { \ \ n \ \ t s e t D e l e t e A t t r , \ \ n \ \ t s s r : \ \ \ " \ \ \ " , \ \ n \ \ t D : g l o b a l T h i s . d o c u m e n t , \ \ n \ \ t F : g l o b a l T h i s . D o c u m e n t F r a g m e n t , \ \ n \ \ t H : g l o b a l T h i s . H T M L E l e m e n t , \ \ n \ \ t S : g l o b a l T h i s . S V G E l e m e n t , \ \ n \ \ t M : g l o b a l T h i s . M u t a t i o n O b s e r v e r \ \ n } ; \ \ n f u n c t i o n s e t D e l e t e A t t r ( o b j , p r o p , v a l ) { \ \ n \ \ t R e f l e c t . s e t ( o b j , p r o p , v a l ) ; \ \ n \ \ t i f ( ! i s U n d e f ( v a l ) ) r e t u r n ; \ \ n \ \ t R e f l e c t . d e l e t e P r o p e r t y ( o b j , p r o p ) ; \ \ n \ \ t i f ( o b j i n s t a n c e o f e n v i r o m e n t . H & & o b j . g e t A t t r i b u t e ( p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n o b j . r e m o v e A t t r i b u t e ( p r o p ) ; \ \ n \ \ t i f ( R e f l e c t . g e t ( o b j , p r o p ) = = = \ \ \ " u n d e f i n e d \ \ \ " ) \ \ n \ \ t \ \ t r e t u r n R e f l e c t . s e t ( o b j , p r o p , \ \ \ " \ \ \ " ) ; \ \ n } \ \ n v a r k e y L T E = \ \ \ " _ _ d d e _ l i f e c y c l e s T o E v e n t s \ \ \ " ; \ \ n v a r e v c = \ \ \ " d d e : c o n n e c t e d \ \ \ " ; \ \ n v a r e v d = \ \ \ " d d e : d i s c o n n e c t e d \ \ \ " ; \ \ n v a r e v a = \ \ \ " d d e : a t t r i b u t e C h a n g e d \ \ \ " ; \ \ n \ \ n / / s r c / d o m . j s \ \ n v a r s c o p e s = [ { \ \ n \ \ t g e t s c o p e ( ) { \ \ n \ \ t \ \ t r e t u r n e n v i r o m e n t . D . b o d y ; \ \ n \ \ t } , \ \ n \ \ t h o s t : ( c ) = > c ? c ( e n v i r o m e n t . D . b o d y ) : e n v i r o m e n t . D . b o d y , \ \ n \ \ t p r e v e n t : t r u e \ \ n } ] ; \ \ n v a r s c o p e = { \ \ n \ \ t g e t c u r r e n t ( ) { \ \ n \ \ t \ \ t r e t u r n s c o p e s [ s c o p e s . l e n g t h - 1 ] ; \ \ n \ \ t } , \ \ n \ \ t g e t h o s t ( ) { \ \ n \ \ t \ \ t r e t u r n t h i s . c u r r e n t . h o s t ; \ \ n \ \ t } , \ \ n \ \ t p r e v e n t D e f a u l t ( ) { \ \ n \ \ t \ \ t c o n s t { c u r r e n t } = t h i s ; \ \ n \ \ t \ \ t c u r r e n t . p r e v e n t = t r u e ; \ \ n \ \ t \ \ t r e t u r n