Skip to content

Commit 88152d8

Browse files
committed
fix: bail-out of event delegation for each block reference
1 parent cf63220 commit 88152d8

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

.changeset/modern-ghosts-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: bail-out of event delegation for each block reference

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function get_delegated_event(node, context) {
157157
if (
158158
binding !== null &&
159159
// Bail-out if we reference anything from the EachBlock (for now) that mutates in non-runes mode,
160-
((!context.state.analysis.runes && binding.kind === 'each') ||
160+
(binding.kind === 'each' ||
161161
// or any normal not reactive bindings that are mutated.
162162
(binding.kind === 'normal' && context.state.analysis.runes) ||
163163
// or any reactive imports (those are rewritten) (can only happen in legacy mode)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<button>Click me and get error</button><button>Click me and get error</button><button>Click me and get error</button>`,
5+
6+
async test({ assert, target }) {
7+
const [btn1, btn2, btn3] = target.querySelectorAll('button');
8+
9+
// ensure each click doesn't trigger an error
10+
await btn1.click();
11+
await Promise.resolve();
12+
13+
await btn2.click();
14+
await Promise.resolve();
15+
16+
await btn3.click();
17+
await Promise.resolve();
18+
19+
assert.htmlEqual(
20+
target.innerHTML,
21+
`<button>Click me and get error</button><button>Click me and get error</button><button>Click me and get error</button>`
22+
);
23+
}
24+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let arr = $state([1,2,3]);
3+
</script>
4+
5+
{#each arr as value}
6+
<button onclick={() => value += 1}>Click me and get error</button>
7+
{/each}

0 commit comments

Comments
 (0)