Created
December 6, 2020 03:35
-
-
Save pinkhominid/ac09780a5dfd356f6a5831d61c09b626 to your computer and use it in GitHub Desktop.
Element.closest() across shadow DOM boundaries
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
// https://stackoverflow.com/questions/54520554/custom-element-getrootnode-closest-function-crossing-multiple-parent-shadowd | |
export function closestElementComposed(selector, base = this) { | |
function __closestFrom(el) { | |
if (!el || el === document || el === window) return null; | |
let found = el.closest(selector); | |
return found ? found : __closestFrom(el.getRootNode().host); | |
} | |
return __closestFrom(base); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment