Skip to content

Commit

Permalink
fix: style directive not updating when style attribute is present and…
Browse files Browse the repository at this point in the history
… style directive is updated via an object prop. fixes #9185 (#9187)

fixes #9185.

I narrowed down the issue to the bug surfacing when we use object properties to update style attributes and directives. This fix removes the size check (because a single object will be of size 1 but can affect n attributes/directives via its properties).

In addition, the order of the OR is switched as the earlier condition has some reactive assignments which are not run in the current order when style_changed_var is truthy.
  • Loading branch information
kelvinsjk authored Sep 20, 2023
1 parent 115ea1f commit d5a1822
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-tigers-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: update style directive when style attribute is present and is updated via an object prop
Original file line number Diff line number Diff line change
Expand Up @@ -1240,11 +1240,7 @@ export default class ElementWrapper extends Wrapper {
}
if (this.dynamic_style_dependencies.size > 0) {
maybe_create_style_changed_var();
// If all dependencies are same as the style attribute dependencies, then we can skip the dirty check
condition =
all_deps.size === this.dynamic_style_dependencies.size
? style_changed_var
: x`${style_changed_var} || ${condition}`;
condition = x`${condition} || ${style_changed_var}`;
}
block.chunks.update.push(b`
if (${condition}) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
html: `
<p style="background-color: green; font-size: 12px;"></p>
`,

test({ assert, target, window, component }) {
const p = target.querySelector('p');
const styles = window.getComputedStyle(p);
assert.equal(styles.backgroundColor, 'green');
assert.equal(styles.fontSize, '12px');

{
component.modify = true;
const p = target.querySelector('p');
const styles = window.getComputedStyle(p);
assert.equal(styles.backgroundColor, 'green');
assert.equal(styles.fontSize, '50px');
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let settings = {
fontSize: 12,
bg: 'green'
};
export let modify = false;
$: if (modify) {
settings.fontSize = 50;
}
</script>

<p style:font-size="{settings.fontSize}px" style="background-color: {settings.bg}" />

0 comments on commit d5a1822

Please sign in to comment.