@twobirds/microcomponents
Version:
Micro Components Organization Class
48 lines • 1.52 kB
JavaScript
;
import { DC } from './MC.js';
import { kebabToPascal } from './helpers.js';
class CeBaseClass extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
DC.trigger(this, 'Connected');
}
disconnectedCallback() {
DC.trigger(this, 'Disconnected');
}
adoptedCallback() {
DC.trigger(this, 'Adopted');
}
attributeChangedCallback(name, oldValue, newValue) {
DC.trigger(this, 'AttributeChanged', { name, oldValue, newValue });
}
}
function createNamedClass(name, ClassBody) {
return Object.defineProperty(ClassBody, 'name', { value: name });
}
export function defineCE(tagName, DefinitionClass, observedAttributes = []) {
if (customElements.get(tagName)) {
console.warn(`IGNORED: Custom element "${tagName}" is already defined!`);
return;
}
if (!tagName.includes('-')) {
throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');
}
let name = kebabToPascal(tagName);
customElements.define(tagName, createNamedClass(name, class extends CeBaseClass {
instance;
constructor(data) {
super();
let instance = new DefinitionClass(this, data);
DC.add(this, name, instance);
}
static get observedAttributes() {
return observedAttributes;
}
}));
}
export function getCE(tagName) {
return customElements.get(tagName);
}
//# sourceMappingURL=elements.js.map