Skip to content

Commit

Permalink
Merge pull request #5064 from nextcloud-libraries/enh/a11y/header-men…
Browse files Browse the repository at this point in the history
…u-focusout

enh(NcHeaderMenu): Close navigation header menu on focusout
  • Loading branch information
susnux committed Jan 14, 2024
2 parents f82f7b1 + 4e24c99 commit 0b9f464
Showing 1 changed file with 22 additions and 2 deletions.
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

0 comments on commit 0b9f464

Please sign in to comment.