2021-01-06 23:36:31 +01:00
|
|
|
/**
|
|
|
|
* Calls the close callback when a click happened outside of the rootElement.
|
|
|
|
*
|
|
|
|
* @param event The "click" event object.
|
|
|
|
* @param rootElement
|
|
|
|
* @param closeCallback A closure function to call when the click event happened outside of the rootElement.
|
|
|
|
*/
|
|
|
|
export const closeWhenClickedOutside = (event, rootElement, closeCallback) => {
|
|
|
|
// We walk up the tree to see if any parent of the clicked element is the root element.
|
|
|
|
// If it is not, we call the close callback. We're doing all this hassle to only call the
|
|
|
|
// closing callback when a click happens outside of the rootElement.
|
|
|
|
let parent = event.target.parentElement
|
|
|
|
while (parent !== rootElement) {
|
2021-01-17 11:36:57 +01:00
|
|
|
if (parent === null || parent.parentElement === null) {
|
2021-01-06 23:36:31 +01:00
|
|
|
parent = null
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = parent.parentElement
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent === rootElement) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
closeCallback()
|
|
|
|
}
|