1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-27 02:09:37 +01:00
deka-dom-el/examples/components/webComponent.js

67 lines
2.0 KiB
JavaScript
Raw Normal View History

import { el, on, customElementRender, customElementWithDDE, scope, simulateSlots } from "../../index.js";
2023-11-24 20:41:04 +01:00
import { O } from "../../observables.js";
/**
2023-11-22 17:39:46 +01:00
* Compatible with `npx -y web-component-analyzer examples/components/webComponent.js`
* @element custom-test
* */
export class CustomHTMLTestElement extends HTMLElement{
static tagName= "custom-test";
static get observedAttributes(){
2023-09-12 15:26:06 +02:00
return [ "name", "pre-name" ];
}
connectedCallback(){
2023-11-22 17:39:46 +01:00
if(!this.hasAttribute("pre-name")) this.setAttribute("pre-name", "default");
2024-01-04 16:05:41 +01:00
console.log(observedAttributes(this));
2023-09-12 15:26:06 +02:00
this.attachShadow({ mode: "open" }).append(
2023-11-22 21:29:40 +01:00
customElementRender(this, this.render)
2023-09-12 15:26:06 +02:00
);
}
2024-01-04 16:05:41 +01:00
render(test){
console.log(scope.state);
2023-11-22 21:29:40 +01:00
scope.host(
on.connected(()=> console.log(CustomHTMLTestElement)),
on.attributeChanged(e=> console.log(e)),
on.disconnected(()=> console.log(CustomHTMLTestElement))
);
2023-11-24 20:41:04 +01:00
const name= O.attribute("name");
const preName= O.attribute("pre-name");
console.log({ name, test, preName});
2023-09-12 15:26:06 +02:00
return el("p").append(
el("#text", name),
el("#text", preName),
el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") })
2023-09-12 15:26:06 +02:00
);
}
2023-11-22 17:39:46 +01:00
test= "A";
get name(){ return this.getAttribute("name"); }
set name(value){ this.setAttribute("name", value); }
2023-11-22 17:39:46 +01:00
/** @attr pre-name */
get preName(){ return this.getAttribute("pre-name"); }
set preName(value){ this.setAttribute("pre-name", value); }
}
customElementWithDDE(CustomHTMLTestElement);
customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement);
export class CustomSlottingHTMLElement extends HTMLElement{
static tagName= "custom-slotting";
render(){
return simulateSlots(this, el().append(
el("p").append(
"Ahoj ", el("slot", { name: "name", className: "name", textContent: "World" })
),
el("p").append(
"BTW ", el("slot")
)
));
}
connectedCallback(){
this.append(customElementRender(this, this.render));
}
}
customElementWithDDE(CustomSlottingHTMLElement);
customElements.define(CustomSlottingHTMLElement.tagName, CustomSlottingHTMLElement);