UNPKG

@helpscout/hsds-react

Version:

React component library for Help Scout's Design System

71 lines (53 loc) 2.17 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.nodesHaveSameParent = nodesHaveSameParent; exports.scrollIntoView = exports.getClosestNode = exports.getClosestDocument = exports.getNodeScope = exports.isNodeElement = void 0; var _closest = _interopRequireDefault(require("./closest")); /* istanbul ignore file */ var Element = window['Element']; /** * Note: A lot of stuff here cannot be tested in JSDOM (missing measurements for props) * Tests exist for some cases but file is not included in coverage */ var isNodeElement = function isNodeElement(node) { return node && (node instanceof Element || node.nodeType === 1 || // Thanks Brett! <3 node === document); }; exports.isNodeElement = isNodeElement; var getNodeScope = function getNodeScope(nodeScope) { return nodeScope && isNodeElement(nodeScope) ? nodeScope : nodeScope === window ? window : document; }; exports.getNodeScope = getNodeScope; var getClosestDocument = function getClosestDocument(node) { return node && isNodeElement(node) ? node.ownerDocument : document; }; exports.getClosestDocument = getClosestDocument; var getClosestNode = function getClosestNode(node, selector) { if (!isNodeElement(node)) return null; if (typeof selector !== 'string') return null; return (0, _closest.default)(node, selector); }; /** * Enhanced wrapper for Node.scrollIntoViewIfNeeded() * * @param {Node} node * @returns {Node} */ exports.getClosestNode = getClosestNode; var scrollIntoView = function scrollIntoView(node) { if (!isNodeElement(node)) return; if (node['scrollIntoViewIfNeeded']) return node.scrollIntoViewIfNeeded(); if (node['scrollIntoView']) return node.scrollIntoView(); }; exports.scrollIntoView = scrollIntoView; function nodesHaveSameParent(parentSelector, node1, node2) { if (!parentSelector || !node1 || !node2) return false; //for testing if (!node1.closest) { return false; } var parent1 = node1.closest(parentSelector); var parent2 = node2.closest(parentSelector); if (!parent1 || !parent2) return false; return parent1.isEqualNode(parent2); }