Skip to content

Commit

Permalink
Don't lose class: directive classes on an element with `{...spread}…
Browse files Browse the repository at this point in the history
…` attributes when updating (#3781)

* include all class: directive updates on elements with spreads (#3421)

* add test

* update changelog
  • Loading branch information
Conduitry authored Oct 24, 2019
1 parent 8c0c15c commit 0419039
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fix `{#each}` context not shadowing outer scope when using `bind:` ([#1565](https://github.com/sveltejs/svelte/issues/1565))
* Fix edge cases in matching selectors against elements ([#1710](https://github.com/sveltejs/svelte/issues/1710))
* Allow exiting a reactive block early with `break $` ([#2828](https://github.com/sveltejs/svelte/issues/2828))
* Don't lose `class:` directive classes on an element with `{...spread}` attributes when updating ([#3421](https://github.com/sveltejs/svelte/issues/3421))
* Check attributes have changed before setting them to avoid image flicker ([#3579](https://github.com/sveltejs/svelte/pull/3579))
* Fix generating malformed code for `{@debug}` tags with no dependencies ([#3588](https://github.com/sveltejs/svelte/issue/3588))
* Fix generated code in specific case involving compound ifs and child components ([#3595](https://github.com/sveltejs/svelte/issue/3595))
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ export default class ElementWrapper extends Wrapper {
}

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

block.chunks.hydrate.push(updater);

if ((dependencies && dependencies.size > 0) || this.class_dependencies.length) {
if (has_spread) {
block.chunks.update.push(updater);
} else if ((dependencies && dependencies.size > 0) || this.class_dependencies.length) {
const all_dependencies = this.class_dependencies.concat(...dependencies);
const condition = changed(all_dependencies);

Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/spread-element-class/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
html: `<div class='foo bar'>hello</div>`,
test({ assert, component, target }) {
component.blah = 'goodbye';
assert.htmlEqual(target.innerHTML, `<div class='foo bar'>goodbye</div>`);
}
};
5 changes: 5 additions & 0 deletions test/runtime/samples/spread-element-class/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let blah = 'hello';
</script>

<div class='foo' class:bar={true} {...{}}>{blah}</div>

0 comments on commit 0419039

Please sign in to comment.