mirror of
https://github.com/jaandrle/deka-dom-el
synced 2025-07-01 04:12:14 +02:00
⚡ Adds Web Componens page into the doc and doc enhancements (#21)
This commit is contained in:
@ -14,15 +14,14 @@ ${host}{
|
||||
--shiki-token-punctuation: var(--code);
|
||||
--shiki-token-link: #EE0000;
|
||||
white-space: pre;
|
||||
tab-size: 2;${""/* TODO: allow custom tab size?! */}
|
||||
tab-size: 2; /* TODO: allow custom tab size?! */
|
||||
overflow: scroll;
|
||||
}
|
||||
${host}[data-js=todo]{
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--standard-border-radius);
|
||||
margin-bottom: 1rem;
|
||||
${/* to fix shift when → dataJS=done */""}
|
||||
margin-top: 18.4px;
|
||||
margin-top: 18.4px; /* to fix shift when → dataJS=done */
|
||||
padding: 1rem 1.4rem;
|
||||
}
|
||||
`;
|
||||
|
@ -0,0 +1,23 @@
|
||||
import { customElementWithDDE, el, on } from "deka-dom-el";
|
||||
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,
|
||||
);
|
33
docs_src/components/examples/customElement/dde.js
Normal file
33
docs_src/components/examples/customElement/dde.js
Normal file
@ -0,0 +1,33 @@
|
||||
import {
|
||||
customElementRender,
|
||||
customElementWithDDE,
|
||||
} from "deka-dom-el";
|
||||
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 "deka-dom-el";
|
||||
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" })
|
||||
);
|
@ -1,5 +1,5 @@
|
||||
class CustomHTMLElement extends HTMLElement{
|
||||
static tagName = "custom-element"; // just suggestion, we can use `el(CustomHTMLElement.tagName)`
|
||||
export class HTMLCustomElement extends HTMLElement{
|
||||
static tagName= "custom-element"; // just suggestion, we can use `el(HTMLCustomElement.tagName)`
|
||||
static observedAttributes= [ "custom-attribute" ];
|
||||
constructor(){
|
||||
super();
|
||||
@ -18,4 +18,4 @@ class CustomHTMLElement extends HTMLElement{
|
||||
get customAttribute(){ return this.getAttribute("custom-attribute"); }
|
||||
set customAttribute(value){ this.setAttribute("custom-attribute", value); }
|
||||
}
|
||||
customElements.define(CustomHTMLElement.tagName, CustomHTMLElement);
|
||||
customElements.define(HTMLCustomElement.tagName, HTMLCustomElement);
|
||||
|
@ -0,0 +1,42 @@
|
||||
import {
|
||||
customElementRender,
|
||||
customElementWithDDE,
|
||||
observedAttributes,
|
||||
el, on, scope
|
||||
} from "deka-dom-el";
|
||||
import { S } from "deka-dom-el/signals";
|
||||
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
|
||||
);
|
69
docs_src/components/examples/customElement/shadowRoot.js
Normal file
69
docs_src/components/examples/customElement/shadowRoot.js
Normal file
@ -0,0 +1,69 @@
|
||||
import {
|
||||
el,
|
||||
customElementRender,
|
||||
customElementWithDDE,
|
||||
} from "deka-dom-el";
|
||||
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)";
|
41
docs_src/components/examples/customElement/simulateSlots.js
Normal file
41
docs_src/components/examples/customElement/simulateSlots.js
Normal file
@ -0,0 +1,41 @@
|
||||
import {
|
||||
customElementRender,
|
||||
customElementWithDDE,
|
||||
el,
|
||||
simulateSlots
|
||||
} from "deka-dom-el";
|
||||
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")
|
||||
)
|
||||
));
|
||||
}
|
6
docs_src/components/examples/introducing/3ps.js
Normal file
6
docs_src/components/examples/introducing/3ps.js
Normal file
@ -0,0 +1,6 @@
|
||||
// pseudo code!
|
||||
const onchage=
|
||||
event=>
|
||||
console.log("Reacting to the:", event); // A
|
||||
input.addEventListener("change", onchange); // B
|
||||
input.dispatchEvent(new Event("change")); // C
|
@ -1,15 +1,15 @@
|
||||
import { el } from "deka-dom-el";
|
||||
import { S } from "deka-dom-el/signals";
|
||||
const clicks= S(0);
|
||||
const clicks= S(0); // A
|
||||
document.body.append(
|
||||
el().append(
|
||||
el("p", S(()=>
|
||||
"Hello World "+"🎉".repeat(clicks())
|
||||
"Hello World "+"🎉".repeat(clicks()) // B
|
||||
)),
|
||||
el("button", {
|
||||
type: "button",
|
||||
onclick: ()=> clicks(clicks()+1),
|
||||
textContent: "Fire"
|
||||
onclick: ()=> clicks(clicks()+1), // C
|
||||
textContent: "Fire",
|
||||
})
|
||||
)
|
||||
);
|
@ -4,7 +4,7 @@ import { mnemonicUl } from "../mnemonicUl.html.js";
|
||||
export function mnemonic(){
|
||||
return mnemonicUl().append(
|
||||
el("li").append(
|
||||
el("code", "customElementRender(<custom-element>, <render-function>[, <properties>])"), " — use function to render DOM structure for given <custom-element>",
|
||||
el("code", "customElementRender(<custom-element>, <connect-target>, <render-function>[, <properties>])"), " — use function to render DOM structure for given <custom-element>",
|
||||
),
|
||||
el("li").append(
|
||||
el("code", "customElementWithDDE(<custom-element>)"), " — register <custom-element> to DDE library, see also `lifecyclesToEvents`, can be also used as decorator",
|
||||
@ -17,6 +17,12 @@ export function mnemonic(){
|
||||
),
|
||||
el("li").append(
|
||||
el("code", "lifecyclesToEvents(<class-declaration>)"), " — convert lifecycle methods to events, can be also used as decorator",
|
||||
)
|
||||
),
|
||||
el("li").append(
|
||||
el("code", "simulateSlots(<class-instance>, <body>[, <mapper>])"), " — simulate slots for Custom Elements without shadow DOM",
|
||||
),
|
||||
el("li").append(
|
||||
el("code", "simulateSlots(<dde-component>[, <mapper>])"), " — simulate slots for “dde”/functional components",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user