Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to use it inside a Shadow DOM. #251

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

igorjs
Copy link

@igorjs igorjs commented Apr 26, 2024

Shadow DOM directly influences how event propagate through the DOM.

The shadow root is the root node for a shadow tree. It is possible to listen to any event originating from a shadow tree by adding the appropriate event listener directly on the shadow root.

Similar to how events work in the light DOM, events only bubble in the shadow DOM if the bubbles option is set to true: new Event('test', { bubbles: true }).

Events dispatched from within the shadow tree invoke the shadow root listeners if the event is marked as bubbles.

By default, events don’t propagate outside shadow trees. This default behavior ensures that internal DOM events don’t inadvertently leak into the document.

For an event to traverse shadow DOM boundaries, it has to be configured as composed: new Event('test', { bubbles: true, composed: true }).

This might be counterintuitive, but a composed event always propagates outside the shadow boundary regardless of whether it is bubbling or not.

The Event.prototype.composedPath method returns an array with all the nodes, in order, through which the event propagates.

That's why using composedPath()[0] guarantees to always refers to the correct "target".

Source: https://pm.dartus.fr/posts/2021/shadow-dom-and-event-propagation/

@igorjs
Copy link
Author

igorjs commented Oct 24, 2024

@onesine Could you please review this one? Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant