From 68e679bb55683a7c3bac915874b3fde82b50e170 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 12 Apr 2024 14:26:41 +0100 Subject: [PATCH] Update click_outside to be more robust --- packages/bbui/src/Actions/click_outside.js | 40 ++++++++++++---------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/bbui/src/Actions/click_outside.js b/packages/bbui/src/Actions/click_outside.js index 36af1398b1..76d015bee6 100644 --- a/packages/bbui/src/Actions/click_outside.js +++ b/packages/bbui/src/Actions/click_outside.js @@ -1,8 +1,13 @@ const ignoredClasses = [ - ".flatpickr-calendar", - ".spectrum-Calendar", - ".spectrum-Popover", ".download-js-link", + ".flatpickr-calendar", + ".spectrum-Menu", + ".date-time-popover", +] +const conditionallyIgnoredClasses = [ + ".spectrum-Underlay", + ".drawer-wrapper", + ".spectrum-Popover", ] let clickHandlers = [] @@ -22,26 +27,23 @@ const handleClick = event => { // Process handlers 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)) { return } - // Ignore clicks for modals, unless the handler is registered from a modal - const sourceInModal = handler.anchor.closest(".spectrum-Underlay") != null - const clickInModal = event.target.closest(".spectrum-Underlay") != null - if (clickInModal && !sourceInModal) { - 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 + // Ignore clicks for certain classes unless we're nested inside them + for (let className of conditionallyIgnoredClasses) { + const sourceInside = handler.anchor.closest(className) != null + const clickInside = event.target.closest(className) != null + if (clickInside && !sourceInside) { + return + } } handler.callback?.(event)