Skip to content

Instantly share code, notes, and snippets.

@Lokua
Created February 5, 2017 19:18
Show Gist options
  • Save Lokua/ecee73d92b780714cd194d77b31b10fd to your computer and use it in GitHub Desktop.
Save Lokua/ecee73d92b780714cd194d77b31b10fd to your computer and use it in GitHub Desktop.
Detect if DOM event happened within a bounding box. Useful for outside click detection
export default function isEventWithin(e, element) {
const rect = element.getBoundingClientRect()
return (
rect.top <= e.clientY &&
e.clientY <= rect.top + rect.height &&
rect.left <= e.clientX &&
e.clientX <= rect.left + rect.width
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment