helpers/closest.js

16 lines
402 B
JavaScript
Raw Normal View History

2022-03-11 23:42:26 +01:00
export const closest = (node, selector, pierce) => {
const matches = node.matches || node.msMatchesSelector || node.oMatchesSelector;
while (node) {
if (matches.call(node, selector)) {
return node;
}
if (pierce && !node.parentElement) {
node = node.getRootNode();
if (node) {
node = node.host;
}
} else {
node = node.parentElement;
}
}
};