Skip to main content

Overview

Event bubbling allows events to propagate up the DOM tree, enabling communication from deeply nested components to ancestor components without requiring event handlers on every intermediate element.

Bubbling vs Non-Bubbling Events

Non-Bubbling Events (Default)

By default, custom events do not bubble. They only reach the immediate parent:

Bubbling Events

Set bubbles: true to make events propagate up the DOM tree:

Example: Contact Selection with Bubbling

Child Component (c-contact-list-item-bubbling)

contactListItemBubbling.js
contactListItemBubbling.html

Parent Component (c-event-bubbling)

eventBubbling.js
eventBubbling.html

Benefits of Bubbling Events

Single Event Handler

With bubbling events, you can attach one handler to a container instead of multiple handlers:

Simplified Template Logic

Bubbling reduces template complexity, especially with nested or dynamic components.

Composed Events

The composed property controls whether events cross shadow DOM boundaries.

Within Same Component Tree

Across Shadow DOM Boundaries

Use composed: true sparingly. Most component communication should stay within the component tree. Only use composed events when you need to communicate across shadow DOM boundaries (e.g., custom events that need to reach the document level).

Event Properties

event.target

The element that originally fired the event:

event.currentTarget

The element where the handler is attached:

event.detail

Custom data passed with the event:

CustomEvent Configuration

Best Practices

  1. Use bubbling for lists: When rendering multiple items, use bubbling to reduce handlers
  2. Keep events scoped: Avoid composed: true unless absolutely necessary
  3. Use event.target carefully: Ensure the target element has the properties you need
  4. Name bubbling events distinctly: Avoid naming conflicts with standard DOM events
  5. Document bubbling behavior: Clearly indicate when your component fires bubbling events
Don’t rely on event.detail with bubbling events across component boundaries. Prefer accessing data from event.target properties or re-dispatching events with appropriate detail at each level.

When to Use Bubbling

Good Use Cases

  • Lists with many items
  • Nested component hierarchies
  • Delegation patterns (single handler for multiple sources)

Avoid Bubbling When

  • Only parent and child communicate (use simple events)
  • Passing complex data (use event.detail on non-bubbling events)
  • Event name might conflict with DOM events

Source Code

View the complete source:
  • Child: force-app/main/default/lwc/contactListItemBubbling/
  • Parent: force-app/main/default/lwc/eventBubbling/

Additional Resources