Last active
June 24, 2019 09:08
-
-
Save arielsalminen/6fe155e944bc4dd2ffcddb9355224d73 to your computer and use it in GitHub Desktop.
Hide browser’s default focus outline when using mouse to click around, but show them when the user is using keyboard to browse the webpage.
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
body:not(.user-is-tabbing) *:focus { | |
outline: none; | |
} |
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
// Handle first tab to set “user-is-tabbing” class for <body> | |
function handleFirstTab(e) { | |
if (e.keyCode === 9) { | |
document.body.classList.add("user-is-tabbing") | |
window.removeEventListener("keydown", handleFirstTab) | |
window.addEventListener("mousedown", handleMouseDownOnce) | |
} | |
} | |
// Handle the user switching back to using mouse | |
function handleMouseDownOnce() { | |
document.body.classList.remove("user-is-tabbing") | |
window.removeEventListener("mousedown", handleMouseDownOnce) | |
window.addEventListener("keydown", handleFirstTab) | |
} | |
// Listen for keydown events on load | |
window.addEventListener("keydown", handleFirstTab) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will hide browser’s default focus outline when using mouse to click around, but show them when the user is using keyboard to browse the webpage.