Created
June 15, 2022 19:52
-
-
Save tylerpaige/290ab5f55706b7e0e571e82585868641 to your computer and use it in GitHub Desktop.
Focus events that tell you which way the user is heading
This file contains hidden or 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
// Assuming `app` is some sort of event bus that can emit events... | |
document.addEventListener("keydown", (e) => { | |
if (e.key !== "Tab") { | |
return; | |
} | |
const focusDirection = e.shiftKey ? "previous" : "next"; | |
const handler = () => { | |
app.trigger(`focus:${focusDirection}`); | |
app.trigger(`focus:change`, { direction: focusDirection }); | |
document.removeEventListener("focusin", handler); | |
}; | |
document.addEventListener("focusin", handler); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment