ziko
Version:
A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...
47 lines (42 loc) • 1.47 kB
JavaScript
export function define_wc(name, UIElement, props = {}, { mode = 'open'} = {}) {
if (globalThis.customElements?.get(name)) {
console.warn(`Custom element "${name}" is already defined`);
return;
}
if(name.search('-') === -1){
console.warn(`"${name}" is not a valid custom element name`);
return;
}
globalThis.customElements?.define(
name,
class extends HTMLElement {
static get observedAttributes() {
return ['style', ...Object.keys(props)];
}
constructor() {
super();
this.attachShadow({ mode });
this.props = {};
this.mask = {
...props,
// style: { type: Object }
};
}
connectedCallback() {
this.render();
}
render() {
this.shadowRoot.innerHTML = '';
const item = UIElement(this.props);
if(item instanceof Array) item.forEach(n => n.mount(this.shadowRoot))
else item.mount(this.shadowRoot)
}
attributeChangedCallback(name, _, newValue) {
Object.assign(this.props, {
[name]: this.mask[name].type(newValue)
});
this.render();
}
}
);
}