mirror of
https://github.com/He4eT/tabswitcher.git
synced 2026-05-05 01:17:23 +00:00
pages/search: focus keyboard control
This commit is contained in:
parent
b7de8393a7
commit
eb160fe223
1 changed files with 30 additions and 0 deletions
|
|
@ -1,7 +1,37 @@
|
|||
const focusButtonsWithArrows = (e) => {
|
||||
if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
|
||||
e.preventDefault()
|
||||
|
||||
const buttons = document.querySelectorAll('#searchResults > button')
|
||||
if (buttons.length === 0) { return }
|
||||
|
||||
const currentIndex = [...buttons].indexOf(document.activeElement)
|
||||
const newIndex = currentIndex === -1
|
||||
? ({
|
||||
'ArrowDown': 0,
|
||||
'ArrowUp': buttons.length - 1,
|
||||
}[e.key])
|
||||
: (({
|
||||
'ArrowDown': 1,
|
||||
'ArrowUp': -1,
|
||||
}[e.key]) + currentIndex + buttons.length) % buttons.length
|
||||
|
||||
buttons[newIndex].focus()
|
||||
}
|
||||
}
|
||||
|
||||
export const attachInputHandlers = (store) => {
|
||||
const searchBox = document.getElementById('searchbox')
|
||||
const actionbox = document.getElementById('actionbox')
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
focusButtonsWithArrows(e)
|
||||
if (e.key === 'Escape') {
|
||||
searchBox.focus()
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
searchBox.addEventListener('input', (e) => {
|
||||
store.actions.updateQuery(e.target.value)
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue