-
-
Save dohoons/025f72362c4ca72bcff9473cba7456ea to your computer and use it in GitHub Desktop.
Scribble to fix scroll position
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
function MyApp({ Component, pageProps }) { | |
return <ScrollPositionProvider> | |
<Component {...pageProps} /> | |
</ScrollPositionProvider> | |
} | |
export default MyApp |
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
import React, { useEffect, useContext } from 'react'; | |
import Router from 'next/router'; | |
export const ScrollPositionContext = React.createContext({ | |
triggerScroll: () => null, | |
}); | |
export const useScrollPosition = () => useContext(ScrollPositionContext); | |
let routerBound = false; | |
let shouldScrollRestore = false; | |
let scrollRestored = false; | |
const cachedScrollPositions: { | |
[asPath: string]: { x: number; y: number }; | |
} = {}; | |
/** | |
* - Save the scroll position mapped to the current url on navigation | |
* - Mark shouldScrollRestore on beforePopState to trigger scrollTo only for back/next navigation | |
* - Remove scroll position when normally navigating in: Refresh, links | |
* - (Re-)trigger scrollTo after all content is loaded with triggerScroll | |
* | |
* Inspired by https://github.com/zeit/next.js/issues/3303#issuecomment-562581796 | |
*/ | |
const ScrollPositionProvider = ({ children }) => { | |
useEffect(() => { | |
if (typeof window === 'undefined') return; | |
// Commented out because I'm not sure if its necessary | |
// if (window?.history?.scrollRestoration) { | |
// window.history.scrollRestoration = 'manual'; | |
// } | |
const handleChangeStart = () => { | |
cachedScrollPositions[Router.asPath] = { | |
x: window.scrollX || window.pageXOffset, | |
y: window.scrollY || window.pageYOffset, | |
}; | |
scrollRestored = false; | |
}; | |
const handleChangeComplete = () => { | |
if (shouldScrollRestore && cachedScrollPositions[Router.asPath]) { | |
const { x, y } = cachedScrollPositions[Router.asPath]; | |
window.scrollTo(x, y); | |
shouldScrollRestore = false; | |
scrollRestored = true; | |
} else { | |
// push action일 때 스크롤 상단으로 이동하도록 수정 | |
window.scrollTo(0, 0); | |
} | |
}; | |
if (!routerBound) { | |
routerBound = true; | |
Router.events.on('routeChangeStart', handleChangeStart); | |
Router.events.on('routeChangeComplete', handleChangeComplete); | |
} | |
Router.beforePopState(() => { | |
shouldScrollRestore = true; | |
return true; | |
}); | |
// // Commented out because not sure if it works | |
// return () => { | |
// router.events.off('routeChangeStart', handleChangeStart); | |
// router.events.on('routeChangeComplete', handleChangeComplete); | |
// }; | |
}); | |
const triggerScroll = () => { | |
if (scrollRestored && cachedScrollPositions[Router.asPath]) { | |
const { x, y } = cachedScrollPositions[Router.asPath]; | |
window.scrollTo(x, y); | |
} | |
}; | |
return ( | |
<ScrollPositionContext.Provider value={{ triggerScroll }}> | |
{children} | |
</ScrollPositionContext.Provider> | |
); | |
}; | |
export default ScrollPositionProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment