Skip to content

Instantly share code, notes, and snippets.

@dmail
Created June 20, 2018 08:44
Show Gist options
  • Select an option

  • Save dmail/be538e5aa47c90000876d76c92cb139a to your computer and use it in GitHub Desktop.

Select an option

Save dmail/be538e5aa47c90000876d76c92cb139a to your computer and use it in GitHub Desktop.
export const listenActiveElement = (element, listener) => {
let { activeElement } = document
const check = (event) => {
const previousActiveElement = activeElement
activeElement = document.activeElement
if (previousActiveElement !== activeElement) {
listener({ previousActiveElement, activeElement, event })
}
}
const onfocus = (event) => {
check(event)
}
const onblur = (event) => {
check(event)
}
element.addEventListener('focus', onfocus, true)
element.addEventListener('blur', onblur, true)
return () => {
element.removeEventListener('focus', onfocus, true)
element.removeEventListener('blur', onblur, true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment