diff --git a/frontend/src/components/common/Navigation/CollectionsDrawer.vue b/frontend/src/components/common/Navigation/CollectionsDrawer.vue index 15e36468f..6c0ff984c 100644 --- a/frontend/src/components/common/Navigation/CollectionsDrawer.vue +++ b/frontend/src/components/common/Navigation/CollectionsDrawer.vue @@ -5,7 +5,7 @@ import CollectionListItem from "@/components/common/Collection/ListItem.vue"; import storeCollections from "@/stores/collections"; import storeNavigation from "@/stores/navigation"; import type { Events } from "@/types/emitter"; -import { useLocalStorage } from "@vueuse/core"; +import { useActiveElement, useLocalStorage } from "@vueuse/core"; import type { Emitter } from "mitt"; import { storeToRefs } from "pinia"; import { inject, onBeforeUnmount, onMounted, ref, watch, computed } from "vue"; @@ -15,6 +15,7 @@ import { useDisplay } from "vuetify"; const { t } = useI18n(); const navigationStore = storeNavigation(); const { mdAndUp, smAndDown } = useDisplay(); +const activeElement = useActiveElement(); const collectionsStore = storeCollections(); const { filteredCollections, @@ -40,16 +41,11 @@ function clear() { filterText.value = ""; } -// Ref to store the element that triggered the drawer -const triggerElement = ref(null); -// Watch for changes in the navigation drawer state -const textFieldRef = ref(); +const triggerElement = ref(undefined); watch(activeCollectionsDrawer, (isOpen) => { if (isOpen) { // Store the currently focused element before opening the drawer - triggerElement.value = document.activeElement as HTMLElement; - // Focus the text field when the drawer is opened - // textFieldRef.value?.focus(); + triggerElement.value = activeElement.value; } }); @@ -69,6 +65,12 @@ function onScroll() { } } +function onClose() { + activeCollectionsDrawer.value = false; + // Refocus the trigger element for keyboard navigation + triggerElement.value?.focus(); +} + onMounted(() => { const collectionsDrawer = document.querySelector( "#collections-drawer .v-navigation-drawer__content", @@ -82,12 +84,6 @@ onBeforeUnmount(() => { ); collectionsDrawer?.removeEventListener("scroll", onScroll); }); - -function onClose() { - activeCollectionsDrawer.value = false; - // Focus the element that triggered the drawer - triggerElement.value?.focus(); -}