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.

Feature: Sidenav or Route Scroll Service

See original GitHub issue

Bug, feature request, or proposal:

Feature Request

What is the expected behavior?

Using a common native API call to window.scrollTo(0, 0) or doing something similar on the document body, etc would allow for scrolling the page to the top after a route ends when using a sidenav, otherwise the next view starts at the same position in the page.

What is the current behavior?

Routing between views within the sidenav content maintains the same scroll position assuming the content is the same length.

What are the steps to reproduce?

Add a sidenav and try to scroll to the top of the page anywhere at anytime. Eventually, I figure out this solution and stuck it in my app.component.

this.router.events
  .filter(event => event instanceof NavigationEnd)
  .subscribe(() => {
        const contentContainer = document.querySelector('.mat-sidenav-content');
        if (contentContainer) {
          document.querySelector('.mat-sidenav-content').scroll({ top: 0, left: 0, behavior: 'smooth' });
        } else {
          this.window.scroll({ top: 0, left: 0, behavior: 'smooth' });
        }
  });

What is the use-case or motivation for changing an existing behavior?

Routing between views within the sidenav should be able to start at the top of the page, which is a common use case. Also, scrolling a browser using the native API has been around forever so it’s a bit confusing when you expect it or one of the many variations of scrolling to work and they don’t.

Which versions of Angular, Material, OS, browsers are affected?

Angular 4.0.2 Angular Material 2.0.0-beta.3

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:15
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

20reactions
elishateradacommented, May 11, 2017

Building on @mtpultz answer, you can keep it little simpler if your page is always going to have md sidenav, and without relying on third party library which I assume is what .scroll() is about.

this.router.events
  .filter(event => event instanceof NavigationEnd)
  .subscribe(() => {
    document.querySelector('.mat-sidenav-content').scrollTop = 0;
  });
9reactions
odahcamcommented, Oct 29, 2018

Disclaimer: I’ve post these somewhere else before, but as @Splaktar requested, here are some demos of the routing scrolling issue that I’m experiencing.

Here are two examples, the first working and the second not:

Why that happens? Because when mat-sidenav-container is used with its fullscreen attribute, it gets its own scrolling container instead of relying on the default viewport overflow behavior.

A curious fact is that the router scrolling seems to work when you play with the browser history, but not when you click the links.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sidenav - Angular Material
Overview for sidenav. Angular Material provides two sets of components designed to add collapsible side content (often navigation, though it can be any...
Read more >
Router anchorScrolling does not work with Material's mat ...
Is there a way for RouterModule to use mat-sidenav-container instead of the viewport as a scrolling container reference? See the StackBlitz ...
Read more >
Reactive Scroll Position Restoration with RxJS - Medium
The main element will be scrolled when we scroll through the pages content. If we get a scrollTop of main , we know...
Read more >
Beginner React JS Project Using Hooks & Router - YouTube
Learn how to make a React Sidebar Navigation Menu in this beginner tutorial. We will be using React Hooks, Router, and React Icons...
Read more >
Anchors Away! How to Create Smooth Scrolling in Angular
A typical UX pattern is to use anchors for quick navigation between sections with just one click without needing to scroll up and...
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