17 lines
413 B
JavaScript
17 lines
413 B
JavaScript
|
|
||
|
/**
|
||
|
* Find nearest parent that satisfies a requirement
|
||
|
* @param {Node|Element} node
|
||
|
* @param {(el: Element) => boolean} findFn
|
||
|
* @returns {HTMLElement | null} Found element, or null if no parent matches
|
||
|
*/
|
||
|
export function findNearestParent(node, findFn) {
|
||
|
let parent = node.parentElement;
|
||
|
while (parent) {
|
||
|
if (findFn(parent)) {
|
||
|
return parent;
|
||
|
}
|
||
|
parent = parent.parentElement;
|
||
|
}
|
||
|
return null;
|
||
|
}
|