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

window does not scroll on top when switching routes #1099

Open
squarfed opened this issue Mar 19, 2020 · 9 comments
Open

window does not scroll on top when switching routes #1099

squarfed opened this issue Mar 19, 2020 · 9 comments
Labels
A-yew-router Area: The yew-router crate feature-request A feature request

Comments

@squarfed
Copy link

I am not sure if it is on purpose, but right now when routes are switched the scrollbar is not reset to the (0,0) position. What is a suitable workaround?

@jstarry jstarry transferred this issue from yewstack/yew_router Apr 20, 2020
@jstarry jstarry added bug A-yew-router Area: The yew-router crate labels Apr 20, 2020
@hgzimmerman
Copy link
Member

This isn't a concern with the router itself.

The probable solution would be to have a Scrollable component that you reset to the top when you detect a route change. I think that such a component would be a welcome addition to the router project should someone want to contribute one.

@jstarry jstarry added question and removed bug labels Apr 23, 2020
@siku2
Copy link
Member

siku2 commented May 18, 2021

@hamza1311 should we consider making this an option with the new user router? I'm thinking we could add a prop scroll_to_top: bool that does exactly this.

@hamza1311
Copy link
Member

@hamza1311 should we consider making this an option with the new user? I'm thinking we could add a prop scroll_to_top: bool that does exactly this.

"new user"? I think we should make this the default with an option to opt out (for cases like Discord, where the URL defines which node should be in view). I wonder how other routers (angular/vue/react router) do this

@siku2
Copy link
Member

siku2 commented May 18, 2021

"new user"? I think we should make this the default with an option to opt out (for cases like Discord, where the URL defines which node should be in view). I wonder how other routers (angular/vue/react router) do this

Sorry, meant to write new router but got distracted and my fingers decided to type something on their own :D
Anyway, I'm not particular over whether this is the default or not (though I'm tending toward yes as well). It seems like we both agree that this should be a feature though.

@siku2 siku2 added feature-request A feature request and removed question labels May 18, 2021
@hamza1311
Copy link
Member

It seems like we both agree that this should be a feature though.

Yes. That's what causes the router example to stay in weird position when switching routes, requiring manual scroll-up

@Enigo
Copy link
Contributor

Enigo commented May 29, 2023

I know it is been a while, but the issue is still there.
React offers this sorta functionality with Scroll Restoration
I briefly skimmed thru their code, and basically they save scroll y for each page in a stack and restore it with window.scrollTo, defaulting to window.scrollTo(0, 0); if a page wasn't visited before.

Prob for starters a simple reset with window.scrollTo(0, 0); on navigation change would suffice, but in the current yew-router implementation I only see one window reference.
Maybe if you could point out where it would be the most suitable to be added I could implement it

@Enigo
Copy link
Contributor

Enigo commented Jun 4, 2023

@hamza1311 @futursolo
any feedback on it ☝️ ?

@futursolo
Copy link
Member

I think you can replicate this behaviour with the following code.

let location = use_location();

use_effect_with_deps(|_| {
    window.scroll_to(ScrollToOptions::new().top(0));
}, location);

You can extend the above by storing the data in location.state() if you wish to restore the scroll location.

@Enigo
Copy link
Contributor

Enigo commented Aug 6, 2023

I ended up with the following implementation:

Cargo.toml

web-sys = { version = "0.3.63", features = ["Window", "ScrollToOptions", "ScrollBehavior"] }

helper function

pub fn scroll_to_top() {
    let window = web_sys::window();
    if window.is_some() {
        window.unwrap().scroll_to_with_scroll_to_options(web_sys::ScrollToOptions::new().top(0.0).behavior(web_sys::ScrollBehavior::Instant));
    } else {
        warn!("Window is not found!")
    }
}

calling it like

use_effect_with_deps(
    |_| {
        navigation_utils::scroll_to_top();
}, ());

The question is - do you think this kind of functionality should reside in the the router code itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew-router Area: The yew-router crate feature-request A feature request
Projects
None yet
Development

No branches or pull requests

7 participants