Skip to content

Commit 65fa717

Browse files
authored
fix: port over props that were set prior to initialization (#9704)
Svelte 5 version of #9701
1 parent 2e461eb commit 65fa717

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

.changeset/hungry-tips-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: port over props that were set prior to initialization

packages/svelte/src/internal/client/custom-element.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ if (typeof HTMLElement === 'function') {
127127
this.$$d[name] = get_custom_element_value(name, attribute.value, this.$$p_d, 'toProp');
128128
}
129129
}
130+
// Port over props that were set programmatically before ce was initialized
131+
for (const key in this.$$p_d) {
132+
// @ts-expect-error
133+
if (!(key in this.$$d) && this[key] !== undefined) {
134+
// @ts-expect-error
135+
this.$$d[key] = this[key]; // don't transform, these were set through JavaScript
136+
// @ts-expect-error
137+
delete this[key]; // remove the property that shadows the getter/setter
138+
}
139+
}
130140
this.$$c = createClassComponent({
131141
component: this.$$ctor,
132142
target: this.shadowRoot || this,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test } from '../../assert';
2+
3+
const tick = () => Promise.resolve();
4+
5+
export default test({
6+
async test({ assert, target, componentCtor }) {
7+
target.innerHTML = '<custom-element red white></custom-element>';
8+
const ce = target.querySelector('custom-element');
9+
ce.prop = 1;
10+
customElements.define('custom-element', componentCtor.element);
11+
await tick();
12+
await tick();
13+
14+
const ce_root = target.querySelector('custom-element').shadowRoot;
15+
const p = ce_root.querySelector('p');
16+
17+
assert.equal(p.textContent, '1');
18+
19+
ce.prop = 2;
20+
await tick();
21+
await tick();
22+
23+
assert.equal(p.textContent, '2');
24+
}
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let prop;
3+
</script>
4+
5+
<p>{prop}</p>

0 commit comments

Comments
 (0)