[Tabs] Automatically page scrolls to top when switching between some tabs
Bug, feature request, or proposal:
We found a bug. We had top content and a tab group below this content. And then we scrolled a little bit to bottom. We switched between some tabs. Now, page scrolled to top automatically.
It only occurs when we have top content on this page.
What is the expected behavior?
Page should keep scroll position when switching some tabs
What is the current behavior?
Page scrolls to top when switching some tabs
What are the steps to reproduce?
-Step 1: Scroll a little bit to bottom on page -Step 2: Click on Tab 3 The page will scroll to top.
Demo here: https://stackblitz.com/edit/angular-wjtrxy
What is the use-case or motivation for changing an existing behavior?
A bug
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular 5.0.3 - Material 5.0.2 - Window 10 - Typescript 2.4.2
Is there anything else we should know?
Thanks a lot!
why is there no definite solution? This issue started from 2 years ago, not closed until now
@asyahril Hey c'mon get off their back, this is open source. What do you expect from a small company with limited resources like Google? I'm just happy the issue hasn't been automatically locked yet.
I'm also having this issue. I've tried @britvik's Directive (doesn't fix for me... fromEvent doesn't appear to fire), I've also tried min-height on containing div. Works in Chrome & Edge but not Firefox.
I've made this directive based on @ludarous answer, to make it work with Angular CDK scrolling. It work on my side, but not the smoothest option.
import { AfterViewInit, Directive, OnInit, OnDestroy } from '@angular/core';
import { MatTabGroup, MatTabChangeEvent } from '@angular/material/tabs';
import { ScrollDispatcher, CdkScrollable } from '@angular/cdk/scrolling';
import { Subscription } from 'rxjs';
@Directive({
selector: '[ScrollRetainer]'
})
export class ScrollRetainerDirective implements AfterViewInit, OnInit, OnDestroy {
constructor(private matTabGroup: MatTabGroup, private scrollDispatcher: ScrollDispatcher) {}
private container: CdkScrollable;
private scrollSub: Subscription;
private tabSub: Subscription;
ngOnInit(): void {
this.scrollSub = this.scrollDispatcher.scrolled().subscribe(
(el: CdkScrollable) => {
this.container = el;
});
}
ngAfterViewInit(): void {
this.tabSub = this.matTabGroup.selectedTabChange.subscribe((tabChangeEvent: MatTabChangeEvent) => {
this.container.scrollTo({
top: this.container.measureScrollOffset('top'),
left: 0,
behavior: 'auto'
});
});
}
ngOnDestroy(): void {
this.scrollSub.unsubscribe();
this.tabSub.unsubscribe();
}
}
after tweak the min-height solution a few times, I realised the number is not totally random
if you set min-height to any number, it will prevent any subsequent tab clicks but not the first one after page load/refresh, in fact it'll jump to window top and then scroll to tab top, that's even worse.
if you set min-height to a number larger than the distance from top of the page to top of the nav tab, then it will prevent jumping for all tab clicks including the first click, that's why in a few previous examples in this thread, setting a min-height: 800px fixes it. But it doesn't have to be 800, it just needs to be larger than the distance mentioned above.
So if you have a project with predicted layout, setting min-height might be the quickest hacky way to fix this.
This issue happening on my angular 9 application, but in different scenario.
I have modal pop-up, when i close the modal, page automatically scrolls up in internet explorer.
previously with angular 8 and material 7.3.3 I was able to fix this issue by adding window.scroll() event after modal event.
but after updating angular 9 and material 9. window.scroll() is not working anymore.
I removed window.scroll() event and in chrome and other browsers scroll behaviour working as I expected.. but not in internet explorer, so I am not sure if it is related to window.scroll() compability with IE or is material breaking something.
anyone figured out how to solve this issue?
Still happens in 9.1.12! Please fix this. It was driving me nuts till I used the work around, but work arounds are difficult to remember. my html now has bug comments in it. ugg
And another month goes by... if only google had developers working in house. They do? You don't say...
We had the same problem and fixed it with this workaround:
@Directive({
// tslint:disable-next-line: directive-selector
selector: 'mat-tab-group[scrollFix]'
})
export class PsMatTabGroupScrollFixDirective implements AfterViewInit, OnDestroy {
private matTabGroupEl: HTMLElement = this.el.nativeElement;
private animationSub = Subscription.EMPTY;
constructor(private matTabGroup: MatTabGroup, private el: ElementRef) {
}
public ngAfterViewInit(): void {
const orig = this.matTabGroup._handleClick.bind(this.matTabGroup);
this.matTabGroup._handleClick = (tab, tabHeader, index) => {
if (!tab.disabled) {
this.matTabGroupEl.style.minHeight = this.matTabGroupEl.clientHeight + 'px';
}
return orig(tab, tabHeader, index);
}
this.animationSub = this.matTabGroup.animationDone.subscribe(() => {
this.matTabGroupEl.style.minHeight = 'unset';
});
}
public ngOnDestroy() {
this.animationSub.unsubscribe();
}
}
Unfortunatelly it overrides the _handleClick method, which is not in the public api. So it can break at any update, but at least it works for now.
Valid arguments, but let's give the Angular devs some more time and not over-react. Understand this is open source. Let's try to view things from their point of view. Explore other options in the meantime.
Here's an updated demo that reproduces this issue with 11.0.3.
And another demo that demonstrates that this behavior still exists with the MDC-based tabs in @angular/material-experimental.
Same happening to me, for now I'm solving this using tips provided above, but proper fix would be awesome.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.