question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Examples] Modal dynamic route doesn't keep scroll position

See original GitHub issue

Bug report

The example from https://github.com/vercel/next.js/tree/canary/examples/with-route-as-modal doesn’t keep scroll position when modal is opened using the dynamic routing method.

Describe the bug

I’m not sure if the dynamic routing method is supposed to work like the QueryString routing where it keeps the scrolling position of the main page. I’m not able to use the QueryString method due to getStaticProps() not working as a component correctly (can’t use mongodb node driver, it errors saying dns could not be found)

The scrolling position is saved when using the QueryString method after you close the modal. But for the dynamic routing method, it unloads the main page and only shows the modal (as seen in the gif below). When the modal is exited, the main page is reloaded again and the scroll position from the previous context is lost.

To Reproduce

  1. yarn create next-app --example with-route-as-modal with-route-as-modal-app
  2. cd with-route-as-modal-app
  3. yarn dev
  4. Go to localhost:3000 in Safari
  5. Click on one of the numbers under “With Dynamic Routing, and reloads will keep the modal”
  6. Exit out of the modal by clicking on the outer area of the screen.

Expected behavior

When exiting out of the modal, it should keep the scroll position of the main page.

Screenshots

2020-09-27 17 09 00

System information

  • OS: macOS 11.0 Beta (20A5364e)
  • Browser: Safari 14.0 (16610.2.2.4)
  • Version of Next.js: v9.5.3
  • Version of Node.js: v12.18.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
jmfurycommented, Oct 16, 2020

@kyleawayan I threw up a repo where I’ve used a couple concepts that break with the original example; namely this is using layouts

Seems to be keeping the scroll as desired via scroll={false} on <Link /> when the page components via layouts are essentially all the same.

This was my hunch on how to achieve what you’re looking for and I tried to be descriptive in the Headings to illustrate the differences in functionality for query string vs. dynamic routing.

Give it a look and let me know of any questions 👍 https://github.com/jmfury/nextjs-route-as-modal-plus-scroll

3reactions
jmfurycommented, Oct 19, 2020

@kyleawayan Great to hear it’s working! Feel free to tag me on any code you can share outside this thread as needed. Happy to assist if any of the concepts are giving you any trouble in your implementation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js maintain scroll position when page Routing
when using Router.push() page scroll position doesn't move. solution code is this. import Link from 'next/link' const handleClickMore ...
Read more >
How to prevent Body from scrolling when a modal is opened ...
Given an HTML document with a modal, the task is to prevent the body element from scrolling whenever the modal is in an...
Read more >
4 Ways to Listen to Page Scrolling for Dynamic UI in Angular ft ...
First, we create a BehaviourSubject to store the scrollY position. Additionally, we create an observable from the scrollY BehaviorSubject, to ...
Read more >
Preserve scroll position in prototypes - Figma Help Center
The preserve scroll position setting lets you keep the same scroll location when you transition between screens. This applies to vertical scroll depth,...
Read more >
Create an advanced scroll lock React Hook - LogRocket Blog
In the capture above, we can see that the modal gets opened, and the scrollable content within that modal is scrolled. When we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found