Skip to content

Instantly share code, notes, and snippets.

@sampotts
Created September 25, 2017 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampotts/707ed00f59c316e1e9872254db8e1d2b to your computer and use it in GitHub Desktop.
Save sampotts/707ed00f59c316e1e9872254db8e1d2b to your computer and use it in GitHub Desktop.
// ==========================================================================
// tab-focus.js
// Detect keyboard tabbing
// ==========================================================================
(function() {
var className = "tab-focus";
// Remove class on blur
document.addEventListener("focusout", function(event) {
event.target.classList.remove(className);
});
// Add classname to tabbed elements
document.addEventListener("keydown", function(event) {
if (event.keyCode !== 9) {
return;
}
// Delay the adding of classname until the focus has changed
// This event fires before the focusin event
window.setTimeout(function() {
document.activeElement.classList.add(className);
}, 0);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment