UNPKG

@wordpress/dom

Version:
57 lines (54 loc) 1.87 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getScrollContainer; var _getComputedStyle = _interopRequireDefault(require("./get-computed-style")); /** * Internal dependencies */ /** * Given a DOM node, finds the closest scrollable container node or the node * itself, if scrollable. * * @param {Element | null} node Node from which to start. * @param {?string} direction Direction of scrollable container to search for ('vertical', 'horizontal', 'all'). * Defaults to 'vertical'. * @return {Element | undefined} Scrollable container node, if found. */ function getScrollContainer(node, direction = 'vertical') { if (!node) { return undefined; } if (direction === 'vertical' || direction === 'all') { // Scrollable if scrollable height exceeds displayed... if (node.scrollHeight > node.clientHeight) { // ...except when overflow is defined to be hidden or visible const { overflowY } = (0, _getComputedStyle.default)(node); if (/(auto|scroll)/.test(overflowY)) { return node; } } } if (direction === 'horizontal' || direction === 'all') { // Scrollable if scrollable width exceeds displayed... if (node.scrollWidth > node.clientWidth) { // ...except when overflow is defined to be hidden or visible const { overflowX } = (0, _getComputedStyle.default)(node); if (/(auto|scroll)/.test(overflowX)) { return node; } } } if (node.ownerDocument === node.parentNode) { return node; } // Continue traversing. return getScrollContainer(/** @type {Element} */node.parentNode, direction); } //# sourceMappingURL=get-scroll-container.js.map