Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: port over props that were set prior to initialization #9704

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-tips-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: port over props that were set prior to initialization
10 changes: 10 additions & 0 deletions packages/svelte/src/internal/client/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ if (typeof HTMLElement === 'function') {
this.$$d[name] = get_custom_element_value(name, attribute.value, this.$$p_d, 'toProp');
}
}
// Port over props that were set programmatically before ce was initialized
for (const key in this.$$p_d) {
// @ts-expect-error
if (!(key in this.$$d) && this[key] !== undefined) {
// @ts-expect-error
this.$$d[key] = this[key]; // don't transform, these were set through JavaScript
// @ts-expect-error
delete this[key]; // remove the property that shadows the getter/setter
}
}
this.$$c = createClassComponent({
component: this.$$ctor,
target: this.shadowRoot || this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test } from '../../assert';

const tick = () => Promise.resolve();

export default test({
async test({ assert, target, componentCtor }) {
target.innerHTML = '<custom-element red white></custom-element>';
const ce = target.querySelector('custom-element');
ce.prop = 1;
customElements.define('custom-element', componentCtor.element);
await tick();
await tick();

const ce_root = target.querySelector('custom-element').shadowRoot;
const p = ce_root.querySelector('p');

assert.equal(p.textContent, '1');

ce.prop = 2;
await tick();
await tick();

assert.equal(p.textContent, '2');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let prop;
</script>

<p>{prop}</p>