Skip to content

Commit ee232e8

Browse files
Merge pull request #5930 from IgniteUI/ddimitrov/fix-5582-8.2.x
Add resize observers for tabs
2 parents 7212a9f + eaa063f commit ee232e8

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

projects/igniteui-angular/src/lib/tabs/tab-item.component.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ import {
66
HostListener,
77
Input,
88
TemplateRef,
9-
ViewChild
9+
ViewChild,
10+
NgZone,
11+
AfterViewInit,
12+
OnDestroy
1013
} from '@angular/core';
1114

1215
import { IgxTabsGroupComponent } from './tabs-group.component';
1316
import { IgxTabItemBase, IgxTabsBase } from './tabs.common';
1417
import { IgxTabItemTemplateDirective } from './tabs.directives';
18+
import ResizeObserver from 'resize-observer-polyfill';
1519

1620
@Component({
1721
selector: 'igx-tab-item',
1822
templateUrl: 'tab-item.component.html'
1923
})
2024

21-
export class IgxTabItemComponent extends IgxTabItemBase {
22-
25+
export class IgxTabItemComponent extends IgxTabItemBase implements AfterViewInit, OnDestroy {
2326
/**
2427
* Gets the group associated with the tab.
2528
* ```html
@@ -82,8 +85,9 @@ export class IgxTabItemComponent extends IgxTabItemBase {
8285
private _changesCount = 0; // changes and updates accordingly applied to the tab.
8386
private _isSelected = false;
8487
private _disabled = false;
88+
private _resizeObserver: ResizeObserver;
8589

86-
constructor(private _tabs: IgxTabsBase, private _element: ElementRef) {
90+
constructor(private _tabs: IgxTabsBase, private _element: ElementRef, private _ngZone: NgZone) {
8791
super();
8892
this._nativeTabItem = _element;
8993
}
@@ -145,6 +149,20 @@ export class IgxTabItemComponent extends IgxTabItemBase {
145149
@HostBinding('attr.aria-controls')
146150
public ariaControls = 'igx-tab-item-group-' + this.index;
147151

152+
ngAfterViewInit(): void {
153+
this._ngZone.runOutsideAngular(() => {
154+
this._resizeObserver = new ResizeObserver(() => {
155+
this._tabs.transformIndicatorAnimation(this._nativeTabItem.nativeElement, 0);
156+
});
157+
});
158+
}
159+
160+
ngOnDestroy(): void {
161+
this._ngZone.runOutsideAngular(() => {
162+
this._resizeObserver.disconnect();
163+
});
164+
}
165+
148166
/**
149167
* @hidden
150168
*/
@@ -153,16 +171,6 @@ export class IgxTabItemComponent extends IgxTabItemBase {
153171
this.select();
154172
}
155173

156-
/**
157-
* @hidden
158-
*/
159-
@HostListener('window:resize', ['$event'])
160-
public onResize(event) {
161-
if (this.isSelected) {
162-
this._tabs.transformIndicatorAnimation(this.nativeTabItem.nativeElement);
163-
}
164-
}
165-
166174
/**
167175
* @hidden
168176
*/
@@ -269,6 +277,15 @@ export class IgxTabItemComponent extends IgxTabItemBase {
269277
*/
270278
public setSelectedInternal(newValue: boolean) {
271279
this._isSelected = newValue;
280+
this._ngZone.runOutsideAngular(() => {
281+
if (this._resizeObserver) {
282+
if (this._isSelected) {
283+
this._resizeObserver.observe(this._element.nativeElement);
284+
} else {
285+
this._resizeObserver.disconnect();
286+
}
287+
}
288+
});
272289
this.tabindex = newValue ? 0 : -1;
273290
}
274291

projects/igniteui-angular/src/lib/tabs/tabs-group.component.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,6 @@ export class IgxTabsGroupComponent extends IgxTabsGroupBase implements AfterCont
9393
@HostBinding('class.igx-tabs__group')
9494
public styleClass = true;
9595

96-
@HostListener('window:resize', ['$event'])
97-
public onResize(event) {
98-
if (this.isSelected) {
99-
this._tabs.transformContentAnimation(this.relatedTab, 0);
100-
}
101-
}
102-
10396
/**
10497
* An accessor that returns the `IgxTabItemComponent` component.
10598
* ```typescript

projects/igniteui-angular/src/lib/tabs/tabs.common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export abstract class IgxTabsBase {
1717
scrollElement(element: any, scrollRight: boolean) {}
1818
performSelectionChange(newTab: IgxTabItemBase) {}
1919
transformContentAnimation(tab: IgxTabItemBase, duration: number) {}
20-
transformIndicatorAnimation(element: HTMLElement) {}
20+
transformIndicatorAnimation(element: HTMLElement, duration?: number) {}
2121
}
2222

2323
/** @hidden */

projects/igniteui-angular/src/lib/tabs/tabs.component.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
QueryList,
1414
ViewChild,
1515
ViewChildren,
16-
OnDestroy
16+
OnDestroy,
17+
NgZone
1718
} from '@angular/core';
1819
import { Subscription } from 'rxjs';
1920
import { IgxBadgeModule } from '../badge/badge.component';
@@ -23,6 +24,7 @@ import { IgxTabItemComponent } from './tab-item.component';
2324
import { IgxTabsGroupComponent } from './tabs-group.component';
2425
import { IgxLeftButtonStyleDirective, IgxRightButtonStyleDirective, IgxTabItemTemplateDirective } from './tabs.directives';
2526
import { IgxTabsBase, IgxTabItemBase } from './tabs.common';
27+
import ResizeObserver from 'resize-observer-polyfill';
2628

2729
export enum TabsType {
2830
FIXED = 'fixed',
@@ -228,6 +230,7 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
228230

229231
private _groupChanges$: Subscription;
230232
private _selectedIndex = -1;
233+
private _resizeObserver: ResizeObserver;
231234

232235
/**
233236
* @hidden
@@ -295,7 +298,7 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
295298
}
296299
}
297300

298-
constructor(private _element: ElementRef) {
301+
constructor(private _element: ElementRef, private _ngZone: NgZone) {
299302
}
300303

301304
/**
@@ -326,6 +329,17 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
326329
this._groupChanges$ = this.groups.changes.subscribe(() => {
327330
this.resetSelectionOnCollectionChanged();
328331
});
332+
333+
this._ngZone.runOutsideAngular(() => {
334+
this._resizeObserver = new ResizeObserver(() => {
335+
if (!this.hasContentTabs && this._selectedIndex >= 0 && this._selectedIndex < this.tabs.length) {
336+
const newTab = this.tabs.toArray()[this._selectedIndex];
337+
this.transformContentAnimation(newTab, 0);
338+
}
339+
});
340+
341+
this._resizeObserver.observe(this.tabsContainer.nativeElement);
342+
});
329343
}
330344

331345
/**
@@ -335,6 +349,10 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
335349
if (this._groupChanges$) {
336350
this._groupChanges$.unsubscribe();
337351
}
352+
353+
this._ngZone.runOutsideAngular(() => {
354+
this._resizeObserver.disconnect();
355+
});
338356
}
339357

340358
private resetSelectionOnCollectionChanged(): void {
@@ -445,16 +463,17 @@ export class IgxTabsComponent implements IgxTabsBase, AfterViewInit, OnDestroy {
445463
// animation for the new panel/group (not needed for tab only mode)
446464
public transformContentAnimation(tab: IgxTabItemBase, duration: number): void {
447465
const contentOffset = this.tabsContainer.nativeElement.offsetWidth * tab.index;
448-
this.contentsContainer.nativeElement.style.transitionDuration = `${duration}s`;
466+
this.contentsContainer.nativeElement.style.transitionDuration = duration > 0 ? `${duration}s` : 'initial';
449467
this.contentsContainer.nativeElement.style.transform = `translate(${-contentOffset}px)`;
450468
}
451469

452470
/**
453471
* @hidden
454472
*/
455-
public transformIndicatorAnimation(element: HTMLElement): void {
473+
public transformIndicatorAnimation(element: HTMLElement, duration = 0.3): void {
456474
if (this.selectedIndicator) {
457475
this.selectedIndicator.nativeElement.style.visibility = 'visible';
476+
this.selectedIndicator.nativeElement.style.transitionDuration = duration > 0 ? `${duration}s` : 'initial';
458477
this.selectedIndicator.nativeElement.style.width = `${element.offsetWidth}px`;
459478
this.selectedIndicator.nativeElement.style.transform = `translate(${element.offsetLeft}px)`;
460479
}

0 commit comments

Comments
 (0)