Skip to content

Instantly share code, notes, and snippets.

@codler
Last active May 25, 2023 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codler/7237990baef87ef00b9e1f7989c24ee5 to your computer and use it in GitHub Desktop.
Save codler/7237990baef87ef00b9e1f7989c24ee5 to your computer and use it in GitHub Desktop.
React hook check if element is in viewport
import { useState, useEffect, RefObject, useCallback } from "react";
function isElementInViewport(el: Element) {
var rect = el.getBoundingClientRect();
return (
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
);
}
export function useInViewport(ref: RefObject<Element>) {
const [isVisible, setIsVisible] = useState(true);
const update = useCallback(() => {
if (ref.current) {
setIsVisible(isElementInViewport(ref.current));
}
}, [ref]);
useEffect(() => {
["scroll", "load", "DOMContentLoaded", "resize", "click"].forEach(type => {
window.addEventListener(type, update);
});
return () => {
["scroll", "load", "DOMContentLoaded", "resize", "click"].forEach(type => {
window.removeEventListener(type, update);
});
};
}, [update]);
return { isVisible, update };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment