1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 07:49:38 +01:00
deka-dom-el/docs_src/components/examples/scopes/class-component.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-12-04 18:19:30 +01:00
import { el } from "deka-dom-el";
2023-11-30 17:05:19 +01:00
class Test {
constructor(params){
this._params= params;
}
render(){
return el("div").append(
this._params.textContent
);
}
}
document.body.append(
elClass(Test, { textContent: "Hello World" })
);
2023-12-04 18:19:30 +01:00
import { chainableAppend, scope } from "deka-dom-el";
function elClass(_class, attributes, ...addons){
2023-11-30 17:05:19 +01:00
let element, element_host;
scope.push({
2023-12-04 18:19:30 +01:00
scope: _class, //just informative purposes
host: (...addons_append)=> addons_append.length
? (
!element
? addons.unshift(...addons_append)
: addons_append.forEach(c=> c(element_host))
, undefined)
: element_host
2023-11-30 17:05:19 +01:00
});
2023-12-04 18:19:30 +01:00
const instance= new _class(attributes);
element= instance.render();
const is_fragment= element instanceof DocumentFragment;
2023-11-30 17:05:19 +01:00
const el_mark= el.mark({ //this creates html comment `<dde:mark …/>`
type: "class-component",
2023-12-04 18:19:30 +01:00
name: _class.name,
2023-11-30 17:05:19 +01:00
host: is_fragment ? "this" : "parentElement",
});
element.prepend(el_mark);
if(is_fragment) element_host= el_mark;
chainableAppend(element);
addons.forEach(c=> c(element_host));
scope.pop();
return element;
}