Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: svelte:component spread props change not picked up #9006

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-wasps-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: `svelte:component` spread props change not picked up
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ export default class InlineComponentWrapper extends Wrapper {
`);
if (all_dependencies.size) {
const condition = renderer.dirty(Array.from(all_dependencies));
if (this.node.name === 'svelte:component') {
// statements will become switch_props function body
// rewrite last statement, add props update logic
statements[statements.length - 1] = b`
if (#dirty !== undefined && ${condition}) {
${props} = @get_spread_update(${levels}, [
${changes}
]);
} else {
for (let #i = 0; #i < ${levels}.length; #i += 1) {
${props} = @assign(${props}, ${levels}[#i]);
}
}
`;
}
updates.push(b`
const ${name_changes} = ${condition} ? @get_spread_update(${levels}, [
${changes}
Expand Down Expand Up @@ -396,7 +411,7 @@ export default class InlineComponentWrapper extends Wrapper {
block.chunks.init.push(b`
var ${switch_value} = ${snippet};

function ${switch_props}(#ctx) {
function ${switch_props}(#ctx, #dirty) {
${
(this.node.attributes.length > 0 || this.node.bindings.length > 0) &&
b`
Expand Down Expand Up @@ -464,7 +479,7 @@ export default class InlineComponentWrapper extends Wrapper {

if (${switch_value}) {
${update_insert}
${name} = @construct_svelte_component(${switch_value}, ${switch_props}(#ctx));
${name} = @construct_svelte_component(${switch_value}, ${switch_props}(#ctx, #dirty));

${munged_bindings}
${munged_handlers}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value;
</script>

<p>value(1) = {value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let value;
</script>

<p>value(2) = {value}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
html: `
<p>value(1) = 1</p>
<button>Toggle Component</button>
`,

async test({ assert, window, target }) {
const button = target.querySelector('button');
await button.dispatchEvent(new window.Event('click'));
assert.htmlEqual(
target.innerHTML,
`
<p>value(2) = 2</p>
<button>Toggle Component</button>
`
);
await button.dispatchEvent(new window.Event('click'));
assert.htmlEqual(
target.innerHTML,
`
<p>value(1) = 1</p>
<button>Toggle Component</button>
`
);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import Comp1 from './Comp1.svelte';
import Comp2 from './Comp2.svelte';

let view = Comp1;

$: props = view === Comp1 ? { value: 1 } : { value: 2 };
</script>

<svelte:component this={view} {...props} />

<button on:click={(e) => (view = view === Comp1 ? Comp2 : Comp1)}>Toggle Component</button>