Skip to content

Commit

Permalink
add focus trapp
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpertsch authored and skjnldsv committed Mar 30, 2022
1 parent 4da67de commit 79a6558
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/components/EmojiPicker/EmojiPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,42 @@ export default {
const picker = this.$refs.picker
const input = picker.$refs.search.$el.querySelector('input')
// add focus trap in modal
picker.$el.addEventListener('keydown', this.checkKeyEvent)
if (input) {
input.focus()
}
},
checkKeyEvent(event) {
const picker = this.$refs.picker
const focusableList = picker.$el.querySelectorAll(
'button, input'
)
// escape early if only 1 or no elements to focus
if (focusableList.length < 2 && event.key === 'Tab') {
event.preventDefault()
return
}
const last = focusableList.length - 1
if (
event.key === 'Tab'
&& event.shiftKey === false
&& event.target === focusableList[last]
) {
event.preventDefault()
focusableList[0].focus()
} else if (
event.key === 'Tab'
&& event.shiftKey === true
&& event.target === focusableList[0]
) {
event.preventDefault()
focusableList[last].focus()
}
},
},
}
</script>
Expand Down

0 comments on commit 79a6558

Please sign in to comment.