@@ -11,6 +11,7 @@ import {
1111 TemplateRef ,
1212 OnChanges ,
1313 SimpleChanges ,
14+ OnDestroy ,
1415 OnInit ,
1516 ChangeDetectorRef
1617} from "@angular/core" ;
@@ -33,7 +34,8 @@ import { Tab } from "./tab.component";
3334 }"
3435 role="navigation"
3536 [attr.aria-label]="ariaLabel"
36- [attr.aria-labelledby]="ariaLabelledby">
37+ [attr.aria-labelledby]="ariaLabelledby"
38+ #tabsScrollable>
3739 <button
3840 #leftOverflowNavButton
3941 type="button"
@@ -132,7 +134,7 @@ import { Tab } from "./tab.component";
132134 `
133135} )
134136
135- export class TabHeaders implements AfterContentInit , OnChanges , OnInit {
137+ export class TabHeaders implements AfterContentInit , OnChanges , OnDestroy , OnInit {
136138 /**
137139 * List of `Tab` components.
138140 */
@@ -172,6 +174,8 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
172174 * Gets the Unordered List element that holds the `Tab` headings from the view DOM.
173175 */
174176 // @ts -ignore
177+ @ViewChild ( "tabsScrollable" , { static : true } ) tabsScrollable : ElementRef < HTMLElement > ;
178+ // @ts -ignore
175179 @ViewChild ( "tabList" , { static : true } ) headerContainer ;
176180 // @ts -ignore
177181 @ViewChild ( "rightOverflowNavButton" , { static : true } ) rightOverflowNavButton ;
@@ -199,8 +203,9 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
199203 public currentSelectedTab : number ;
200204
201205 public get hasHorizontalOverflow ( ) {
202- const tabList = this . headerContainer . nativeElement ;
203- return tabList . scrollWidth > tabList . clientWidth ;
206+ const scrollWidth = this . headerContainer . nativeElement . scrollWidth ;
207+ const availableWidth = this . tabsScrollable . nativeElement . clientWidth ;
208+ return scrollWidth > availableWidth ;
204209 }
205210
206211 public get leftOverflowNavButtonHidden ( ) {
@@ -217,6 +222,7 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
217222 // width of the overflow buttons
218223 OVERFLOW_BUTTON_OFFSET = 40 ;
219224
225+ private resizeObserver : ResizeObserver ;
220226 private overflowNavInterval ;
221227
222228 constructor (
@@ -290,8 +296,17 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
290296 }
291297 }
292298
293- ngOnInit ( ) {
294- this . eventService . on ( window as any , "resize" , ( ) => this . handleScroll ( ) ) ;
299+ ngOnInit ( ) : void {
300+ // Update scroll on resize
301+ this . resizeObserver = new ResizeObserver ( ( ) => {
302+ // Need to explicitly trigger change detection since this runs outside Angular zone
303+ this . changeDetectorRef . detectChanges ( ) ;
304+ } ) ;
305+ this . resizeObserver . observe ( this . tabsScrollable . nativeElement ) ;
306+ }
307+
308+ ngOnDestroy ( ) : void {
309+ this . resizeObserver . unobserve ( this . tabsScrollable . nativeElement ) ;
295310 }
296311
297312 ngAfterContentInit ( ) {
0 commit comments