Skip to content

Instantly share code, notes, and snippets.

@kevinmeyvaert
Created January 15, 2019 11:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmeyvaert/d411dd5b8c07cc028b9a9ade24275c9d to your computer and use it in GitHub Desktop.
Save kevinmeyvaert/d411dd5b8c07cc028b9a9ade24275c9d to your computer and use it in GitHub Desktop.
Scroll node to center React Hook
const useScrollIntoView = (condition: boolean) => {
const ref = React.useRef<HTMLDivElement | null>(null);
React.useEffect(() => {
if (condition && ref.current) {
ref.current.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'center',
});
}
}, []);
return ref;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment