Update click_outside to be more robust

This commit is contained in:
Andrew Kingston 2024-04-12 14:26:41 +01:00
parent 1ab7e4ecd3
commit 68e679bb55
1 changed files with 21 additions and 19 deletions

View File

@ -1,8 +1,13 @@
const ignoredClasses = [ const ignoredClasses = [
".flatpickr-calendar",
".spectrum-Calendar",
".spectrum-Popover",
".download-js-link", ".download-js-link",
".flatpickr-calendar",
".spectrum-Menu",
".date-time-popover",
]
const conditionallyIgnoredClasses = [
".spectrum-Underlay",
".drawer-wrapper",
".spectrum-Popover",
] ]
let clickHandlers = [] let clickHandlers = []
@ -22,26 +27,23 @@ const handleClick = event => {
// Process handlers // Process handlers
clickHandlers.forEach(handler => { clickHandlers.forEach(handler => {
// Check that we're the right kind of click event
if (handler.allowedType && event.type !== handler.allowedType) {
return
}
// Check that the click isn't inside the target
if (handler.element.contains(event.target)) { if (handler.element.contains(event.target)) {
return return
} }
// Ignore clicks for modals, unless the handler is registered from a modal // Ignore clicks for certain classes unless we're nested inside them
const sourceInModal = handler.anchor.closest(".spectrum-Underlay") != null for (let className of conditionallyIgnoredClasses) {
const clickInModal = event.target.closest(".spectrum-Underlay") != null const sourceInside = handler.anchor.closest(className) != null
if (clickInModal && !sourceInModal) { const clickInside = event.target.closest(className) != null
if (clickInside && !sourceInside) {
return return
} }
// Ignore clicks for drawers, unless the handler is registered from a drawer
const sourceInDrawer = handler.anchor.closest(".drawer-wrapper") != null
const clickInDrawer = event.target.closest(".drawer-wrapper") != null
if (clickInDrawer && !sourceInDrawer) {
return
}
if (handler.allowedType && event.type !== handler.allowedType) {
return
} }
handler.callback?.(event) handler.callback?.(event)