* pull changes from docs-playground * cleanup, add callout banner * cleanup linting and test fixes * add discussion link Co-authored-by: James M. Greene <JamesMGreene@github.com>
25 lines
627 B
TypeScript
25 lines
627 B
TypeScript
import { useState, useEffect } from 'react'
|
|
|
|
// returns scroll position
|
|
export function useWindowScroll(): number {
|
|
const [scrollPosition, setScrollPosition] = useState(
|
|
typeof window !== 'undefined' ? window.scrollY : 0
|
|
)
|
|
|
|
useEffect(() => {
|
|
const setScollPositionCallback = () => setScrollPosition(window.scrollY)
|
|
|
|
if (typeof window !== 'undefined') {
|
|
window.addEventListener('scroll', setScollPositionCallback)
|
|
}
|
|
|
|
return () => {
|
|
if (typeof window !== 'undefined') {
|
|
window.removeEventListener('scroll', setScollPositionCallback)
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
return scrollPosition
|
|
}
|