Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(NcHeaderMenu): Close navigation header menu on focusout #5064

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/components/NcHeaderMenu/NcHeaderMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ export default {
<template>
<component :is="wrapperTag"
:id="id"
ref="headerMenu"
v-click-outside="clickOutsideConfig"
:aria-labelledby="isNav ? triggerId : null"
:class="{ 'header-menu--opened': opened }"
class="header-menu">
class="header-menu"
v-on="listeners">
<!-- Trigger -->
<button :id="isNav ? triggerId : null"
ref="trigger"
Expand Down Expand Up @@ -200,6 +202,15 @@ export default {
this.clickOutsideOptions,
]
},

listeners() {
if (this.isNav) {
return {
focusout: this.onFocusOut,
}
}
return null
},
},

watch: {
Expand Down Expand Up @@ -282,13 +293,22 @@ export default {
}
},

/**
* @param {FocusEvent} event The focus event
*/
onFocusOut(event) {
if (!this.$refs.headerMenu.contains(event.relatedTarget)) {
this.closeMenu()
}
},

/**
* Add focus trap for accessibility.
* Shall only be used when all children are mounted
* and available in the DOM. We use $nextTick for that.
*/
async useFocusTrap() {
if (this.focusTrap) {
if (this.isNav || this.focusTrap) {
return
}
// Init focus trap
Expand Down