Last active
December 10, 2019 13:55
-
-
Save acutmore/6502c28194468716f70b0d6934d5113b to your computer and use it in GitHub Desktop.
Tracking DOM focus
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
function updateFocus(elm) { | |
const focusedClass = 'focused'; | |
const currentlyFocused = document.querySelector('.' + focusedClass); | |
if (currentlyFocused != null) { | |
currentlyFocused.classList.remove(focusedClass); | |
} | |
if (elm) { | |
elm.classList.add(focusedClass); | |
} | |
} | |
document.body.addEventListener('focusin', e => { | |
updateFocus(e.target); | |
}); | |
document.body.addEventListener('focusout', e => { | |
if (document.activeElement === document.body) { | |
updateFocus(null); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment