Skip to content

Commit

Permalink
fix(NcModal): ignore arrow navigation when active element is outside
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
  • Loading branch information
ShGKme committed Aug 9, 2023
1 parent f6a6de1 commit d7410b7
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/components/NcModal/NcModal.vue
Expand Up @@ -616,18 +616,26 @@ export default {
}
},
// Key Handlers
handleKeydown(e) {
switch (e.keyCode) {
case 37: // left arrow
this.previous(e)
break
case 39: // right arrow
this.next(e)
break
case 27: // escape key
this.close(e)
break
/**
* @param {KeyboardEvent} event - keyboard event
*/
handleKeydown(event) {
if (event.key === 'Escape') {
return this.close(event)
}
const arrowHandlers = {
ArrowLeft: this.previous,
ArrowRight: this.next,
}
if (arrowHandlers[event.key]) {
// Ignore arrow navigation, if there is a current focus outside the modal.
// For example, when the focus is in Sidebar or NcActions's items,
// arrow navigation should not be intercept by modal slider
if (document.activeElement && !this.$el.contains(document.activeElement)) {
return
}
return arrowHandlers[event.key](event)
}
},
Expand Down Expand Up @@ -714,6 +722,9 @@ export default {
allowOutsideClick: true,
fallbackFocus: contentContainer,
trapStack: getTrapStack(),
// Esc can be used without stop in content or additionalTrapElements where it should not deacxtivate modal's focus trap.
// Focus trap is deactivated on modal close anyway.
escapeDeactivates: false,
}
// Init focus trap
Expand Down

0 comments on commit d7410b7

Please sign in to comment.