While standard DOM events (click, input, keydown) handle direct user input, modular architectures require components to communicate domain-specific events—such as cart:item-added, modal:closed, or user:authenticated. The CustomEvent API allows you to create, dispatch, and intercept custom events across the DOM and Web Components.
A CustomEvent takes an event type string and an optional initialization dictionary:
detailEvent payloads passed in { detail } are available on event.detail:
composed: trueBy default, events dispatched inside a Shadow DOM are trapped inside and cannot bubble up into the main document. Setting composed: true allows the event to penetrate through the shadow boundary into the outer Light DOM:
If you set cancelable: true, listeners can call event.preventDefault(). The caller of dispatchEvent() receives a boolean indicating whether the action was permitted:
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Storage Quotas & Persistence Policies | Debouncing vs Throttling Deep Dive |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.