YesPlayMusic/packages/web/components/ScrollRestoration.tsx

21 lines
566 B
TypeScript
Raw Normal View History

2022-06-25 13:47:07 +08:00
import { useLayoutEffect } from 'react'
2022-07-11 11:06:41 +08:00
import scrollPositions from '@/web/states/scrollPositions'
2022-06-25 13:47:07 +08:00
import { throttle } from 'lodash-es'
const ScrollRestoration = () => {
useLayoutEffect(() => {
const main = document.querySelector('main')
const handleScroll = throttle(() => {
scrollPositions.set(window.location.pathname, main?.scrollTop ?? 0)
2022-08-03 23:48:39 +08:00
}, 200)
2022-06-25 13:47:07 +08:00
main?.addEventListener('scroll', handleScroll)
return () => {
main?.removeEventListener('scroll', handleScroll)
}
}, [])
return <></>
}
export default ScrollRestoration