Skip to content

fix(material/tabs): ensure the ink bar realigns when the tab header changes dimensions #24885

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
May 18, 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
45 changes: 42 additions & 3 deletions src/material/tabs/paginated-tab-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ import {
import {ViewportRuler} from '@angular/cdk/scrolling';
import {FocusKeyManager, FocusableOption} from '@angular/cdk/a11y';
import {ENTER, SPACE, hasModifierKey} from '@angular/cdk/keycodes';
import {merge, of as observableOf, Subject, timer, fromEvent} from 'rxjs';
import {take, takeUntil} from 'rxjs/operators';
import {
merge,
of as observableOf,
Subject,
EMPTY,
Observer,
Observable,
timer,
fromEvent,
} from 'rxjs';
import {take, switchMap, startWith, skip, takeUntil} from 'rxjs/operators';
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';

Expand Down Expand Up @@ -218,7 +227,7 @@ export abstract class MatPaginatedTabHeader

// On dir change or window resize, realign the ink bar and update the orientation of
// the key manager if the direction has changed.
merge(dirChange, resize, this._items.changes)
merge(dirChange, resize, this._items.changes, this._itemsResized())
.pipe(takeUntil(this._destroyed))
.subscribe(() => {
// We need to defer this to give the browser some time to recalculate
Expand Down Expand Up @@ -246,6 +255,36 @@ export abstract class MatPaginatedTabHeader
});
}

/** Sends any changes that could affect the layout of the items. */
private _itemsResized(): Observable<void> {
if (typeof ResizeObserver !== 'function') {
return EMPTY;
}

return this._items.changes.pipe(
startWith(this._items),
switchMap(
(tabItems: QueryList<MatPaginatedTabHeaderItem>) =>
new Observable((observer: Observer<void>) =>
this._ngZone.runOutsideAngular(() => {
const resizeObserver = new ResizeObserver(() => {
observer.next();
});
tabItems.forEach(item => {
resizeObserver.observe(item.elementRef.nativeElement);
});
return () => {
resizeObserver.disconnect();
};
}),
),
),
// 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),
);
}

ngAfterContentChecked(): void {
// If the number of tab labels have changed, check if scrolling should be enabled
if (this._tabLabelCount != this._items.length) {
Expand Down