Sub topic 9
Bubbling and Crossing Shadow Boundaries with bubbles and composed
This chapter assumes you can already create a CustomEvent, dispatch it from a child, and handle it in a parent template with an on attribute. Here you will learn how the bubbles and composed options decide which components receive an event.
Default behavior: the event stays at the host
When you dispatch an event with no options, it fires on the child's host element and goes no further. Only a listener attached directly to that element receives it. This is why onproductselected works when you put it on <c-product-tile> itself.
this.dispatchEvent(
new CustomEvent('productselected', {
detail: { productId: this.productId }
})
);The default is bubbles: false and composed: false. It is the safest choice because only the direct parent can react.
What bubbles does
Bubbling means the event travels upward through ancestor elements after it fires. With bubbles: true, an element between the child and the root of the parent's template can also handle it.
// productTile.js (dispatch)
this.dispatchEvent(
new CustomEvent('productselected', {
detail: { productId: this.productId },
bubbles: true
})
);<!-- productList.html -->
<template>
<div onproductselected={handleProductSelected}>
<c-product-tile product-id="p1" name="Desk"></c-product-tile>
<c-product-tile product-id="p2" name="Chair"></c-product-tile>
</div>
</template>One handler on the wrapper div now covers every tile inside it. This is useful for lists, because you do not need a listener on each tile.
Bubbling is limited to the parent's own template. The event reaches the template root and stops there. It does not continue into the parent's parent.
What composed does
Each LWC component has a shadow tree, which is a private DOM subtree. The boundary around it is called the shadow boundary. By default, events do not cross it.
Setting composed: true lets a bubbling event leave the shadow tree and continue up through the ancestors of the host element. Together, the two options give four combinations:
| bubbles | composed | Who can receive the event |
|---|---|---|
| false | false | Only listeners on the child's host element |
| true | false | Host element and ancestors inside the parent's template |
| true | true | Every ancestor, across all shadow boundaries, up to the document |
| false | true | Host element and the document-level path only in limited ways. Rarely useful |
Why composed should be rare
A composed event leaks past encapsulation. Any component up the tree can listen for it, including components you did not write. Name collisions become possible, and the event becomes part of your component's public contract. For these reasons, treat composed: true as a last resort.
A safer pattern for deep trees is to have the middle component catch the event and dispatch its own.
// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
handleProductSelected(event) {
event.stopPropagation();
this.dispatchEvent(
new CustomEvent('selectionchanged', {
detail: { id: event.detail.productId }
})
);
}
}Common errors
- Handler on a wrapper never fires. The child did not set
bubbles: true, so the event never reaches thediv. - Grandparent never receives the event. Bubbling alone stops at the parent's template root. Re-dispatch from the parent, or add
composed: trueif that is justified. - Handler fires more than once. A bubbling event can match a listener on both the child and a wrapper. Call
stopPropagation()where you have handled it. - Event name is rejected or ignored. Names should be lowercase with no spaces or hyphens, as covered in the naming chapter.
Exercise
A c-product-tile dispatches productselected with bubbles: true and composed: false. It sits inside c-product-list, which sits inside c-shop-page. A div inside the c-product-list template has onproductselected={handle}, and c-shop-page has onproductselected={handle} on its <c-product-list> tag. Which handlers run, and what change would make the second one run?
Show answer
Only the div handler in c-product-list runs. The event bubbles within the list's template but does not cross the shadow boundary, so the handler on <c-product-list> in c-shop-page does not run. Adding composed: true to the event would make it run, but a better design is for c-product-list to catch the event and dispatch its own event to c-shop-page.