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 #9701

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/wicked-chefs-bow.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
7 changes: 7 additions & 0 deletions packages/svelte/src/runtime/internal/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ 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) {
if (!(key in this.$$d) && this[key] !== undefined) {
this.$$d[key] = this[key]; // don't transform, these were set through JavaScript
delete this[key]; // remove the property that shadows the getter/setter
}
}
this.$$c = new this.$$ctor({
target: this.shadowRoot || this,
props: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<svelte:options
customElement={null}
/>

<script>
export let prop;
</script>

<p>{prop}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as assert from 'assert.js';
import { tick } from 'svelte';
import Main from './main.svelte';

export default async function (target) {
target.innerHTML = '<custom-element red white></custom-element>';
const ce = target.querySelector('custom-element');
ce.prop = 1;
customElements.define('custom-element', Main.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');
}