Skip to content

Commit 757ac1f

Browse files
committed
fix(tabs): fix tab headers resize/scroll issue
1 parent 4d640e8 commit 757ac1f

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"@storybook/theming": "5.3.19",
9595
"@types/jasmine": "2.8.2",
9696
"@types/node": "11.13.0",
97+
"@types/resize-observer-browser": "^0.1.7",
9798
"babel-loader": "8.0.5",
9899
"carbon-components": "10.58.3",
99100
"chai": "4.2.0",

src/tabs/tab-headers.component.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

tsconfig.spec.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
],
2323
"types": [
2424
"jasmine",
25-
"node"
25+
"node",
26+
"resize-observer-browser"
2627
],
2728
"paths": {
2829
"carbon-components-angular": [

0 commit comments

Comments
 (0)