Last active
January 25, 2022 17:06
-
-
Save wesruv/ccaf91e1157f2a9e400ffa9d7069c2bb to your computer and use it in GitHub Desktop.
This file contains 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
var nav = document.querySelector('pfe-navigation'); | |
// Reach into shadow DOM and find all top level items that are links (not buttons) | |
if (nav.shadowRoot && typeof nav.shadowRoot.querySelectorAll === 'function') { | |
/** | |
* Fire custom event with correct details | |
* @param {Element} self Element that was activated | |
* @param {Element} componentParent The element that owns the shadowRoot the activated element was in | |
*/ | |
function topLevelLinkClickHandler(self, componentParent) { | |
var shadowDomEvent = new CustomEvent('pfe-shadow-dom-event', | |
{ | |
detail: { | |
target: self, | |
parent: componentParent, | |
} | |
} | |
); | |
document.dispatchEvent(shadowDomEvent); | |
} | |
var topLevelLinks = nav.shadowRoot.querySelectorAll('a.pfe-navigation__menu-link'); | |
for (var linkIndex = 0; linkIndex < topLevelLinks.length; linkIndex++) { | |
var topLevelLink = topLevelLinks[linkIndex]; | |
// Emit pfe-shadow-dom-event for EDDL to pick up | |
topLevelLink.addEventListener('click', function(e) { | |
topLevelLinkClickHandler(e.currentTarget, nav); | |
}); | |
} | |
} | |
// To show it's working | |
document.addEventListener('pfe-shadow-dom-event', function(event) { | |
console.log('wakka', event); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment