Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Hide popover on click outside (#2226)

Olga Bulat2114e826b5e6
diff --git a/frontend/src/composables/use-dialog-content.ts b/frontend/src/composables/use-dialog-content.ts
index c48ef641a..1b5e75027 100644
--- a/frontend/src/composables/use-dialog-content.ts
+++ b/frontend/src/composables/use-dialog-content.ts
@@ -58,6 +58,7 @@ export function useDialogContent({
autoFocusOnShowRef,
trapFocusRef,
+ hideOnClickOutsideRef,
})
useFocusOnHide({
dialogRef,
diff --git a/frontend/src/composables/use-focus-on-show.ts b/frontend/src/composables/use-focus-on-show.ts
index e8c66323a..c7e13f4d1 100644
--- a/frontend/src/composables/use-focus-on-show.ts
+++ b/frontend/src/composables/use-focus-on-show.ts
@@ -18,6 +18,7 @@ type Props = {
visibleRef: Ref<boolean>
autoFocusOnShowRef: Ref<boolean>
trapFocusRef: Ref<boolean>
+ hideOnClickOutsideRef: Ref<boolean>
initialFocusElementRef: Ref<HTMLElement | null>
}
@@ -29,6 +30,7 @@ export const useFocusOnShow = ({
visibleRef,
autoFocusOnShowRef,
trapFocusRef,
+ hideOnClickOutsideRef,
initialFocusElementRef,
}: Props) => {
let activateFocusTrap = () => {
@@ -48,7 +50,9 @@ export const useFocusOnShow = ({
// We already do that in a more flexible, adaptive way in our Dialog composables.
initialFocus: false,
// if set to true, focus-trap prevents the default for the keyboard event, and we cannot handle it in our composables.
- escapeDeactivates: false, // Even though we pass `initialFocus` as `false` above, `focus-trap` still
+ escapeDeactivates: false,
+ clickOutsideDeactivates: () => hideOnClickOutsideRef.value,
+ // Even though we pass `initialFocus` as `false` above, `focus-trap` still
// checks if the container has at least one tabbable element. Because it sometimes
// doesn't play nicely with Vue's rendering life-cycle, we need to get it a
// dynamic way to retrieve the fallback node.