1
1
import { FocusKeyManager } from '@angular/cdk/a11y' ;
2
2
import { ObserversModule } from '@angular/cdk/observers' ;
3
3
import { PortalModule } from '@angular/cdk/portal' ;
4
- import { ViewportRuler } from '@angular/cdk/scrolling' ;
5
4
import { NgClass , NgIf } from '@angular/common' ;
6
5
import {
7
6
AfterContentChecked ,
@@ -20,10 +19,10 @@ import {
20
19
ViewChild ,
21
20
ViewEncapsulation ,
22
21
} from '@angular/core' ;
23
- import { Subject , takeUntil } from 'rxjs' ;
22
+ import { Subject , debounceTime , merge , takeUntil } from 'rxjs' ;
24
23
25
24
import { IconComponent } from '../icon/icon.component' ;
26
- import { Bem , buildBem } from '../internal/utils' ;
25
+ import { Bem , buildBem , observeResizeOn } from '../internal/utils' ;
27
26
28
27
import {
29
28
TabHeaderAddonDirective ,
@@ -95,12 +94,6 @@ export class TabHeaderComponent
95
94
/** Used to manage focus between the tabs. */
96
95
private _keyManager : FocusKeyManager < TabLabelWrapperDirective > ;
97
96
98
- /**
99
- * The number of tab labels that are displayed on the header. When this changes, the header
100
- * should re-evaluate the scroll position.
101
- */
102
- private _tabLabelCount : number ;
103
-
104
97
@Input ( )
105
98
type : TabType = TabType . Line ;
106
99
@@ -160,13 +153,6 @@ export class TabHeaderComponent
160
153
}
161
154
162
155
ngAfterContentChecked ( ) : void {
163
- // If the number of tab labels have changed, check if scrolling should be enabled
164
- if ( this . _tabLabelCount !== this . _labelWrappers . length ) {
165
- this . _updatePagination ( ) ;
166
- this . _tabLabelCount = this . _labelWrappers . length ;
167
- this . _changeDetectorRef . markForCheck ( ) ;
168
- }
169
-
170
156
// If the selected index has changed, scroll to the label and check if the scrolling controls
171
157
// should be disabled.
172
158
if ( this . _selectedIndexChanged ) {
@@ -196,7 +182,6 @@ export class TabHeaderComponent
196
182
* Aligns the ink bar to the selected tab on load.
197
183
*/
198
184
ngAfterContentInit ( ) {
199
- const resize = this . _viewportRuler . change ( 150 ) ;
200
185
const realign = ( ) => {
201
186
this . _updatePagination ( ) ;
202
187
this . _alignActiveIndicatorToSelectedTab ( ) ;
@@ -210,15 +195,15 @@ export class TabHeaderComponent
210
195
. withWrap ( ) ;
211
196
this . _keyManager . updateActiveItem ( 0 ) ;
212
197
213
- // Defer the first call in order to allow for slower browsers to lay out the elements.
214
- // This helps in cases where the user lands directly on a page with paginated tabs.
215
- requestAnimationFrame ( realign ) ;
216
-
217
- // On window resize, realign the ink bar and update the orientation of
198
+ // On tab list resize, realign the ink bar and update the orientation of
218
199
// the key manager if the direction has changed.
219
- resize . pipe ( takeUntil ( this . _destroyed ) ) . subscribe ( ( ) => {
220
- realign ( ) ;
221
- } ) ;
200
+ merge (
201
+ observeResizeOn ( this . _tabList . nativeElement ) ,
202
+ observeResizeOn ( this . _tabListContainer . nativeElement ) ,
203
+ observeResizeOn ( this . _paginationWrapper . nativeElement ) ,
204
+ )
205
+ . pipe ( debounceTime ( 100 ) , takeUntil ( this . _destroyed ) )
206
+ . subscribe ( realign ) ;
222
207
223
208
// If there is a change in the focus key manager we need to emit the `indexFocused`
224
209
// event in order to provide a public event that notifies about focus changes. Also we realign
@@ -456,8 +441,5 @@ export class TabHeaderComponent
456
441
this . _activeIndicator . alignToElement ( selectedLabelWrapper ) ;
457
442
}
458
443
459
- constructor (
460
- private readonly _changeDetectorRef : ChangeDetectorRef ,
461
- private readonly _viewportRuler : ViewportRuler ,
462
- ) { }
444
+ constructor ( private readonly _changeDetectorRef : ChangeDetectorRef ) { }
463
445
}
0 commit comments