Skip to content

fix: propagate custom element component prop changes #12774

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

Merged
merged 2 commits into from
Aug 11, 2024
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/poor-mugs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: propagate custom element component prop changes
15 changes: 13 additions & 2 deletions packages/svelte/src/internal/client/dom/elements/custom-element.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createClassComponent } from '../../../../legacy/legacy-client.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
import { append } from '../template.js';
import { define_property, object_keys } from '../../../shared/utils.js';
import { define_property, get_descriptor, object_keys } from '../../../shared/utils.js';

/**
* @typedef {Object} CustomElementPropDefinition
Expand Down Expand Up @@ -305,7 +305,18 @@ export function create_custom_element(
set(value) {
value = get_custom_element_value(prop, value, props_definition);
this.$$d[prop] = value;
this.$$c?.$set({ [prop]: value });
var component = this.$$c;

if (component) {
// // If the instance has an accessor, use that instead
var setter = get_descriptor(component, prop)?.get;

if (setter) {
component[prop] = value;
} else {
component.$set({ [prop]: value });
}
}
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { flushSync } from 'svelte';
import { test } from '../../assert';
const tick = () => Promise.resolve();

export default test({
async test({ assert, target }) {
target.innerHTML = '<custom-element></custom-element>';
await tick();
await tick();

/** @type {any} */
const el = target.querySelector('custom-element');
const button = el.shadowRoot.querySelector('button');

assert.equal(button.textContent, '0');
assert.equal(el.count, 0);

button.click();

flushSync();

assert.equal(button.textContent, '1');
assert.equal(el.count, 1);

el.count = 0;

assert.equal(button.textContent, '0');
assert.equal(el.count, 0);

button.click();

flushSync();

assert.equal(button.textContent, '1');
assert.equal(el.count, 1);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<svelte:options customElement="custom-element" />

<script>
export let count = 0;
</script>

<button onclick={() => count++}>{count}</button>
Loading