Skip to content

Commit 654abd7

Browse files
authored
fix: tab header paginator not show without viewport resize (#562)
* fix: tab header paginator not show without viewport resize * feat: use observeResizeOn instead and mod stream * feat: mod detect dom resize and realign * Create small-sheep-train.md
1 parent a886dcd commit 654abd7

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

.changeset/small-sheep-train.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alauda/ui": patch
3+
---
4+
5+
fix: tab header paginator not show without viewport resize

jest.setup.ts

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ Object.defineProperty(window, 'matchMedia', {
2222
dispatchEvent: jest.fn(),
2323
})),
2424
});
25+
26+
global.ResizeObserver = jest.fn().mockImplementation(() => ({
27+
observe: jest.fn(),
28+
unobserve: jest.fn(),
29+
disconnect: jest.fn(),
30+
}));

src/tabs/tab-header.component.ts

+11-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FocusKeyManager } from '@angular/cdk/a11y';
22
import { ObserversModule } from '@angular/cdk/observers';
33
import { PortalModule } from '@angular/cdk/portal';
4-
import { ViewportRuler } from '@angular/cdk/scrolling';
54
import { NgClass, NgIf } from '@angular/common';
65
import {
76
AfterContentChecked,
@@ -20,10 +19,10 @@ import {
2019
ViewChild,
2120
ViewEncapsulation,
2221
} from '@angular/core';
23-
import { Subject, takeUntil } from 'rxjs';
22+
import { Subject, debounceTime, merge, takeUntil } from 'rxjs';
2423

2524
import { IconComponent } from '../icon/icon.component';
26-
import { Bem, buildBem } from '../internal/utils';
25+
import { Bem, buildBem, observeResizeOn } from '../internal/utils';
2726

2827
import {
2928
TabHeaderAddonDirective,
@@ -95,12 +94,6 @@ export class TabHeaderComponent
9594
/** Used to manage focus between the tabs. */
9695
private _keyManager: FocusKeyManager<TabLabelWrapperDirective>;
9796

98-
/**
99-
* The number of tab labels that are displayed on the header. When this changes, the header
100-
* should re-evaluate the scroll position.
101-
*/
102-
private _tabLabelCount: number;
103-
10497
@Input()
10598
type: TabType = TabType.Line;
10699

@@ -160,13 +153,6 @@ export class TabHeaderComponent
160153
}
161154

162155
ngAfterContentChecked(): void {
163-
// If the number of tab labels have changed, check if scrolling should be enabled
164-
if (this._tabLabelCount !== this._labelWrappers.length) {
165-
this._updatePagination();
166-
this._tabLabelCount = this._labelWrappers.length;
167-
this._changeDetectorRef.markForCheck();
168-
}
169-
170156
// If the selected index has changed, scroll to the label and check if the scrolling controls
171157
// should be disabled.
172158
if (this._selectedIndexChanged) {
@@ -196,7 +182,6 @@ export class TabHeaderComponent
196182
* Aligns the ink bar to the selected tab on load.
197183
*/
198184
ngAfterContentInit() {
199-
const resize = this._viewportRuler.change(150);
200185
const realign = () => {
201186
this._updatePagination();
202187
this._alignActiveIndicatorToSelectedTab();
@@ -210,15 +195,15 @@ export class TabHeaderComponent
210195
.withWrap();
211196
this._keyManager.updateActiveItem(0);
212197

213-
// Defer the first call in order to allow for slower browsers to lay out the elements.
214-
// This helps in cases where the user lands directly on a page with paginated tabs.
215-
requestAnimationFrame(realign);
216-
217-
// On window resize, realign the ink bar and update the orientation of
198+
// On tab list resize, realign the ink bar and update the orientation of
218199
// the key manager if the direction has changed.
219-
resize.pipe(takeUntil(this._destroyed)).subscribe(() => {
220-
realign();
221-
});
200+
merge(
201+
observeResizeOn(this._tabList.nativeElement),
202+
observeResizeOn(this._tabListContainer.nativeElement),
203+
observeResizeOn(this._paginationWrapper.nativeElement),
204+
)
205+
.pipe(debounceTime(100), takeUntil(this._destroyed))
206+
.subscribe(realign);
222207

223208
// If there is a change in the focus key manager we need to emit the `indexFocused`
224209
// event in order to provide a public event that notifies about focus changes. Also we realign
@@ -456,8 +441,5 @@ export class TabHeaderComponent
456441
this._activeIndicator.alignToElement(selectedLabelWrapper);
457442
}
458443

459-
constructor(
460-
private readonly _changeDetectorRef: ChangeDetectorRef,
461-
private readonly _viewportRuler: ViewportRuler,
462-
) {}
444+
constructor(private readonly _changeDetectorRef: ChangeDetectorRef) {}
463445
}

0 commit comments

Comments
 (0)