generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
40 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export useClickOutside from './useClickOutside'; | ||
export usePrevious from './usePrevious'; | ||
export useOnScreen from './useOnScreen'; |
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,27 @@ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
import React from 'react'; | ||
|
||
function useOnScreen(ref, rootMargin = '0px') { | ||
// State and setter for storing whether element is visible | ||
const [isIntersecting, setIntersecting] = React.useState(false); | ||
React.useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
([entry]) => { | ||
// Update our state when observer callback fires | ||
setIntersecting(entry.isIntersecting); | ||
}, | ||
{ | ||
rootMargin, | ||
}, | ||
); | ||
if (ref.current) { | ||
observer.observe(ref.current); | ||
} | ||
return () => { | ||
observer.unobserve(ref.current); | ||
}; | ||
}, []); // Empty array ensures that effect is only run on mount and unmount | ||
return isIntersecting; | ||
} | ||
|
||
export { useOnScreen }; |
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