Using custom elements in combinantion with DDE
// use NPM or for example https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js
+`deka-dom-el` — Web Components `deka-dom-el` — Web Components
Using custom elements in combinantion with DDE
Using web components in combinantion with DDE
The DDE library allows for use within Web Components for dom-tree generation. However, in order to be able to use signals (possibly mapping to registered observedAttributes
) and additional functionality is (unfortunately) required to use helpers provided by the library.
// use NPM or for example https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js
import {
customElementRender,
customElementWithDDE,
@@ -10,7 +10,7 @@ S.observedAttributes;
// “internal” utils
import { lifecyclesToEvents } from "deka-dom-el";
-
# Custom Elements Introduction
class CustomHTMLElement extends HTMLElement{
+
# Custom Elements Introduction
To start with, let’s see how to use native Custom Elements. As starting point please read Using Custom Elements on MDN. To sum up and for mnemonic see following code overview:
export class CustomHTMLElement extends HTMLElement{
static tagName= "custom-element"; // just suggestion, we can use `el(CustomHTMLElement.tagName)`
static observedAttributes= [ "custom-attribute" ];
constructor(){
@@ -31,4 +31,4 @@ import { lifecyclesToEvents } from "deka-dom-el";
set customAttribute(value){ this.setAttribute("custom-attribute", value); }
}
customElements.define(CustomHTMLElement.tagName, CustomHTMLElement);
-
Handy Custom Elements' Patterns
# Mnemonic
customElementRender(<custom-element>, <render-function>[, <properties>])
— use function to render DOM structure for given <custom-element>customElementWithDDE(<custom-element>)
— register <custom-element> to DDE library, see also `lifecyclesToEvents`, can be also used as decoratorobservedAttributes(<custom-element>)
— returns record of observed attributes (keys uses camelCase)S.observedAttributes(<custom-element>)
— returns record of observed attributes (keys uses camelCase and values are signals)lifecyclesToEvents(<class-declaration>)
— convert lifecycle methods to events, can be also used as decorator
\ No newline at end of file
+
For more advanced use of Custom Elements, the summary Handy Custom Elements' Patterns 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 (el(CustomHTMLElement.tagName, { customAttribute: "new-value" });
).
# Mnemonic
customElementRender(<custom-element>, <render-function>[, <properties>])
— use function to render DOM structure for given <custom-element>customElementWithDDE(<custom-element>)
— register <custom-element> to DDE library, see also `lifecyclesToEvents`, can be also used as decoratorobservedAttributes(<custom-element>)
— returns record of observed attributes (keys uses camelCase)S.observedAttributes(<custom-element>)
— returns record of observed attributes (keys uses camelCase and values are signals)lifecyclesToEvents(<class-declaration>)
— convert lifecycle methods to events, can be also used as decorator