Skip to content

Enhanced Navigation does not scroll new page to top #51338

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

Closed
1 task done
oliverw opened this issue Oct 12, 2023 · 3 comments
Closed
1 task done

Enhanced Navigation does not scroll new page to top #51338

oliverw opened this issue Oct 12, 2023 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@oliverw
Copy link

oliverw commented Oct 12, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I believe page navigation in a Blazor Web App should behave identical whether enhanced navigation is turned on or off. In RC2 the destination page will retain the scroll position of the originating page if enhanced navigation is active.

Expected Behavior

No response

Steps To Reproduce

  1. Create a Blazor Web App project using the default settings (no auth etc.)

  2. Change Components/Pages/Home.razor to this:

@page "/"

<PageTitle>Home</PageTitle>

<div style="height: 4000px">
</div>

<h1>Hello, world!</h1>

Welcome to your new app.

<NavLink class="nav-link" href="weather" style="margin-top: auto">
    <span class="bi bi-list-nested" aria-hidden="true"></span> Weather
</NavLink>
  1. Add the fragment below to Components/Pages/Weather.razor between the PageTitle component and the h1:
<div style="height: 4000px">
</div>
  1. Run the app and open /
  2. Scroll to the bottom and click the link to /weather
  3. Observe the scroll position at location /weather

Exceptions (if any)

No response

.NET Version

8.0.100-rc.2.23502.2

Anything else?

No response

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Oct 12, 2023
@javiercn
Copy link
Member

@oliverw thanks for contacting us.

The behavior is deliberate. There are other situations where what you are suggesting results in a bad experience that enhanced navigation tries to improve.

For example, when you scroll down to the bottom of a page and post a form. The browser will "reset" the scroll position, which forces the user to navigate all the way down.

The "goal" of enhanced page navigations is to make the app feel like a SPA, and in such case, the expectation is that clicking a link won't change the scroll position.

If you want to scroll to the top, you can either do it via JavaScript with window.scrollTo(0,0) after each enhancednavigation event happened or with a bit of javascript associated with the link.

@marinasundstrom
Copy link

marinasundstrom commented Oct 17, 2023

Here is the script that I use:

Blazor.addEventListener('enhancedload', () => {
    window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
});

@oliverw
Copy link
Author

oliverw commented Oct 17, 2023

@oliverw thanks for contacting us.

The behavior is deliberate. There are other situations where what you are suggesting results in a bad experience that enhanced navigation tries to improve.

For example, when you scroll down to the bottom of a page and post a form. The browser will "reset" the scroll position, which forces the user to navigate all the way down.

The "goal" of enhanced page navigations is to make the app feel like a SPA, and in such case, the expectation is that clicking a link won't change the scroll position.

If you want to scroll to the top, you can either do it via JavaScript with window.scrollTo(0,0) after each enhancednavigation event happened or with a bit of javascript associated with the link.

Allright, fair enough.

@oliverw oliverw closed this as completed Oct 17, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Nov 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

3 participants