Skip to content

Commit

Permalink
Issue #10: Fixing bug present when components have context and we're …
Browse files Browse the repository at this point in the history
…instantiating them outside of their context (before it's parent has initialized it or not inside of parent).
  • Loading branch information
patricknelson committed Sep 28, 2023
1 parent 3aad00f commit 10c7c03
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ export default function(opts) {
});
}

// Inspect the component early on to get its available properties (statically available).
const propInstance = new opts.component({ target: document.createElement('div') });
const propMap = new Map();
for(let key of Object.keys(propInstance.$$.props)) {
propMap.set(key.toLowerCase(), key);
}
propInstance.$destroy();


/**
* Defines the actual custom element responsible for rendering the provided Svelte component.
Expand All @@ -70,6 +62,17 @@ export default function(opts) {

this._debug('constructor()');

// Temporarily instantiate the component ahead of time just so we can get its available properties (statically
// available). Note that we're doing it here in the constructor in case this component has context (so it may
// normally only be instantiated from within another component).
const propInstance = new opts.component({ target: document.createElement('div') });
this.propMap = new Map();
for(let key of Object.keys(propInstance.$$.props)) {
this.propMap.set(key.toLowerCase(), key);
}
propInstance.$destroy();


// Setup shadow root early (light-DOM root is initialized in connectedCallback() below).
if (opts.shadow) {
this.attachShadow({ mode: 'open' });
Expand Down Expand Up @@ -265,8 +268,8 @@ export default function(opts) {
_translateAttribute(attributeName) {
// In the unlikely scenario that a browser somewhere doesn't do this for us (or maybe we're in a quirks mode or something...)
attributeName = attributeName.toLowerCase();
if (propMap.has(attributeName)) {
return propMap.get(attributeName);
if (this.propMap.has(attributeName)) {
return this.propMap.get(attributeName);
} else {
this._debug(`_translateAttribute(): ${attributeName} not found`);
return attributeName;
Expand Down

0 comments on commit 10c7c03

Please sign in to comment.