More search functionality

This commit is contained in:
Pieter Vander Vennet 2024-08-22 22:50:37 +02:00
parent 5d0de8520b
commit 1c46a65c84
25 changed files with 962 additions and 846 deletions

View file

@ -0,0 +1,21 @@
export function focusWithArrows(htmlElement: HTMLDivElement, classname: string) {
const destroy: () => void = undefined
htmlElement.addEventListener("keyup", e => {
const currentElement = document.activeElement
const canBeSelected = <HTMLElement[]> Array.from(htmlElement.getElementsByClassName(classname))
const i = canBeSelected.findIndex(el => el.contains(currentElement) || el === currentElement)
const l = canBeSelected.length
if (e.key === "ArrowDown") {
canBeSelected.at((i + 1) % l).focus()
return
}
if (e.key === "ArrowUp") {
canBeSelected.at((i - 1) ).focus()
return
}
})
return { destroy }
}