Open giacomoran opened 1 year ago
Findings - the destroy check whether it has some parent div and since block chunks read from top to bottom, it says to detach the div first then destroy the child component.
I would like to work on this issue
Hey @tanhauhau can you guide me if this solution can cause any edge case?, in this case, I am checking if there is a parent node instead of push append to the first, and for nested components then the last child will be at the first index.
if (parent_node) {
block.chunks.destroy.unshift(b`
@destroy_component(${name}, detaching);
`);
} else {
block.chunks.destroy.push(b`
@destroy_component(${name}, ${parent_node ? null : 'detaching'});
`);
}
I think current behaivior is correct.
I created REPL with valinna JS but blur event occured.
https://codepen.io/baseballyama/pen/VwdVXMY
We should have same behaivior between vanilla JS and Svelte.
-> This is not related to the issue.
Describe the bug
Hi!
If you have something like:
and
show
becomesfalse
, theonDestroy
method ofCustomComponent
will fire after it has disappeared from the DOM. It works as expected if we remove the wrappingdiv
.The reason is that the
if
block callsnode.parentNode.removeChild(node)
on thediv
element before destroying the custom component. When thediv
element is removed from the DOM also the custom component is removed.Our use case
We listen for
blur
events on a component within a list, we remove the listeners inonDestroy
. If the component is removed from the list while holding the focus, theblur
event will be fired. The event is captured becauseonDestroy
has not been called yet, we instead expected theblur
event to be ignored.Reproduction
Full repro (see
App.svelte
,lib/CustomComponent.svelte
and the instructions below): https://stackblitz.com/edit/vitejs-vite-aaxjzf?file=src/App.svelteInstructions:
detach
function (we search fornode.parentNode.removeChild(node)
in the "Source" tab of Chrome DevTools to find it)Logs
No response
System Info
Severity
annoyance