Skip to content

fix: react to mutated slot props in legacy mode #10197

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

Merged
merged 2 commits into from
Jan 16, 2024
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/olive-shirts-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: react to mutated slot props in legacy mode
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,12 @@ export const template_visitors = {
const name = node.expression === null ? node.name : node.expression.name;
return b.const(
name,
b.call('$.derived', b.thunk(b.member(b.id('$$slotProps'), b.id(node.name))))
b.call(
// in legacy mode, sources can be mutated but they're not fine-grained.
// Using the safe-equal derived version ensures the slot is still updated
state.analysis.runes ? '$.derived' : '$.derived_safe_equal',
b.thunk(b.member(b.id('$$slotProps'), b.id(node.name)))
)
);
}
},
Expand Down
12 changes: 12 additions & 0 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,18 @@ export function derived(init) {
return signal;
}

/**
* @template V
* @param {() => V} init
* @returns {import('./types.js').ComputationSignal<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function derived_safe_equal(init) {
const signal = derived(init);
signal.e = safe_equal;
return signal;
}

/**
* @template V
* @param {V} initial_value
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/internal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
source,
mutable_source,
derived,
derived_safe_equal,
prop,
user_effect,
render_effect,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export let things;
</script>

<div>
{#each things as thing}
<slot {thing}/>
{/each}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { tick } from 'svelte';
import { test } from '../../test';

export default test({
html: `
<button>mutate</button>
<div>
<span>hello</span>
</div>
`,

async test({ assert, target }) {
target.querySelector('button')?.click();
await tick();
assert.htmlEqual(
target.innerHTML,
`
<button>mutate</button>
<div>
<span>bye</span>
</div>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import Nested from './Nested.svelte';

let things = [{ text: 'hello' }];
</script>

<button on:click={() => things[0].text = 'bye'}>mutate</button>
<Nested {things} let:thing>
<span>{thing.text}</span>
</Nested>