@@ -11,10 +11,10 @@ import {
1111 TemplateRef ,
1212 OnChanges ,
1313 SimpleChanges ,
14+ OnDestroy ,
1415 OnInit ,
1516 ChangeDetectorRef
1617} from "@angular/core" ;
17- import { EventService } from "carbon-components-angular/utils" ;
1818import { I18n } from "carbon-components-angular/i18n" ;
1919import { Tab } from "./tab.component" ;
2020
@@ -33,7 +33,8 @@ import { Tab } from "./tab.component";
3333 }"
3434 role="navigation"
3535 [attr.aria-label]="ariaLabel"
36- [attr.aria-labelledby]="ariaLabelledby">
36+ [attr.aria-labelledby]="ariaLabelledby"
37+ #tabsScrollable>
3738 <button
3839 #leftOverflowNavButton
3940 type="button"
@@ -132,7 +133,7 @@ import { Tab } from "./tab.component";
132133 `
133134} )
134135
135- export class TabHeaders implements AfterContentInit , OnChanges , OnInit {
136+ export class TabHeaders implements AfterContentInit , OnChanges , OnDestroy , OnInit {
136137 /**
137138 * List of `Tab` components.
138139 */
@@ -172,6 +173,8 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
172173 * Gets the Unordered List element that holds the `Tab` headings from the view DOM.
173174 */
174175 // @ts -ignore
176+ @ViewChild ( "tabsScrollable" , { static : true } ) tabsScrollable : ElementRef < HTMLElement > ;
177+ // @ts -ignore
175178 @ViewChild ( "tabList" , { static : true } ) headerContainer ;
176179 // @ts -ignore
177180 @ViewChild ( "rightOverflowNavButton" , { static : true } ) rightOverflowNavButton ;
@@ -199,8 +202,9 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
199202 public currentSelectedTab : number ;
200203
201204 public get hasHorizontalOverflow ( ) {
202- const tabList = this . headerContainer . nativeElement ;
203- return tabList . scrollWidth > tabList . clientWidth ;
205+ const scrollWidth = this . headerContainer . nativeElement . scrollWidth ;
206+ const availableWidth = this . tabsScrollable . nativeElement . clientWidth ;
207+ return scrollWidth > availableWidth ;
204208 }
205209
206210 public get leftOverflowNavButtonHidden ( ) {
@@ -217,12 +221,12 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
217221 // width of the overflow buttons
218222 OVERFLOW_BUTTON_OFFSET = 40 ;
219223
224+ private resizeObserver : ResizeObserver ;
220225 private overflowNavInterval ;
221226
222227 constructor (
223228 protected elementRef : ElementRef ,
224229 protected changeDetectorRef : ChangeDetectorRef ,
225- protected eventService : EventService ,
226230 protected i18n : I18n
227231 ) { }
228232
@@ -290,8 +294,17 @@ export class TabHeaders implements AfterContentInit, OnChanges, OnInit {
290294 }
291295 }
292296
293- ngOnInit ( ) {
294- this . eventService . on ( window as any , "resize" , ( ) => this . handleScroll ( ) ) ;
297+ ngOnInit ( ) : void {
298+ // Update scroll on resize
299+ this . resizeObserver = new ResizeObserver ( ( ) => {
300+ // Need to explicitly trigger change detection since this runs outside Angular zone
301+ this . changeDetectorRef . detectChanges ( ) ;
302+ } ) ;
303+ this . resizeObserver . observe ( this . tabsScrollable . nativeElement ) ;
304+ }
305+
306+ ngOnDestroy ( ) : void {
307+ this . resizeObserver . unobserve ( this . tabsScrollable . nativeElement ) ;
295308 }
296309
297310 ngAfterContentInit ( ) {
0 commit comments