Skip to content

Commit

Permalink
fix(material/tabs): ensure the ink bar realigns when the tab header i…
Browse files Browse the repository at this point in the history
…tems have changed in dimensions (#24885)

(cherry picked from commit 2ced52a)
  • Loading branch information
hilts-vaughan authored and crisbeto committed May 18, 2022
1 parent 6f7584a commit 8611a74
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/material/tabs/paginated-tab-header.ts
Expand Up @@ -26,8 +26,17 @@ import {coerceNumberProperty, NumberInput} from '@angular/cdk/coercion';
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 @@ -207,7 +216,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 @@ -235,6 +244,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

0 comments on commit 8611a74

Please sign in to comment.