Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restoreScrollState true sends me to the very top of the page #282

Open
jonathanstegall opened this issue Oct 20, 2022 · 4 comments
Open

Comments

@jonathanstegall
Copy link

In my App.svelte, I have <Router {routes}/>, and when I navigate (with push or with use:link), the application preserves a scroll position that may not exist. For example, if I'm on a very long page and I click a link that goes to a very short page, the browser position will be way below the content.

When I try using <Router {routes} restoreScrollState={true}, regardless of whether I click back or click push or use:link, the application sends me to the very top of the browser window. Because in this case it's far above <Router> (header elements, navigation elements, etc.) the browser window is far away from the content it was viewing.

Are there any settings I can use for restoreScrollState, or ways to extend what it's doing? Ideally, I'd love it if the application could remember the previous scroll position, if applicable, and otherwise if it could just scroll to where <Router> starts.

@jonathanstegall
Copy link
Author

I figured out that on the component where the data is ultimately rendered (it didn't work on App.svelte), I can add this:

import { onMount } from 'svelte';
onMount(async () => {
    const el = document.querySelector('.my-selector');
    if (!el) return;
    el.scrollIntoView({
        behavior: 'smooth'
    });
});

@ItalyPaleAle
Copy link
Owner

ItalyPaleAle commented Oct 29, 2022

The above would work although directly accessing the DOM is considered anti-pattern in Svelte.

In your case, it's hard to be able to answer without seeing some code. It could be due to the fact that the content may be dynamically generated and loaded inside an onMount?

PS: onMount is not necessary if you're not doing SSR (and if you're using this router, I can probably guess you aren't!)

@phocks
Copy link

phocks commented Mar 20, 2024

I'm having the same. No matter what it simply sends me to the top of the page. I'll see if I can find the mechanism that remembers the scroll position in the code. I'm using wrap async components, so maybe that has something to do with it?

@phocks
Copy link

phocks commented Mar 20, 2024

Oh OK. I just found this. Looks like I mistook the expected behaviour of restoreScrollState.

/**
 * If set to true, the router will restore scroll positions on back navigation
 * and scroll to top on forward navigation.
 */
export let restoreScrollState = false

I was expecting the scroll state to be remembered on page reload and if I forward navigated to a page, to remember where I last was at, the last time I was on that page.

I'll continue to add my own mechanism for remembering scroll position, using localStorage or something. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants