Skip to content

Commit

Permalink
fix: interpolated style directive updates properly with spread (#8505)
Browse files Browse the repository at this point in the history
fixes #8438
  • Loading branch information
gtm-nayan committed Apr 18, 2023
1 parent 1770fc1 commit 1964535
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,19 +1240,27 @@ export default class ElementWrapper extends Wrapper {

block.chunks.hydrate.push(updater);

const self_deps = expression.dynamic_dependencies();
const all_deps = new Set([
...self_deps,
...this.dynamic_style_dependencies
]);

let condition = block.renderer.dirty([...all_deps]);

// Assume that style has changed through the spread attribute
if (has_spread) {
if (should_cache && all_deps.size) {
// Update the cached value
block.chunks.update.push(b`
if (${condition}) {
${cached_snippet} = ${snippet};
}`
);
}
block.chunks.update.push(updater);
} else {
const self_deps = expression.dynamic_dependencies();
const all_deps = new Set([
...self_deps,
...this.dynamic_style_dependencies
]);

if (all_deps.size === 0) return;

let condition = block.renderer.dirty([...all_deps]);
if (all_deps.size === 0) return;

if (should_cache) {
condition = x`${condition} && ${cached_snippet} !== (${cached_snippet} = ${snippet})`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
html: `
<div style="background-color: rgb(255, 0, 0);"></div>
`,

test({ assert, target, window, component }) {
const div = target.querySelector('div');
const styles = window.getComputedStyle(div);
assert.equal(styles.backgroundColor, 'rgb(255, 0, 0)');

{
component.backgroundColor = 128;
const div = target.querySelector('div');
const styles = window.getComputedStyle(div);
assert.equal(styles.backgroundColor, 'rgb(128, 0, 0)');
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
export let backgroundColor = 255;
</script>

<div style:background-color="rgb({backgroundColor}, 0, 0)" {...$$restProps} />

0 comments on commit 1964535

Please sign in to comment.