azure-devops-ui
Version:
React components for building web UI in Azure DevOps
39 lines (38 loc) • 1.25 kB
JavaScript
export function elementContains(parent, child) {
let currentElement = child;
while (currentElement) {
if (currentElement === parent) {
return true;
}
currentElement = currentElement.parentElement;
}
return false;
}
/**
* Gets the window or iframe container of the target element
* @param element
*/
export function getWindow(element) {
if (typeof window === "undefined") {
return undefined;
}
else {
return element && element.ownerDocument && element.ownerDocument.defaultView ? element.ownerDocument.defaultView : window;
}
}
/**
* Finds the nearest parent element of the target that is scrollable on the Y axis
* @param element
*/
export function findScrollableParent(element) {
let currentElement = element;
while (currentElement && currentElement !== document.body) {
const styles = getComputedStyle(currentElement);
let overflowY = styles ? styles.getPropertyValue("overflow-y") : "";
if (overflowY && (overflowY === "scroll" || overflowY === "auto")) {
return currentElement;
}
currentElement = currentElement.parentElement;
}
return document.body;
}