Skip to content

Instantly share code, notes, and snippets.

@tylerpaige
Created June 15, 2022 19:52
Show Gist options
  • Save tylerpaige/290ab5f55706b7e0e571e82585868641 to your computer and use it in GitHub Desktop.
Save tylerpaige/290ab5f55706b7e0e571e82585868641 to your computer and use it in GitHub Desktop.
Focus events that tell you which way the user is heading
// 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