darkroomengineering / lenis

How smooth scroll should be
https://lenis.darkroom.engineering
MIT License
9.13k stars 388 forks source link

Next.js and Lenis are not scrolling to the top of the page when I navigate to a new one #375

Closed Liam-hi closed 3 months ago

Liam-hi commented 3 months ago

Describe the bug

When the lenis-scrolling lenis-smooth classes are active and I click on a link, the new page does not start at the top, creating a bad user experience. When these classes are removed from the HTML element, the navigation works as expected

Secondly, googling this, this seems a very common problem.

To Reproduce

I used Stackblitz to create a minimal demo. I haven't added unnecessary CSS or other elements that might interfere.

To recreate the problem, click on a link quickly while the lenis-scrolling lenis-smooth class is active.

But will a user click on a link this fast in a real project? From a user experience perspective, yes. Imagine a user clicking on an image while scrolling. This creates a bad experience for the user.

Minimal demo

clementroche commented 3 months ago

That's because your lenis instance is shared between pages since it's created between in layout.js, two solutions: