Update click outside handler to handle modals properly
This commit is contained in:
parent
cf57a511f4
commit
2ebfdc91bd
|
@ -5,7 +5,7 @@ let clickHandlers = []
|
||||||
* Handle a body click event
|
* Handle a body click event
|
||||||
*/
|
*/
|
||||||
const handleClick = event => {
|
const handleClick = event => {
|
||||||
// Ignore click if needed
|
// Ignore click if this is an ignored class
|
||||||
for (let className of ignoredClasses) {
|
for (let className of ignoredClasses) {
|
||||||
if (event.target.closest(className)) {
|
if (event.target.closest(className)) {
|
||||||
return
|
return
|
||||||
|
@ -14,9 +14,18 @@ const handleClick = event => {
|
||||||
|
|
||||||
// Process handlers
|
// Process handlers
|
||||||
clickHandlers.forEach(handler => {
|
clickHandlers.forEach(handler => {
|
||||||
if (!handler.element.contains(event.target)) {
|
if (handler.element.contains(event.target)) {
|
||||||
handler.callback?.(event)
|
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)
|
document.documentElement.addEventListener("click", handleClick, true)
|
||||||
|
|
Loading…
Reference in New Issue