Skip to content

Commit

Permalink
Enhance keyboard navigation for suggestions dropdown by adding suppor…
Browse files Browse the repository at this point in the history
…t for PageUp, PageDown, Home, and End keys. This allows users to navigate through suggestions more efficiently without using the mouse.
  • Loading branch information
yairEO committed Dec 7, 2024
1 parent 1cd4459 commit 719827b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/parts/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ export default {
// selectedElm.scrollIntoView({inline: 'nearest', behavior: 'smooth'})
break;
}
case 'PageUp':
case 'PageDown': {
e.preventDefault()
const dropdownItems = this.dropdown.getAllSuggestionsRefs()
const itemsPerPage = Math.floor(this.DOM.dropdown.content.clientHeight / dropdownItems[0]?.offsetHeight) || 1
const isPageUp = e.key === 'PageUp'

if (selectedElm) {
const currentIndex = dropdownItems.indexOf(selectedElm)
const targetIndex = isPageUp
? Math.max(0, currentIndex - itemsPerPage)
: Math.min(dropdownItems.length - 1, currentIndex + itemsPerPage)
selectedElm = dropdownItems[targetIndex]
} else {
selectedElm = dropdownItems[0]
}

this.dropdown.highlightOption(selectedElm, true)
break;
}
case 'Home':
case 'End': {
e.preventDefault()
const dropdownItems = this.dropdown.getAllSuggestionsRefs()
selectedElm = dropdownItems[e.key === 'Home' ? 0 : dropdownItems.length - 1]
this.dropdown.highlightOption(selectedElm, true)
break;
}
case 'Escape' :
case 'Esc': // IE11
this.dropdown.hide();
Expand Down

0 comments on commit 719827b

Please sign in to comment.