| 85 | } |
| 86 | |
| 87 | const handleIntersection: IntersectionObserverCallback = entries => { |
| 88 | entries.forEach(entry => { |
| 89 | const onLeave = activeIntersections.get(entry.target) |
| 90 | |
| 91 | if (entry.isIntersecting === Boolean(onLeave)) { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | if (entry.isIntersecting) { |
| 96 | const newOnLeave = onEnter() |
| 97 | if (is.fun(newOnLeave)) { |
| 98 | activeIntersections.set(entry.target, newOnLeave) |
| 99 | } else { |
| 100 | observer.unobserve(entry.target) |
| 101 | } |
| 102 | } else if (onLeave) { |
| 103 | onLeave() |
| 104 | activeIntersections.delete(entry.target) |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | const observer = new IntersectionObserver(handleIntersection, { |
| 110 | root: (root && root.current) || undefined, |