Overview
This example demonstrates how bubbling events simplify event handling in component hierarchies. Instead of attaching listeners to each child component, a single listener on a parent container can handle events from all descendants.Bubbling vs Non-Bubbling Events
Non-Bubbling (Default)
Requires a listener on each child component:Bubbling
Uses a single listener on a parent container:Child Component: c-contact-list-item-bubbling
The bubbling version creates events withbubbles: true.
JavaScript (contactListItemBubbling.js)
bubbles: trueallows the event to propagate up the DOM tree- Event can be caught by any ancestor component
- Event name uses lowercase convention (‘contactselect’)
Template (contactListItemBubbling.html)
Parent Component: c-event-bubbling
The parent places a single event listener on a container element.JavaScript (eventBubbling.js)
event.targetreferences the component that fired the event- Single handler processes events from all child components
- Access child component’s public properties via
event.target
Template (eventBubbling.html)
- Listener
oncontactselectis on thelightning-layout-item, not individual children - Child components don’t need event listeners in their tags
- More efficient for lists with many items
CustomEvent Options
bubbles
bubbles: false (default)
- Event only fires on the element that dispatched it
- Parent must listen directly on the child component
- Does not propagate up the DOM tree
bubbles: true
- Event propagates up through parent elements
- Any ancestor can listen for the event
- Stops at shadow DOM boundary (unless
composed: true)
composed
composed: false (default)
- Event does not cross shadow DOM boundaries
- Only visible within the same shadow tree
composed: true
- Event crosses shadow DOM boundaries
- Can be caught by components outside the shadow tree
- Usually paired with
bubbles: true
Combined Options Example
When to Use Bubbling
Use bubbling events when:- Handling events from multiple child components
- Child components are in a loop or list
- Event needs to reach grandparent or higher ancestor
- Simplifying event listener management
- Event is specific to one child component
- Preventing event propagation is desired
- Explicit component relationships are important
Event Flow
- User clicks a contact in the bubbling list
- Child component creates
CustomEventwithbubbles: true - Event is dispatched from child component
- Event bubbles up through DOM tree
- Parent’s listener on container element catches the event
- Parent accesses child component via
event.target - Parent retrieves contact data and displays details
Best Practices
- Use unique, descriptive event names (e.g., ‘contactselect’ not ‘select’)
- Set
bubbles: truefor events from list items or repeated components - Use
event.targetto identify which child fired the event - Consider
composed: trueonly when crossing shadow DOM is necessary - Document event names and expected detail structure
Source Files
- Child:
force-app/main/default/lwc/contactListItemBubbling/ - Parent:
force-app/main/default/lwc/eventBubbling/ - Compare with:
force-app/main/default/lwc/eventWithData/(non-bubbling version)
