Skip to content

Commit 6f392d6

Browse files
authored
fix: properly set value property of custom elements (#15206)
Avoid going through the `element.value = element.__value = newValue` condition because `__value` is actually how Lit stores the current value on the element, and messing with that would break things: Lit would think the value hasn't changed (because `__value` is already set to the new value by us) and doesn't fire an update. fixes #15194
1 parent 872ee51 commit 6f392d6

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

.changeset/great-planes-swim.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: properly set `value` property of custom elements

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,10 @@ export function set_attributes(
363363
element.style.cssText = value + '';
364364
} else if (key === 'autofocus') {
365365
autofocus(/** @type {HTMLElement} */ (element), Boolean(value));
366-
} else if (key === '__value' || (key === 'value' && value != null)) {
367-
// @ts-ignore
368-
element.value = element[key] = element.__value = value;
366+
} else if (!is_custom_element && (key === '__value' || (key === 'value' && value != null))) {
367+
// @ts-ignore We're not running this for custom elements because __value is actually
368+
// how Lit stores the current value on the element, and messing with that would break things.
369+
element.value = element.__value = value;
369370
} else if (key === 'selected' && is_option_element) {
370371
set_selected(/** @type {HTMLOptionElement} */ (element), value);
371372
} else {
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { test } from '../../test';
22

33
export default test({
4-
mode: ['client', 'server'],
4+
mode: ['client'],
55
async test({ assert, target }) {
66
const my_element = /** @type HTMLElement & { object: { test: true }; } */ (
77
target.querySelector('my-element')
88
);
9-
const my_link = /** @type HTMLAnchorElement & { object: { test: true }; } */ (
10-
target.querySelector('a')
11-
);
129
assert.equal(my_element.getAttribute('string'), 'test');
1310
assert.equal(my_element.hasAttribute('object'), false);
1411
assert.deepEqual(my_element.object, { test: true });
12+
13+
const my_link = /** @type HTMLAnchorElement & { object: { test: true }; } */ (
14+
target.querySelector('a')
15+
);
1516
assert.equal(my_link.getAttribute('string'), 'test');
1617
assert.equal(my_link.hasAttribute('object'), false);
1718
assert.deepEqual(my_link.object, { test: true });
19+
20+
const [value1, value2] = target.querySelectorAll('value-element');
21+
assert.equal(value1.shadowRoot?.innerHTML, '<span>test</span>');
22+
assert.equal(value2.shadowRoot?.innerHTML, '<span>test</span>');
1823
}
1924
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1+
<script module>
2+
customElements.define('value-element', class extends HTMLElement {
3+
4+
constructor() {
5+
super();
6+
this.attachShadow({ mode: 'open' });
7+
}
8+
9+
set value(v) {
10+
if (this.__value !== v) {
11+
this.__value = v;
12+
this.shadowRoot.innerHTML = `<span>${v}</span>`;
13+
}
14+
}
15+
});
16+
</script>
17+
118
<my-element string="test" object={{ test: true }}></my-element>
219
<a is="my-link" string="test" object={{ test: true }}></a>
20+
21+
<value-element value="test"></value-element>
22+
<value-element {...{value: "test"}}></value-element>

0 commit comments

Comments
 (0)