pages/search: focus searchbox in edgecases

This commit is contained in:
He4eT 2024-01-21 00:54:29 +01:00
commit 3f644ee959

View file

@ -1,4 +1,4 @@
const focusButtonsWithArrows = (e) => {
const focusButtonsWithArrows = (e, abortCallback) => {
if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
e.preventDefault()
@ -16,8 +16,17 @@ const focusButtonsWithArrows = (e) => {
'ArrowUp': -1,
}[e.key]) + currentIndex + buttons.length) % buttons.length
const shouldAbort = [
e.key === 'ArrowUp' && currentIndex === 0,
e.key === 'ArrowDown' && currentIndex === buttons.length - 1,
].some(Boolean)
if (shouldAbort) {
abortCallback()
} else {
buttons[newIndex].focus()
}
}
}
export const attachInputHandlers = (store) => {
@ -25,7 +34,10 @@ export const attachInputHandlers = (store) => {
const actionbox = document.getElementById('actionbox')
document.addEventListener('keydown', (e) => {
focusButtonsWithArrows(e)
focusButtonsWithArrows(e, () => {
searchBox.focus()
})
if (e.key === 'Escape') {
searchBox.focus()
}