@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
26 lines • 768 B
JavaScript
import { warn } from "../helpers.js";
export 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) {
warn('getClosest: Failed to traverse DOM tree:', e);
}
return element;
};
//# sourceMappingURL=getClosest.js.map