Skip to content

Commit bb491f1

Browse files
authored
fix: avoid chromium issue with dispatching blur on element removal (#13694)
* fix: avoid chromium issue with dispatching blur on element removal * fix: avoid chromium issue with dispatching blur on element removal * fix: avoid chromium issue with dispatching blur on element removal * active effect too * try/finally
1 parent 793a8de commit bb491f1

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

.changeset/seven-dancers-brush.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: avoid chromium issue with dispatching blur on element removal

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
set_is_destroying_effect,
1717
set_is_flushing_effect,
1818
set_signal_status,
19-
untrack
19+
untrack,
20+
set_active_effect
2021
} from '../runtime.js';
2122
import {
2223
DIRTY,
@@ -375,13 +376,26 @@ export function destroy_effect(effect, remove_dom = true) {
375376
/** @type {TemplateNode | null} */
376377
var node = effect.nodes_start;
377378
var end = effect.nodes_end;
379+
var previous_reaction = active_reaction;
380+
var previous_effect = active_effect;
378381

379-
while (node !== null) {
380-
/** @type {TemplateNode | null} */
381-
var next = node === end ? null : /** @type {TemplateNode} */ (get_next_sibling(node));
382+
// Really we only need to do this in Chromium because of https://chromestatus.com/feature/5128696823545856,
383+
// as removal of the DOM can cause sync `blur` events to fire, which can cause logic to run inside
384+
// the current `active_reaction`, which isn't what we want at all. Additionally, the blur event handler
385+
// might create a derived or effect and they will be incorrectly attached to the wrong thing
386+
set_active_reaction(null);
387+
set_active_effect(null);
388+
try {
389+
while (node !== null) {
390+
/** @type {TemplateNode | null} */
391+
var next = node === end ? null : /** @type {TemplateNode} */ (get_next_sibling(node));
382392

383-
node.remove();
384-
node = next;
393+
node.remove();
394+
node = next;
395+
}
396+
} finally {
397+
set_active_reaction(previous_reaction);
398+
set_active_effect(previous_effect);
385399
}
386400

387401
removed = true;

0 commit comments

Comments
 (0)