Skip to content

fix(material/tabs): scroll position lost when tab header is hidden #25855

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

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/material/tabs/paginated-tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
timer,
fromEvent,
} from 'rxjs';
import {take, switchMap, startWith, skip, takeUntil} from 'rxjs/operators';
import {take, switchMap, startWith, skip, takeUntil, filter} from 'rxjs/operators';
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';

Expand Down Expand Up @@ -256,7 +256,7 @@ export abstract class MatPaginatedTabHeader
}

/** Sends any changes that could affect the layout of the items. */
private _itemsResized(): Observable<void> {
private _itemsResized(): Observable<ResizeObserverEntry[]> {
if (typeof ResizeObserver !== 'function') {
return EMPTY;
}
Expand All @@ -265,14 +265,10 @@ export abstract class MatPaginatedTabHeader
startWith(this._items),
switchMap(
(tabItems: QueryList<MatPaginatedTabHeaderItem>) =>
new Observable((observer: Observer<void>) =>
new Observable((observer: Observer<ResizeObserverEntry[]>) =>
this._ngZone.runOutsideAngular(() => {
const resizeObserver = new ResizeObserver(() => {
observer.next();
});
tabItems.forEach(item => {
resizeObserver.observe(item.elementRef.nativeElement);
});
const resizeObserver = new ResizeObserver(entries => observer.next(entries));
tabItems.forEach(item => resizeObserver.observe(item.elementRef.nativeElement));
return () => {
resizeObserver.disconnect();
};
Expand All @@ -282,6 +278,9 @@ export abstract class MatPaginatedTabHeader
// Skip the first emit since the resize observer emits when an item
// is observed for new items when the tab is already inserted
skip(1),
// Skip emissions where all the elements are invisible since we don't want
// the header to try and re-render with invalid measurements. See #25574.
filter(entries => entries.some(e => e.contentRect.width > 0 && e.contentRect.height > 0)),
);
}

Expand Down