Skip to content

Commit 0419039

Browse files
authored
Don't lose class: directive classes on an element with {...spread} attributes when updating (sveltejs#3781)
* include all class: directive updates on elements with spreads (sveltejs#3421) * add test * update changelog
1 parent 8c0c15c commit 0419039

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fix `{#each}` context not shadowing outer scope when using `bind:` ([#1565](https://github.com/sveltejs/svelte/issues/1565))
66
* Fix edge cases in matching selectors against elements ([#1710](https://github.com/sveltejs/svelte/issues/1710))
77
* Allow exiting a reactive block early with `break $` ([#2828](https://github.com/sveltejs/svelte/issues/2828))
8+
* Don't lose `class:` directive classes on an element with `{...spread}` attributes when updating ([#3421](https://github.com/sveltejs/svelte/issues/3421))
89
* Check attributes have changed before setting them to avoid image flicker ([#3579](https://github.com/sveltejs/svelte/pull/3579))
910
* Fix generating malformed code for `{@debug}` tags with no dependencies ([#3588](https://github.com/sveltejs/svelte/issue/3588))
1011
* Fix generated code in specific case involving compound ifs and child components ([#3595](https://github.com/sveltejs/svelte/issue/3595))

src/compiler/compile/render_dom/wrappers/Element/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ export default class ElementWrapper extends Wrapper {
809809
}
810810

811811
add_classes(block: Block) {
812+
const has_spread = this.node.attributes.some(attr => attr.is_spread);
812813
this.node.classes.forEach(class_directive => {
813814
const { expression, name } = class_directive;
814815
let snippet;
@@ -824,7 +825,9 @@ export default class ElementWrapper extends Wrapper {
824825

825826
block.chunks.hydrate.push(updater);
826827

827-
if ((dependencies && dependencies.size > 0) || this.class_dependencies.length) {
828+
if (has_spread) {
829+
block.chunks.update.push(updater);
830+
} else if ((dependencies && dependencies.size > 0) || this.class_dependencies.length) {
828831
const all_dependencies = this.class_dependencies.concat(...dependencies);
829832
const condition = changed(all_dependencies);
830833

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
html: `<div class='foo bar'>hello</div>`,
3+
test({ assert, component, target }) {
4+
component.blah = 'goodbye';
5+
assert.htmlEqual(target.innerHTML, `<div class='foo bar'>goodbye</div>`);
6+
}
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let blah = 'hello';
3+
</script>
4+
5+
<div class='foo' class:bar={true} {...{}}>{blah}</div>

0 commit comments

Comments
 (0)