UNPKG

@fluentui/react-northstar

Version:
49 lines (45 loc) 1.63 kB
/** * Returns the parent node or the host of the node argument. * @param node - DOM node. * @returns - parent DOM node. */ export var getParentNode = function getParentNode(node) { if (node.nodeName === 'HTML') return node; return node.parentNode || node.host; }; /** * Returns CSS styles of the given node. * @param node - DOM node. * @returns - CSS styles. */ var getStyleComputedProperty = function getStyleComputedProperty(node) { if (node.nodeType !== 1) return {}; var window = node.ownerDocument.defaultView; return window.getComputedStyle(node, null); }; /** * Returns the first scrollable parent of the given element. * @param node - DOM node. * @returns - the first scrollable parent. */ export var getScrollParent = function getScrollParent(node) { // Return body, `getScroll` will take care to get the correct `scrollTop` from it var parentNode = node && getParentNode(node); // eslint-disable-next-line if (!parentNode) return document.body; switch (parentNode.nodeName) { case 'HTML': case 'BODY': return parentNode.ownerDocument.body; case '#document': return parentNode.body; } // If any of the overflow props is defined for the node then we return it as the parent var _getStyleComputedProp = getStyleComputedProperty(parentNode), overflow = _getStyleComputedProp.overflow, overflowX = _getStyleComputedProp.overflowX, overflowY = _getStyleComputedProp.overflowY; if (/(auto|scroll|overlay)/.test(overflow + overflowY + overflowX)) return parentNode; return getScrollParent(parentNode); }; //# sourceMappingURL=getScrollParent.js.map