Reputation: 1939
I found a strange behaviour, if I add document.addEventListener
to click on onMount
.
isShow = true
, and Nested component shows.onMount
where runs document.addEventListener('click')
I guess it happens because Svelte does it in one tick. I tried await tick()
, but it didn't help.
I see one of solution is wrap document.addEventListener
to setTimeout
. It works, but I think it is bad decision.
So, is it behaviour normal or is it a bug?
Here is an example: https://svelte.dev/repl/c89c272ca6c245dabf8451ba950d10c0?version=3.6.8
Upvotes: 1
Views: 3606
Reputation: 29605
This is expected behaviour. The simplest solution is probably to add { capture: true }
(or just true
) as the third argument to addEventListener
and removeEventListener
: https://svelte.dev/repl/daee8509d6634a68b2cf35e53f660e34?version=3.6.8. This works because the event has left the capturing phase by the time the nested event listener is attached, and is now in the bubbling phase.
Upvotes: 5