Today I Learned – The Difference Between Bubble and Capture for Events
I've recently been spending some time learning about Svelte and have been going through the tutorials.
When I made it to the event modifiers section, I saw that there's a modifier for capture
where it mentions firing the handler during the capture phase instead of the bubbling phase.
I'm not an expert on front-end development, but I'm not familiar with either of these concepts. Thankfully, the Svelte docs refer out to MDN for a better explanation of the two.
What is Event Bubbling?
Long story short, by default, when an event happens, the element that's been interacted with will fire first and then each parent will receive the event.
So if we have the following HTML structure where there's a body
that has a div
that has a button
With the an event listener at each level:
And we click the button, our <pre>
element will have:
What is Event Capturing?
Event Capturing is the opposite of Event Bubbling where the root parent receives the event and then each inner parent will receive the event, finally making it to the innermost child of the element that started the event.
Let's see what happens with our example when we use the capture approach.
After clicking the button, we'll see the following messages:
Why Would You Use Capture?
By default, events will work in a bubbling fashion and this intuitively makes sense to me since the element that was interacted with is most likely the right element to handle the event.
One case that comes to mind is if you finding yourself attaching the same event listener to every child element. Instead, we could move that up.
For example, let's say that we had the following layout
With this layout, let's say that we need to do some business rules for when any of those buttons are clicked. If we used the bubble down approach, we would have the following code:
This isn't a big deal with three elements, but let's pretend that you had a list with tens of items, or a hundred items. You may run into a performance hit because of the overhead of wiring up that many event listeners.
Instead, we can use one event listener, bound to the common parent. This can accomplish the same affect, without as much complexity.
Let's revisit our JavaScript and make the necessary changes.
With this change, we're now only wiring up a single listener instead of having multiple wirings.
Wrapping Up
In this post, we looked at two different event propagation models, bubble and capture, the differences between the two and when you might want to use capture.