fix(runtime): add addEventListener support for slot elements in scope components#6281
Merged
christian-bromann merged 2 commits intomainfrom Jun 10, 2025
Merged
fix(runtime): add addEventListener support for slot elements in scope components#6281christian-bromann merged 2 commits intomainfrom
christian-bromann merged 2 commits intomainfrom
Conversation
…d components Fixes issue where onSlotchange event listeners would fail with "addEventListener is not a function" error when used on <slot> elements in scoped components. **Root Cause:** In scoped components, slots are polyfilled using text/comment nodes since native shadow DOM is not used. These nodes lack native addEventListener methods, causing runtime errors when event listeners are attached. **Solution:** - Enhanced patchSlotNode() to add custom addEventListener, removeEventListener, and dispatchEvent methods to polyfilled slot nodes - Fixed timing issue by calling patchSlotNode() immediately after slot reference node creation, before updateElement() attempts to attach event listeners - Added defensive checks to avoid overwriting existing methods or double-patching nodes **Changes:** - src/runtime/slot-polyfill-utils.ts: Added event listener polyfill methods with proper TypeScript types - src/runtime/vdom/vdom-render.ts: Moved slot patching to occur before event listener attachment - src/runtime/test/scoped.spec.tsx: Added comprehensive tests verifying event listener functionality works end-to-end **Testing:** Added tests that verify: - Event listeners can be attached without errors - slotchange events are properly dispatched and handled - Multiple events work correctly - Component state updates as expected Closes #6269
3 tasks
3 tasks
Contributor
|
@christian-bromann I think this is just an issue with MockDoc as the native dom can happily attach / remove event listeners from text / comment nodes - which is how it works in the browser in the first place. Perhaps this code can be moved to mock-doc |
2 tasks
Member
Author
|
Great catch @johnjenkins , mind taking a look at #6287? |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What is the current behavior?
GitHub Issue Number: #6269
When using
onSlotchangeevent listeners on<slot>elements in scoped components, the application crashes with:This occurs because in scoped components, Stencil uses a slot polyfill that replaces
<slot>elements with text/comment nodes (since native shadow DOM is not used). These nodes don't have nativeaddEventListenermethods, causing runtime errors when the framework attempts to attach event listeners.What is the new behavior?
onSlotchangeevent listeners now work correctly on<slot>elements in scoped componentsaddEventListener,removeEventListener, anddispatchEventmethods for polyfilled slot nodesslotchangeevents can be dispatched and handled properlyTechnical Details:
patchSlotNode()to add event listener polyfill methods to slot reference nodesDocumentation
No documentation changes required - this restores expected functionality for existing APIs.
Does this introduce a breaking change?
This is a bug fix that restores expected functionality. No migration is required.
Testing
Unit Tests Added:
onSlotchangerender without errorsslotchangeevents are properly dispatched and handledManual Testing:
Test Command:
All tests pass with comprehensive coverage of the event listener polyfill.
Other information
Before this fix:
After this fix:
Key Files Modified:
src/runtime/slot-polyfill-utils.ts- Added event listener polyfillsrc/runtime/vdom/vdom-render.ts- Fixed timing of slot patchingsrc/runtime/test/scoped.spec.tsx- Added comprehensive testsThis fix ensures that scoped components have feature parity with shadow DOM components for slot event handling.