export default function targetIsDescendant( element: HTMLElement, parent: HTMLElement, ) { let node: HTMLElement | null = element; while (node !== null) { // When it has an origin ID it means that is an element generated by a Portal so that // the id makes a reference to the element that is the opener and the tree should be // navigated from there. const originId = node.getAttribute && node.getAttribute('data-origin-id'); if (originId) { const portalParent = document.getElementById(originId); if (portalParent) { node = portalParent; } } if (node === parent) { return true; } node = node.parentElement; } return false; }