Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intersectionRatio === 0 is not a safe assumption for "exit" #2

Closed
nolanlawson opened this issue Sep 14, 2017 · 1 comment
Closed

intersectionRatio === 0 is not a safe assumption for "exit" #2

nolanlawson opened this issue Sep 14, 2017 · 1 comment

Comments

@nolanlawson
Copy link

This code may lead to very subtle bugs because, per the spec, an element may still be "intersecting" even though intersectionRatio is 0. For instance, this situation is possible:

{
  isIntersecting: true,
  intersectionRatio: 0
}

This is due to how edge adjacency works, and you can read the gory details here (w3c/IntersectionObserver#222). You can repro this by scrolling such that the element perfectly overlaps the root (may be easiest to repro in Firefox currently).

My recommendation would be to change the if() statement to

if (typeof entry.isIntersecting === 'boolean' ? !entry.isIntersecting : entry.intersectionRatio === 0) {
  onExit()
}

The case where typeof entry.isIntersecting isn't a boolean covers Edge 15, which does not support isIntersecting, and where entry.intersectionRatio === 0 is actually a safe proxy for "no longer intersecting."

@yoshuawuyts
Copy link
Owner

Thanks heaps! Reworked the API, and included this lil snip. Should be a lot better now! ✨

📦 v2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants