@oruga-ui/oruga-next
Version:
UI components for Vue.js and CSS framework agnostic
32 lines (23 loc) • 981 B
text/typescript
import { isDefined } from "@/utils/helpers";
/**
* Given an element, returns the element who scrolls it.
*/
export function getScrollingParent(target: HTMLElement): HTMLElement | null {
if (target.style.position === "fixed" || !target)
return document.documentElement;
let isScrollingParent = false;
let nextParent = target.parentElement;
while (!isScrollingParent && isDefined(nextParent)) {
if (nextParent === document.documentElement) break;
const { overflow, overflowY } = getComputedStyle(nextParent);
const { scrollHeight, clientHeight } = nextParent; // Both rounded by nature
isScrollingParent =
/(auto|scroll)/.test(`${overflow}${overflowY}`) &&
scrollHeight > clientHeight;
/* ...found it, this one is returned */
if (isScrollingParent) break;
/* ...if not check the next one */
nextParent = nextParent.parentElement;
}
return nextParent;
}