Skip to content

Commit 75fb9cb

Browse files
authored
Merge pull request #2493 from yinglejia/tabs
fix(tabs): fix tab headers resize/scroll issue
2 parents 4232578 + 87789b4 commit 75fb9cb

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
1818
import { I18n } from "carbon-components-angular/i18n";
1919
import { 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() {

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)