Skip to content

Instantly share code, notes, and snippets.

@RyanHirsch
Created April 1, 2021 14:51
Show Gist options
  • Save RyanHirsch/92acade4313d74fe32696575bb0faaad to your computer and use it in GitHub Desktop.
Save RyanHirsch/92acade4313d74fe32696575bb0faaad to your computer and use it in GitHub Desktop.
Traverse up the dom to find sticky parent
/**
* Traverse up the dom to find a sticky parent node crossing any shadow boundaries
*/
export function findStickyParent(element: HTMLElement): HTMLElement | null {
let elm = element.parentNode || element.host;
while (elm) {
if (elm.style && elm.style.top && window.getComputedStyle(elm).position === `sticky`) {
return elm;
}
elm = elm.parentNode || elm.host;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment