-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix useInView with hook from the initial implementation. See desc.
Copypaste and convert to js: https://github.com/cats-oss/use-intersection/blob/master/src/index.ts Original use-intersection react dependency hasn't been updated in a while: cats-oss/use-intersection#143 Can't use the react-use version because it's not triggering sometimes: streamich/react-use#2359 streamich/react-use#2376
- Loading branch information
1 parent
2c7a34f
commit f9ccaf2
Showing
5 changed files
with
104 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useRef, useState, useEffect } from 'react'; | ||
import shallowEqual from 'shallowequal'; | ||
|
||
/** | ||
* Copypaste and conversion to js of the following: | ||
* https://github.com/cats-oss/use-intersection/blob/master/src/index.ts | ||
* | ||
* Originals react dependency hasn't been updated in a while: | ||
* https://github.com/cats-oss/use-intersection/issues/143 | ||
* | ||
* Can't use the react-use version because it's not triggering sometimes: | ||
* https://github.com/streamich/react-use/issues/2359 | ||
* https://github.com/streamich/react-use/issues/2376 | ||
*/ | ||
export const useIntersection = (target, options = {}, callback) => { | ||
const { defaultIntersecting, once, ...opts } = options; | ||
const optsRef = useRef(opts); | ||
const [intersecting, setIntersecting] = useState( | ||
defaultIntersecting === true, | ||
); | ||
const intersectedRef = useRef(false); | ||
|
||
useEffect(() => { | ||
if (!shallowEqual(optsRef.current, opts)) { | ||
optsRef.current = opts; | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
if (target == null) { | ||
return; | ||
} | ||
|
||
const element = target instanceof Element ? target : target.current; | ||
if (element == null) { | ||
return; | ||
} | ||
|
||
if (once && intersectedRef.current) { | ||
return; | ||
} | ||
|
||
const observer = new IntersectionObserver( | ||
entries => { | ||
const entry = entries[entries.length - 1]; | ||
setIntersecting(entry.isIntersecting); | ||
|
||
if (callback != null) { | ||
callback(entry); | ||
} | ||
|
||
if (entry.isIntersecting) { | ||
intersectedRef.current = true; | ||
} | ||
|
||
if (once && entry.isIntersecting && element != null) { | ||
observer.unobserve(element); | ||
} | ||
}, | ||
{ | ||
...optsRef.current, | ||
root: | ||
optsRef.current.root != null ? optsRef.current.root.current : null, | ||
}, | ||
); | ||
|
||
observer.observe(element); | ||
|
||
return () => { | ||
if (once && intersectedRef.current) { | ||
return; | ||
} | ||
|
||
if (element != null) { | ||
observer.unobserve(element); | ||
} | ||
}; | ||
}, [optsRef.current, target]); | ||
|
||
return intersecting; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters