Created
January 15, 2019 11:33
-
-
Save kevinmeyvaert/d411dd5b8c07cc028b9a9ade24275c9d to your computer and use it in GitHub Desktop.
Scroll node to center React Hook
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
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