Skip to content

Commit

Permalink
refactor: improve custom element definition and handling of 'for' att…
Browse files Browse the repository at this point in the history
…ribute
  • Loading branch information
mathertel committed Feb 15, 2025
1 parent bd151f7 commit ef3b84a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sfc/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UComponent extends HTMLElement {
}
});

if (document.readyState == 'loading') {
if (document.readyState === 'loading') {
window.addEventListener('DOMContentLoaded', this.init.bind(this));
} else {
window.requestAnimationFrame(this.init.bind(this));
Expand Down Expand Up @@ -134,8 +134,19 @@ window.loadComponent = (function() {
// make template and style available to object constructor()
def.uTemplate = dom.querySelector('template');
def.uStyle = dom.querySelector('style');
customElements.define(tagName, def);
console.debug('SFC', `${tagName} defined.`);
def.for = scriptObj.getAttribute('for');

if (def.for) {
customElements.define(tagName, def, { extends: def.for });
if (def.uStyle) {
document.head.appendChild(def.uStyle.cloneNode(true));
}
console.debug('SFC', `${def.for}.${tagName} defined.`);

} else {
customElements.define(tagName, def);
console.debug('SFC', `${tagName} defined.`);
}
}; // fetchSFC()


Expand All @@ -146,3 +157,5 @@ window.loadComponent = (function() {

return loadComponent;
}());

// EOF

0 comments on commit ef3b84a

Please sign in to comment.