Closed kwooshung closed 3 weeks ago
Good day @kwooshung
I believe the problem is not that OverlayScrollbars
doesnt support this behavior, rather than you are replacing the body
scroll container with your own.
In the example you posted the DOM should look something like this:
<html>
<head>
</head>
<body>
<OverlayScrollbarsComponent>
<!-- more content -->
</OverlayScrollbarsComponent>
</body>
</html>
The OverlayScrollbarsComponent
is now essentially taking up all the space and acts now as the scroll container for the entire page. The problem here is that with this setup you essentially replaced the original scroll container. With scroll: false
Next.js
will now try to reset the scroll position of the original scroll container every time you navigate. This will obviously have no effect, because your new scroll container is the OverlayScrollbarsComponent
component.
To fix this issue you have to append OverlayScrollbars
to the original scroll container (which is the body
element).
This is possible in two ways:
Layout
file you can initialize the OverlayScrollbarsComponent
like this:<html lang="en" data-overlayscrollbars-initialize="">
<OverlayScrollbarsComponent element="body">
{/** your content */}
</OverlayScrollbarsComponent>
</html>
useOverlayScrollbars
hook in your root Layout
like this:const Layout = () => {
const [initialize] = useOverlayScrollbars();
useEffect(() => {
initialize(document.body);
}, [initialize]);
return (
<html lang="en" data-overlayscrollbars-initialize="">
<body data-overlayscrollbars-initialize="">
{/** your content */}
</body>
</html>
);
}
I hope this helps and solves your problem
I've created an small example here: https://stackblitz.com/edit/stackblitz-starters-qfqhsa?file=app%2Flayout.tsx
Hello,
I'm experiencing an issue with the
OverlayScrollbars
library when used in a Next.js application. Specifically, theOverlayScrollbars
component does not support thescroll: false
configuration for scroll restoration as outlined in the Next.js documentation.Steps to Reproduce
OverlayScrollbars
integrated.Demo
Expected Behavior:
When navigating between pages, the scroll position should be reset to the top of the page, as specified by the scroll: false configuration.
Actual Behavior:
The scroll position is not reset, and the previous scroll position is retained when navigating between pages.