diff --git a/src/layouts/PrimaryPageLayout/index.tsx b/src/layouts/PrimaryPageLayout/index.tsx index 6b9a9b2..bf34a44 100644 --- a/src/layouts/PrimaryPageLayout/index.tsx +++ b/src/layouts/PrimaryPageLayout/index.tsx @@ -45,20 +45,26 @@ const PrimaryPageLayout = forwardRef( ) useEffect(() => { - if (isSmallScreen) { - if (smallScreenScrollAreaRef.current?.checkVisibility()) { - window.scrollTo({ top: smallScreenLastScrollTopRef.current, behavior: 'instant' }) - } - const handleScroll = () => { - if (smallScreenScrollAreaRef.current?.checkVisibility()) { - smallScreenLastScrollTopRef.current = window.scrollY - } - } - window.addEventListener('scroll', handleScroll) - return () => { - window.removeEventListener('scroll', handleScroll) + if (!isSmallScreen) return + + const isVisible = () => { + return smallScreenScrollAreaRef.current?.checkVisibility + ? smallScreenScrollAreaRef.current?.checkVisibility() + : false + } + + if (isVisible()) { + window.scrollTo({ top: smallScreenLastScrollTopRef.current, behavior: 'instant' }) + } + const handleScroll = () => { + if (isVisible()) { + smallScreenLastScrollTopRef.current = window.scrollY } } + window.addEventListener('scroll', handleScroll) + return () => { + window.removeEventListener('scroll', handleScroll) + } }, [current, isSmallScreen, display]) if (isSmallScreen) {