-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathtab-group.ts
More file actions
609 lines (526 loc) · 22.1 KB
/
Copy pathtab-group.ts
File metadata and controls
609 lines (526 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
AfterContentChecked,
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChildren,
ElementRef,
EventEmitter,
Input,
OnDestroy,
Output,
QueryList,
ViewChild,
ViewEncapsulation,
booleanAttribute,
inject,
numberAttribute,
ViewChildren,
AfterViewInit,
NgZone,
} from '@angular/core';
import {MAT_TAB_GROUP, MatTab} from './tab';
import {MatTabHeader} from './tab-header';
import {ThemePalette, MatRipple, _animationsDisabled} from '../core';
import {merge, Subscription} from 'rxjs';
import {MAT_TABS_CONFIG, MatTabsConfig} from './tab-config';
import {startWith} from 'rxjs/operators';
import {_IdGenerator, CdkMonitorFocus, FocusOrigin} from '@angular/cdk/a11y';
import {MatTabBody} from './tab-body';
import {CdkPortalOutlet} from '@angular/cdk/portal';
import {MatTabLabelWrapper} from './tab-label-wrapper';
import {Platform} from '@angular/cdk/platform';
import {normalizeDuration} from './paginated-tab-header';
/** @docs-private */
export interface MatTabGroupBaseHeader {
_alignInkBarToSelectedTab(): void;
updatePagination(): void;
focusIndex: number;
}
/** Possible positions for the tab header. */
export type MatTabHeaderPosition = 'above' | 'below';
/** Possible values for the animation duration of a tab group. */
export type MatTabGroupAnimationDuration =
| string
| number
| {body: string | number; header: string | number};
/** Boolean constant that determines whether the tab group supports the `backgroundColor` input */
const ENABLE_BACKGROUND_INPUT = true;
/**
* Material design tab-group component. Supports basic tab pairs (label + content) and includes
* animated ink-bar, keyboard navigation, and screen reader.
* See: https://material.io/design/components/tabs.html
*/
@Component({
selector: 'mat-tab-group',
exportAs: 'matTabGroup',
templateUrl: 'tab-group.html',
styleUrl: 'tab-group.css',
encapsulation: ViewEncapsulation.None,
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Eager,
providers: [
{
provide: MAT_TAB_GROUP,
useExisting: MatTabGroup,
},
],
host: {
'class': 'mat-mdc-tab-group',
'[class]': '"mat-" + (color || "primary")',
'[class.mat-mdc-tab-group-dynamic-height]': 'dynamicHeight',
'[class.mat-mdc-tab-group-inverted-header]': 'headerPosition === "below"',
'[class.mat-mdc-tab-group-stretch-tabs]': 'stretchTabs',
'[attr.mat-align-tabs]': 'alignTabs',
'[style.--mat-tab-body-animation-duration]': '_bodyAnimationDuration',
'[style.--mat-tab-header-animation-duration]': '_headerAnimationDuration',
},
imports: [
MatTabHeader,
MatTabLabelWrapper,
CdkMonitorFocus,
MatRipple,
CdkPortalOutlet,
MatTabBody,
],
})
export class MatTabGroup
implements AfterViewInit, AfterContentInit, AfterContentChecked, OnDestroy
{
readonly _elementRef = inject(ElementRef);
private _changeDetectorRef = inject(ChangeDetectorRef);
private _ngZone = inject(NgZone);
private _tabsSubscription = Subscription.EMPTY;
private _tabLabelSubscription = Subscription.EMPTY;
private _tabBodySubscription = Subscription.EMPTY;
private _diAnimationsDisabled = _animationsDisabled();
protected _bodyAnimationDuration!: string;
protected _headerAnimationDuration!: string;
/**
* All tabs inside the tab group. This includes tabs that belong to groups that are nested
* inside the current one. We filter out only the tabs that belong to this group in `_tabs`.
*/
@ContentChildren(MatTab, {descendants: true}) _allTabs!: QueryList<MatTab>;
@ViewChildren(MatTabBody) _tabBodies: QueryList<MatTabBody> | undefined;
@ViewChild('tabBodyWrapper') _tabBodyWrapper!: ElementRef;
@ViewChild('tabHeader') _tabHeader!: MatTabHeader;
/** All of the tabs that belong to the group. */
_tabs: QueryList<MatTab> = new QueryList<MatTab>();
/** The tab index that should be selected after the content has been checked. */
private _indexToSelect: number | null = 0;
/** Index of the tab that was focused last. */
private _lastFocusedTabIndex: number | null = null;
/** Snapshot of the height of the tab body wrapper before another tab is activated. */
private _tabBodyWrapperHeight: number = 0;
/**
* Theme color of the tab group. This API is supported in M2 themes only, it
* has no effect in M3 themes. For color customization in M3, see https://material.angular.dev/components/tabs/styling.
*
* For information on applying color variants in M3, see
* https://material.angular.dev/guide/material-2-theming#optional-add-backwards-compatibility-styles-for-color-variants
*/
@Input()
color: ThemePalette;
/** Whether the ink bar should fit its width to the size of the tab label content. */
@Input({transform: booleanAttribute})
get fitInkBarToContent(): boolean {
return this._fitInkBarToContent;
}
set fitInkBarToContent(value: boolean) {
this._fitInkBarToContent = value;
this._changeDetectorRef.markForCheck();
}
private _fitInkBarToContent = false;
/** Whether tabs should be stretched to fill the header. */
@Input({alias: 'mat-stretch-tabs', transform: booleanAttribute})
stretchTabs: boolean = true;
/** Alignment for tabs label. */
@Input({alias: 'mat-align-tabs'})
alignTabs: string | null = null;
/** Whether the tab group should grow to the size of the active tab. */
@Input({transform: booleanAttribute})
dynamicHeight: boolean = false;
/** The index of the active tab. */
@Input({transform: numberAttribute})
get selectedIndex(): number | null {
return this._selectedIndex;
}
set selectedIndex(value: number) {
this._indexToSelect = isNaN(value) ? null : value;
}
private _selectedIndex: number | null = null;
/** Position of the tab header. */
@Input() headerPosition: MatTabHeaderPosition = 'above';
/** Duration for the tab animation. Will be normalized to milliseconds if no units are set. */
@Input()
get animationDuration(): MatTabGroupAnimationDuration {
return this._animationDuration;
}
set animationDuration(value: MatTabGroupAnimationDuration) {
this._animationDuration = value;
if (value && typeof value === 'object') {
this._bodyAnimationDuration = normalizeDuration(value.body);
this._headerAnimationDuration = normalizeDuration(value.header);
} else {
this._headerAnimationDuration = this._bodyAnimationDuration = normalizeDuration(value);
}
}
private _animationDuration!: MatTabGroupAnimationDuration;
/**
* `tabindex` to be set on the inner element that wraps the tab content. Can be used for improved
* accessibility when the tab does not have focusable elements or if it has scrollable content.
* The `tabindex` will be removed automatically for inactive tabs.
* Read more at https://www.w3.org/TR/wai-aria-practices/examples/tabs/tabs-2/tabs.html
*/
@Input({transform: numberAttribute})
get contentTabIndex(): number | null {
return this._contentTabIndex;
}
set contentTabIndex(value: number) {
this._contentTabIndex = isNaN(value) ? null : value;
}
private _contentTabIndex: number | null = null;
/**
* Whether pagination should be disabled. This can be used to avoid unnecessary
* layout recalculations if it's known that pagination won't be required.
*/
@Input({transform: booleanAttribute})
disablePagination: boolean = false;
/** Whether ripples in the tab group are disabled. */
@Input({transform: booleanAttribute})
disableRipple: boolean = false;
/**
* By default tabs remove their content from the DOM while it's off-screen.
* Setting this to `true` will keep it in the DOM which will prevent elements
* like iframes and videos from reloading next time it comes back into the view.
*/
@Input({transform: booleanAttribute})
preserveContent: boolean = false;
/**
* Theme color of the background of the tab group. This API is supported in M2 themes only, it
* has no effect in M3 themes. For color customization in M3, see https://material.angular.dev/components/tabs/styling.
*
* For information on applying color variants in M3, see
* https://material.angular.dev/guide/material-2-theming#optional-add-backwards-compatibility-styles-for-color-variants
*
* @deprecated The background color should be customized through Sass theming APIs.
* @breaking-change 20.0.0 Remove this input
*/
@Input()
get backgroundColor(): ThemePalette {
return this._backgroundColor;
}
set backgroundColor(value: ThemePalette) {
if (!ENABLE_BACKGROUND_INPUT) {
throw new Error(`mat-tab-group background color must be set through the Sass theming API`);
}
const classList: DOMTokenList = this._elementRef.nativeElement.classList;
classList.remove('mat-tabs-with-background', `mat-background-${this.backgroundColor}`);
if (value) {
classList.add('mat-tabs-with-background', `mat-background-${value}`);
}
this._backgroundColor = value;
}
private _backgroundColor!: ThemePalette;
/** Aria label of the inner `tablist` of the group. */
@Input('aria-label') ariaLabel!: string;
/** Sets the `aria-labelledby` of the inner `tablist` of the group. */
@Input('aria-labelledby') ariaLabelledby!: string;
/** Output to enable support for two-way binding on `[(selectedIndex)]` */
@Output() readonly selectedIndexChange: EventEmitter<number> = new EventEmitter<number>();
/** Event emitted when focus has changed within a tab group. */
@Output() readonly focusChange: EventEmitter<MatTabChangeEvent> =
new EventEmitter<MatTabChangeEvent>();
/** Event emitted when the body animation has completed */
@Output() readonly animationDone: EventEmitter<void> = new EventEmitter<void>();
/** Event emitted when the tab selection has changed. */
@Output() readonly selectedTabChange: EventEmitter<MatTabChangeEvent> =
new EventEmitter<MatTabChangeEvent>(true);
private _groupId: string;
/** Whether the tab group is rendered on the server. */
protected _isServer: boolean = !inject(Platform).isBrowser;
constructor() {
const defaultConfig = inject<MatTabsConfig>(MAT_TABS_CONFIG, {optional: true});
this._groupId = inject(_IdGenerator).getId('mat-tab-group-');
this.animationDuration =
defaultConfig && defaultConfig.animationDuration ? defaultConfig.animationDuration : '500ms';
this.disablePagination =
defaultConfig && defaultConfig.disablePagination != null
? defaultConfig.disablePagination
: false;
this.dynamicHeight =
defaultConfig && defaultConfig.dynamicHeight != null ? defaultConfig.dynamicHeight : false;
if (defaultConfig?.contentTabIndex != null) {
this.contentTabIndex = defaultConfig.contentTabIndex;
}
this.preserveContent = !!defaultConfig?.preserveContent;
this.fitInkBarToContent =
defaultConfig && defaultConfig.fitInkBarToContent != null
? defaultConfig.fitInkBarToContent
: false;
this.stretchTabs =
defaultConfig && defaultConfig.stretchTabs != null ? defaultConfig.stretchTabs : true;
this.alignTabs =
defaultConfig && defaultConfig.alignTabs != null ? defaultConfig.alignTabs : null;
}
/**
* After the content is checked, this component knows what tabs have been defined
* and what the selected index should be. This is where we can know exactly what position
* each tab should be in according to the new selected index, and additionally we know how
* a new selected tab should transition in (from the left or right).
*/
ngAfterContentChecked() {
// Don't clamp the `indexToSelect` immediately in the setter because it can happen that
// the amount of tabs changes before the actual change detection runs.
const indexToSelect = (this._indexToSelect = this._clampTabIndex(this._indexToSelect));
// If there is a change in selected index, emit a change event. Should not trigger if
// the selected index has not yet been initialized.
if (this._selectedIndex != indexToSelect) {
const isFirstRun = this._selectedIndex == null;
if (!isFirstRun) {
this.selectedTabChange.emit(this._createChangeEvent(indexToSelect));
// Preserve the height so page doesn't scroll up during tab change.
// Fixes https://stackblitz.com/edit/mat-tabs-scroll-page-top-on-tab-change
const wrapper = this._tabBodyWrapper.nativeElement;
wrapper.style.minHeight = wrapper.clientHeight + 'px';
}
// Changing these values after change detection has run
// since the checked content may contain references to them.
Promise.resolve().then(() => {
this._tabs.forEach((tab, index) => (tab.isActive = index === indexToSelect));
if (!isFirstRun) {
this.selectedIndexChange.emit(indexToSelect);
// Clear the min-height, this was needed during tab change to avoid
// unnecessary scrolling.
this._tabBodyWrapper.nativeElement.style.minHeight = '';
}
});
}
// Setup the position for each tab and optionally setup an origin on the next selected tab.
this._tabs.forEach((tab: MatTab, index: number) => {
tab.position = index - indexToSelect;
// If there is already a selected tab, then set up an origin for the next selected tab
// if it doesn't have one already.
if (this._selectedIndex != null && tab.position == 0 && !tab.origin) {
tab.origin = indexToSelect - this._selectedIndex;
}
});
if (this._selectedIndex !== indexToSelect) {
this._selectedIndex = indexToSelect;
this._lastFocusedTabIndex = null;
this._changeDetectorRef.markForCheck();
}
}
ngAfterContentInit() {
this._subscribeToAllTabChanges();
this._subscribeToTabLabels();
// Subscribe to changes in the amount of tabs, in order to be
// able to re-render the content as new tabs are added or removed.
this._tabsSubscription = this._tabs.changes.subscribe(() => {
const indexToSelect = this._clampTabIndex(this._indexToSelect);
// Maintain the previously-selected tab if a new tab is added or removed and there is no
// explicit change that selects a different tab.
if (indexToSelect === this._selectedIndex) {
const tabs = this._tabs.toArray();
let selectedTab: MatTab | undefined;
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].isActive) {
// Assign both to the `_indexToSelect` and `_selectedIndex` so we don't fire a changed
// event, otherwise the consumer may end up in an infinite loop in some edge cases like
// adding a tab within the `selectedIndexChange` event.
this._indexToSelect = this._selectedIndex = i;
this._lastFocusedTabIndex = null;
selectedTab = tabs[i];
break;
}
}
// If we haven't found an active tab and a tab exists at the selected index, it means
// that the active tab was swapped out. Since this won't be picked up by the rendering
// loop in `ngAfterContentChecked`, we need to sync it up manually.
if (!selectedTab && tabs[indexToSelect]) {
Promise.resolve().then(() => {
tabs[indexToSelect].isActive = true;
this.selectedTabChange.emit(this._createChangeEvent(indexToSelect));
});
}
}
this._changeDetectorRef.markForCheck();
});
}
ngAfterViewInit() {
this._tabBodySubscription = this._tabBodies!.changes.subscribe(() => this._bodyCentered(true));
}
/** Listens to changes in all of the tabs. */
private _subscribeToAllTabChanges() {
// Since we use a query with `descendants: true` to pick up the tabs, we may end up catching
// some that are inside of nested tab groups. We filter them out manually by checking that
// the closest group to the tab is the current one.
this._allTabs.changes.pipe(startWith(this._allTabs)).subscribe((tabs: QueryList<MatTab>) => {
this._tabs.reset(
tabs.filter(tab => {
return tab._closestTabGroup === this || !tab._closestTabGroup;
}),
);
this._tabs.notifyOnChanges();
});
}
ngOnDestroy() {
this._tabs.destroy();
this._tabsSubscription.unsubscribe();
this._tabLabelSubscription.unsubscribe();
this._tabBodySubscription.unsubscribe();
}
/** Re-aligns the ink bar to the selected tab element. */
realignInkBar() {
if (this._tabHeader) {
this._tabHeader._alignInkBarToSelectedTab();
}
}
/**
* Recalculates the tab group's pagination dimensions.
*
* WARNING: Calling this method can be very costly in terms of performance. It should be called
* as infrequently as possible from outside of the Tabs component as it causes a reflow of the
* page.
*/
updatePagination() {
if (this._tabHeader) {
this._tabHeader.updatePagination();
}
}
/**
* Sets focus to a particular tab.
* @param index Index of the tab to be focused.
*/
focusTab(index: number) {
const header = this._tabHeader;
if (header) {
header.focusIndex = index;
}
}
_focusChanged(index: number) {
this._lastFocusedTabIndex = index;
this.focusChange.emit(this._createChangeEvent(index));
}
private _createChangeEvent(index: number): MatTabChangeEvent {
const event = new MatTabChangeEvent();
event.index = index;
if (this._tabs && this._tabs.length) {
event.tab = this._tabs.toArray()[index];
}
return event;
}
/**
* Subscribes to changes in the tab labels. This is needed, because the @Input for the label is
* on the MatTab component, whereas the data binding is inside the MatTabGroup. In order for the
* binding to be updated, we need to subscribe to changes in it and trigger change detection
* manually.
*/
private _subscribeToTabLabels() {
if (this._tabLabelSubscription) {
this._tabLabelSubscription.unsubscribe();
}
this._tabLabelSubscription = merge(...this._tabs.map(tab => tab._stateChanges)).subscribe(() =>
this._changeDetectorRef.markForCheck(),
);
}
/** Clamps the given index to the bounds of 0 and the tabs length. */
private _clampTabIndex(index: number | null): number {
// Note the `|| 0`, which ensures that values like NaN can't get through
// and which would otherwise throw the component into an infinite loop
// (since Math.max(NaN, 0) === NaN).
return Math.min(this._tabs.length - 1, Math.max(index || 0, 0));
}
/** Returns a unique id for each tab label element */
_getTabLabelId(tab: MatTab, index: number): string {
return tab.id || `${this._groupId}-label-${index}`;
}
/** Returns a unique id for each tab content element */
_getTabContentId(index: number): string {
return `${this._groupId}-content-${index}`;
}
/**
* Sets the height of the body wrapper to the height of the activating tab if dynamic
* height property is true.
*/
_setTabBodyWrapperHeight(tabHeight: number): void {
if (!this.dynamicHeight || !this._tabBodyWrapperHeight) {
this._tabBodyWrapperHeight = tabHeight;
return;
}
const wrapper: HTMLElement = this._tabBodyWrapper.nativeElement;
wrapper.style.height = this._tabBodyWrapperHeight + 'px';
// This conditional forces the browser to paint the height so that
// the animation to the new height can have an origin.
if (this._tabBodyWrapper.nativeElement.offsetHeight) {
wrapper.style.height = tabHeight + 'px';
}
}
/** Removes the height of the tab body wrapper. */
_removeTabBodyWrapperHeight(): void {
const wrapper = this._tabBodyWrapper.nativeElement;
this._tabBodyWrapperHeight = wrapper.clientHeight;
wrapper.style.height = '';
this._ngZone.run(() => this.animationDone.emit());
}
/** Handle click events, setting new selected index if appropriate. */
_handleClick(tab: MatTab, tabHeader: MatTabGroupBaseHeader, index: number) {
tabHeader.focusIndex = index;
if (!tab.disabled) {
this.selectedIndex = index;
}
}
/** Retrieves the tabindex for the tab. */
_getTabIndex(index: number): number {
const targetIndex = this._lastFocusedTabIndex ?? this.selectedIndex;
return index === targetIndex ? 0 : -1;
}
/** Callback for when the focused state of a tab has changed. */
_tabFocusChanged(focusOrigin: FocusOrigin, index: number) {
// Mouse/touch focus happens during the `mousedown`/`touchstart` phase which
// can cause the tab to be moved out from under the pointer, interrupting the
// click sequence (see #21898). We don't need to scroll the tab into view for
// such cases anyway, because it will be done when the tab becomes selected.
if (focusOrigin && focusOrigin !== 'mouse' && focusOrigin !== 'touch') {
this._tabHeader.focusIndex = index;
}
}
/**
* Callback invoked when the centered state of a tab body changes.
* @param isCenter Whether the tab will be in the center.
*/
protected _bodyCentered(isCenter: boolean) {
// Marks all the existing tabs as inactive and the center tab as active. Note that this can
// be achieved much easier by using a class binding on each body. The problem with
// doing so is that we can't control the timing of when the class is removed from the
// previously-active element and added to the newly-active one. If there's a tick between
// removing the class and adding the new one, the content will jump in a very jarring way.
// We go through the trouble of setting the classes ourselves to guarantee that they're
// swapped out at the same time.
if (isCenter) {
this._tabBodies?.forEach((body, i) => body._setActiveClass(i === this._selectedIndex));
}
}
protected _bodyAnimationsDisabled(): boolean {
return (
this._diAnimationsDisabled ||
this._bodyAnimationDuration === '0' ||
this._bodyAnimationDuration === '0ms'
);
}
}
/** A simple change event emitted on focus or selection changes. */
export class MatTabChangeEvent {
/** Index of the currently-selected tab. */
index!: number;
/** Reference to the currently-selected tab. */
tab!: MatTab;
}