Skip to content

Instantly share code, notes, and snippets.

@pinkhominid
Created December 6, 2020 03:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinkhominid/ac09780a5dfd356f6a5831d61c09b626 to your computer and use it in GitHub Desktop.
Save pinkhominid/ac09780a5dfd356f6a5831d61c09b626 to your computer and use it in GitHub Desktop.
Element.closest() across shadow DOM boundaries
// 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