Skip to content

Instantly share code, notes, and snippets.

@ncesar
Last active February 26, 2020 13:07
Show Gist options
  • Save ncesar/0c073ed9bbda710b760d2afafb588583 to your computer and use it in GitHub Desktop.
Save ncesar/0c073ed9bbda710b760d2afafb588583 to your computer and use it in GitHub Desktop.
Check if a React component is out the viewport
const checkIfComponentIsOutViewport = () => {
console.log('executed');
if (componentRef.current) {
const componentCurrentPosition = componentRef.current.getBoundingClientRect();
if (componentCurrentPosition.top < 0) {
console.log('top is out');
}
if (
componentCurrentPosition.bottom >
(window.innerHeight || document.documentElement.clientHeight)
) {
console.log('bottom is out');
}
if (componentCurrentPosition.left < 0) {
// Left side is out of viewoprt
}
if (componentCurrentPosition.right > (window.innerWidth || document.documentElement.clientWidth)) {
// Right side is out of viewport
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment