Skip to content

Instantly share code, notes, and snippets.

@joshyu
Last active August 16, 2021 01:40
Show Gist options
  • Save joshyu/4685744849c1889b80a3c6156197ba2f to your computer and use it in GitHub Desktop.
Save joshyu/4685744849c1889b80a3c6156197ba2f to your computer and use it in GitHub Desktop.
detect internal component when clicking on webcomponent

When we want to detect which internal component we are clicking on some webcomponent,we can use event.composed to check whether the event is broadcasted through a webcomponent, and then check the array list returned from event.composedPath() to look up the DOM, if found then we can confirm we are clicking on the specified DOM node and do some specific things.

https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath

@joshyu
Copy link
Author

joshyu commented Aug 16, 2021

$('.webcomponent-class').on('click', event => {
     const paths = event.originalEvent.composedPath();
     const clickOnCloseButton = paths.filter(node=>{
       return node && node.classList && node.classList.contains('webcomponent-internal-dom-class');
     }).length >0;

    if(clickOnCloseButton){
      $(event.currentTarget).addClass('hide');
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment