Last active
February 22, 2022 12:35
-
-
Save tadeaspetak/83117b16ccca0856b09e73eb9b61fe55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const nextFocusable = (elements: HTMLElement[], forward = true) => { | |
const currentIndex = elements.findIndex((e) => e === document.activeElement); | |
let nextIndex = 0; | |
if (currentIndex > -1) { | |
if (forward) { | |
nextIndex = currentIndex < elements.length - 1 ? currentIndex + 1 : 0; | |
} else { | |
nextIndex = currentIndex > 0 ? currentIndex - 1 : elements.length - 1; | |
} | |
} | |
elements[nextIndex]?.focus(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment