Skip to content

Instantly share code, notes, and snippets.

@avestura
Created September 4, 2022 04:09
Show Gist options
  • Save avestura/f9235709719398305ff510c858cfcc18 to your computer and use it in GitHub Desktop.
Save avestura/f9235709719398305ff510c858cfcc18 to your computer and use it in GitHub Desktop.
Closest get past shadow dom
const isShadowRoot = (maybeRoot) => {
return maybeRoot && maybeRoot.toString() === '[object ShadowRoot]'
}
const closestPastShadowDom = (elem, selector) => {
const closest = elem.closest(selector);
if(closest)
return closest;
const root = elem.getRootNode();
if(isShadowRoot(root)) {
return closestPastShadowDom((root).host, selector)
}
return undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment