<ScrollRestoration>
with scrolling container other than window
#9495
Replies: 15 comments 14 replies
-
Dude you are a saviour!! Plus helped me to learn about patch-package which I did not know before :) worked like a charm! |
Beta Was this translation helpful? Give feedback.
-
I've built a hook to restore scroll of individual elements: function SomeComp() {
useElementScrollRestoration("content-pane");
return (
<div id="content-pane">
{/* ... */}
</div>
)
} And then the inside of that looks a whole lot like our code inside of I'd love to add this hook. If you or someone else wants to take this on I'd love to offer a few tips on how I think I'd implement it. |
Beta Was this translation helpful? Give feedback.
-
Ran into this issue as well, would love to be able to pass |
Beta Was this translation helpful? Give feedback.
-
I have a similar situation to the one showed in the CodeSandbox demo. I'm using a big |
Beta Was this translation helpful? Give feedback.
-
My above hack still works for scrolling to top on forward navigation, but when I upgraded from 6.4.2 to 6.6.1, I noticed that scroll restore on back nav doesn't work anymore — it always scrolls to top. What's especially odd is I put in a |
Beta Was this translation helpful? Give feedback.
-
I am also hitting a practically identical issue (my app uses flexbox for static header / scrollable body / static footer) -- being able to pass a ref to ScrollRestoration sounds ideal :) |
Beta Was this translation helpful? Give feedback.
-
Using a ref seems like it would fix this
Does that work? Are there other edge cases I'm not thinking of? ("What if we want to support multiple scrolled elements side-by-side?" seems like a potential case, but my answer right now is "this change is designed to support the common use-case of having a single scrolling element, and only that case; supporting multiple elements would probably require more significant API and internal changes than adding a single optional parameter") |
Beta Was this translation helpful? Give feedback.
-
@ryanflorence, would you mind sharing your |
Beta Was this translation helpful? Give feedback.
-
Cross-posting from #10468 (comment)
I also wanted to add a specific use case we have, to hopefully get feedback and maybe inform future design decisions: We've got a scrollable list of items, and clicking one navigates to a detail page. That detail page has a "Back to List" link, where when clicked, should navigate back to the list view, restoring the scroll position to the initially clicked item. While we also want scroll restore to work on the browser's back button and have its state survive a reload, that "Back to List" link isn't just a For the record, that initial scrollable list is a component itself, with things like optional sticky headers/footers, so FWIW, we made all this work using React Router v5, although I'm not particularly proud of that implementation, TBH. The key for us with RRv5 was that the |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I am also having this issue. My problem, which has also been state above, is that |
Beta Was this translation helpful? Give feedback.
-
FWIW @kentcdodds just released this package yesterday https://github.com/epicweb-dev/restore-scroll |
Beta Was this translation helpful? Give feedback.
-
Thanks for the workaround! It's really unfortunate that ScrollRestoration doesn't support specifying an element rather than the window |
Beta Was this translation helpful? Give feedback.
-
+1 to needing resotre scroll position of scrollable divs that aren't window. |
Beta Was this translation helpful? Give feedback.
-
I also ran into this. I ended up migrating from react-router to tanstack router which supports this. Only took a few minutes and it works as you'd expect: https://tanstack.com/router/v1/docs/framework/react/migrate-from-react-router |
Beta Was this translation helpful? Give feedback.
-
I briefly discussed this with @brophdawg11 on Discord and he suggested starting a discussion here. He raised some useful concerns:
The problem
If you use CSS grid to define a layout where only one pane scrolls rather than the entire page,
<ScrollRestoration>
does not work because it only useswindow.scrollY
andwindow.scrollTo(_, _)
.react-router/packages/react-router-dom/index.tsx
Lines 1018 to 1110 in 5f3cfb7
CodeSandbox demo
Very Bad patch fix
In my app at work, I'm working around this by using patch-package to simply use
document.querySelector('#content-pane')
instead ofwindow
. This is a little scary, but it seems to work quite well.#content-pane
is in layouts so we know it will more or less always be there.More general approach
Obviously hard coding an element doesn't make sense for the real library. Here are some ways I can picture it working:
and in the library:
Calling
document.querySelector
in React is usually not a thing we want to do, so maybe it should be a ref instead. I think this would require puttingScrollRestoration
next to your scroll container so you can get the ref:In both cases I think falling back to
window
should be ok, because generally it will do nothing. Though maybe that's only true in my use case because we've made sure the whole page doesn't scroll. So maybe it's safer not to fall back, and instead just don't do anything if the ref is null or if the selector function comes back null.Beta Was this translation helpful? Give feedback.
All reactions