UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

33 lines (32 loc) 938 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getClosestParent = void 0; var _helpers = require("../helpers.js"); const getClosestParent = (selector, element, excludeSelf = false) => { try { const tagName = /[A-Z]/.test(selector) ? selector : null; const className = tagName ? null : selector.replace(/^\./, ''); const contains = element => { if (!element) { return null; } if (tagName) { return element?.tagName === tagName; } return element?.classList.contains(className); }; if (excludeSelf) { element = element?.parentElement; } while (element && !contains(element)) { element = element?.parentElement; } } catch (e) { (0, _helpers.warn)('getClosest: Failed to traverse DOM tree:', e); } return element; }; exports.getClosestParent = getClosestParent; //# sourceMappingURL=getClosest.js.map