Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
Last active April 26, 2022 07:16
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 rhysburnie/2890ac3d127416026b532725381af5a8 to your computer and use it in GitHub Desktop.
Save rhysburnie/2890ac3d127416026b532725381af5a8 to your computer and use it in GitHub Desktop.
Simple document.activeElement observation
export default function ActiveElementObserver (handler = function () {}) {
let prev;
let raf;
this.observe = _ => {
cancelAnimationFrame(raf);
raf = undefined;
if (document.activeElement !== prev) {
handler({ activeElement: document.activeElement, prev });
prev = document.activeElement;
}
if (raf === false)
return;
raf = requestAnimationFrame(this.observe);
};
this.disconnect = _ => {
cancelAnimationFrame(raf);
raf = false;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment