1
0
Fork 0
mirror of synced 2024-09-08 21:51:58 +12:00

Update click outside handler to handle modals properly

This commit is contained in:
Andrew Kingston 2023-01-12 15:39:46 +00:00
parent b21f0b3925
commit 2477f4f40b

View file

@ -5,7 +5,7 @@ let clickHandlers = []
* Handle a body click event
*/
const handleClick = event => {
// Ignore click if needed
// Ignore click if this is an ignored class
for (let className of ignoredClasses) {
if (event.target.closest(className)) {
return
@ -14,9 +14,18 @@ const handleClick = event => {
// Process handlers
clickHandlers.forEach(handler => {
if (!handler.element.contains(event.target)) {
handler.callback?.(event)
if (handler.element.contains(event.target)) {
return
}
// Ignore clicks for modals, unless the handler is registered from a modal
const sourceInModal = handler.element.closest(".spectrum-Modal") != null
const clickInModal = event.target.closest(".spectrum-Modal") != null
if (clickInModal && !sourceInModal) {
return
}
handler.callback?.(event)
})
}
document.documentElement.addEventListener("click", handleClick, true)