/
tab-nav-bar.ts
463 lines (406 loc) · 13.6 KB
/
tab-nav-bar.ts
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
/**
* @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.io/license
*/
import {
AfterContentChecked,
AfterContentInit,
AfterViewInit,
Attribute,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChildren,
Directive,
ElementRef,
forwardRef,
Inject,
Input,
NgZone,
OnDestroy,
Optional,
QueryList,
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {
CanDisable,
CanDisableRipple,
HasTabIndex,
MAT_RIPPLE_GLOBAL_OPTIONS,
mixinDisabled,
mixinDisableRipple,
mixinTabIndex,
RippleConfig,
RippleGlobalOptions,
RippleTarget,
ThemePalette,
} from '@angular/material/core';
import {FocusableOption, FocusMonitor} from '@angular/cdk/a11y';
import {Directionality} from '@angular/cdk/bidi';
import {ViewportRuler} from '@angular/cdk/scrolling';
import {Platform} from '@angular/cdk/platform';
import {MatInkBar, MatInkBarItem, mixinInkBarItem} from '../ink-bar';
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
import {BehaviorSubject, Subject} from 'rxjs';
import {startWith, takeUntil} from 'rxjs/operators';
import {SPACE} from '@angular/cdk/keycodes';
import {MAT_TABS_CONFIG, MatTabsConfig} from '../tab-config';
import {MatPaginatedTabHeader, MatPaginatedTabHeaderItem} from '../paginated-tab-header';
// Increasing integer for generating unique ids for tab nav components.
let nextUniqueId = 0;
/**
* Base class with all of the `MatTabNav` functionality.
* @docs-private
*/
@Directive()
export abstract class _MatTabNavBase
extends MatPaginatedTabHeader
implements AfterContentChecked, AfterContentInit, OnDestroy
{
/** Query list of all tab links of the tab navigation. */
abstract override _items: QueryList<MatPaginatedTabHeaderItem & {active: boolean; id: string}>;
/** Background color of the tab nav. */
@Input()
get backgroundColor(): ThemePalette {
return this._backgroundColor;
}
set backgroundColor(value: ThemePalette) {
const classList = 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;
/** Whether the ripple effect is disabled or not. */
@Input()
get disableRipple(): boolean {
return this._disableRipple;
}
set disableRipple(value: BooleanInput) {
this._disableRipple = coerceBooleanProperty(value);
}
private _disableRipple: boolean = false;
/** Theme color of the nav bar. */
@Input() color: ThemePalette = 'primary';
/**
* Associated tab panel controlled by the nav bar. If not provided, then the nav bar
* follows the ARIA link / navigation landmark pattern. If provided, it follows the
* ARIA tabs design pattern.
*/
@Input() tabPanel?: MatTabNavPanel;
constructor(
elementRef: ElementRef,
@Optional() dir: Directionality,
ngZone: NgZone,
changeDetectorRef: ChangeDetectorRef,
viewportRuler: ViewportRuler,
platform: Platform,
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
) {
super(elementRef, changeDetectorRef, viewportRuler, dir, ngZone, platform, animationMode);
}
protected _itemSelected() {
// noop
}
override ngAfterContentInit() {
// We need this to run before the `changes` subscription in parent to ensure that the
// selectedIndex is up-to-date by the time the super class starts looking for it.
this._items.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => {
this.updateActiveLink();
});
super.ngAfterContentInit();
}
/** Notifies the component that the active link has been changed. */
updateActiveLink() {
if (!this._items) {
return;
}
const items = this._items.toArray();
for (let i = 0; i < items.length; i++) {
if (items[i].active) {
this.selectedIndex = i;
this._changeDetectorRef.markForCheck();
if (this.tabPanel) {
this.tabPanel._activeTabId = items[i].id;
}
return;
}
}
// The ink bar should hide itself if no items are active.
this.selectedIndex = -1;
this._inkBar.hide();
}
_getRole(): string | null {
return this.tabPanel ? 'tablist' : this._elementRef.nativeElement.getAttribute('role');
}
}
// Boilerplate for applying mixins to MatTabLink.
const _MatTabLinkMixinBase = mixinTabIndex(mixinDisableRipple(mixinDisabled(class {})));
/** Base class with all of the `MatTabLink` functionality. */
@Directive()
export class _MatTabLinkBase
extends _MatTabLinkMixinBase
implements
AfterViewInit,
OnDestroy,
CanDisable,
CanDisableRipple,
HasTabIndex,
RippleTarget,
FocusableOption
{
/** Whether the tab link is active or not. */
protected _isActive: boolean = false;
/** Whether the link is active. */
@Input()
get active(): boolean {
return this._isActive;
}
set active(value: BooleanInput) {
const newValue = coerceBooleanProperty(value);
if (newValue !== this._isActive) {
this._isActive = newValue;
this._tabNavBar.updateActiveLink();
}
}
/**
* Ripple configuration for ripples that are launched on pointer down. The ripple config
* is set to the global ripple options since we don't have any configurable options for
* the tab link ripples.
* @docs-private
*/
rippleConfig: RippleConfig & RippleGlobalOptions;
/**
* Whether ripples are disabled on interaction.
* @docs-private
*/
get rippleDisabled(): boolean {
return (
this.disabled ||
this.disableRipple ||
this._tabNavBar.disableRipple ||
!!this.rippleConfig.disabled
);
}
/** Unique id for the tab. */
@Input() id = `mat-tab-link-${nextUniqueId++}`;
constructor(
private _tabNavBar: _MatTabNavBase,
/** @docs-private */ public elementRef: ElementRef,
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalRippleOptions: RippleGlobalOptions | null,
@Attribute('tabindex') tabIndex: string,
private _focusMonitor: FocusMonitor,
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
) {
super();
this.rippleConfig = globalRippleOptions || {};
this.tabIndex = parseInt(tabIndex) || 0;
if (animationMode === 'NoopAnimations') {
this.rippleConfig.animation = {enterDuration: 0, exitDuration: 0};
}
}
/** Focuses the tab link. */
focus() {
this.elementRef.nativeElement.focus();
}
ngAfterViewInit() {
this._focusMonitor.monitor(this.elementRef);
}
ngOnDestroy() {
this._focusMonitor.stopMonitoring(this.elementRef);
}
_handleFocus() {
// Since we allow navigation through tabbing in the nav bar, we
// have to update the focused index whenever the link receives focus.
this._tabNavBar.focusIndex = this._tabNavBar._items.toArray().indexOf(this);
}
_handleKeydown(event: KeyboardEvent) {
if (this._tabNavBar.tabPanel && event.keyCode === SPACE) {
this.elementRef.nativeElement.click();
}
}
_getAriaControls(): string | null {
return this._tabNavBar.tabPanel
? this._tabNavBar.tabPanel?.id
: this.elementRef.nativeElement.getAttribute('aria-controls');
}
_getAriaSelected(): string | null {
if (this._tabNavBar.tabPanel) {
return this.active ? 'true' : 'false';
} else {
return this.elementRef.nativeElement.getAttribute('aria-selected');
}
}
_getAriaCurrent(): string | null {
return this.active && !this._tabNavBar.tabPanel ? 'page' : null;
}
_getRole(): string | null {
return this._tabNavBar.tabPanel ? 'tab' : this.elementRef.nativeElement.getAttribute('role');
}
_getTabIndex(): number {
if (this._tabNavBar.tabPanel) {
return this._isActive && !this.disabled ? 0 : -1;
} else {
return this.tabIndex;
}
}
}
const _MatTabLinkBaseWithInkBarItem = mixinInkBarItem(_MatTabLinkBase);
/**
* Navigation component matching the styles of the tab group header.
* Provides anchored navigation with animated ink bar.
*/
@Component({
selector: '[mat-tab-nav-bar]',
exportAs: 'matTabNavBar, matTabNav',
inputs: ['color'],
templateUrl: 'tab-nav-bar.html',
styleUrls: ['tab-nav-bar.css'],
host: {
'[attr.role]': '_getRole()',
'class': 'mat-mdc-tab-nav-bar mat-mdc-tab-header',
'[class.mat-mdc-tab-header-pagination-controls-enabled]': '_showPaginationControls',
'[class.mat-mdc-tab-header-rtl]': "_getLayoutDirection() == 'rtl'",
'[class.mat-mdc-tab-nav-bar-stretch-tabs]': 'stretchTabs',
'[class.mat-primary]': 'color !== "warn" && color !== "accent"',
'[class.mat-accent]': 'color === "accent"',
'[class.mat-warn]': 'color === "warn"',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
},
encapsulation: ViewEncapsulation.None,
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Default,
})
export class MatTabNav extends _MatTabNavBase implements AfterContentInit, AfterViewInit {
/** Whether the ink bar should fit its width to the size of the tab label content. */
@Input()
get fitInkBarToContent(): boolean {
return this._fitInkBarToContent.value;
}
set fitInkBarToContent(v: BooleanInput) {
this._fitInkBarToContent.next(coerceBooleanProperty(v));
this._changeDetectorRef.markForCheck();
}
_fitInkBarToContent = new BehaviorSubject(false);
/** Whether tabs should be stretched to fill the header. */
@Input('mat-stretch-tabs')
get stretchTabs(): boolean {
return this._stretchTabs;
}
set stretchTabs(v: BooleanInput) {
this._stretchTabs = coerceBooleanProperty(v);
}
private _stretchTabs = true;
@ContentChildren(forwardRef(() => MatTabLink), {descendants: true}) _items: QueryList<MatTabLink>;
@ViewChild('tabListContainer', {static: true}) _tabListContainer: ElementRef;
@ViewChild('tabList', {static: true}) _tabList: ElementRef;
@ViewChild('tabListInner', {static: true}) _tabListInner: ElementRef;
@ViewChild('nextPaginator') _nextPaginator: ElementRef<HTMLElement>;
@ViewChild('previousPaginator') _previousPaginator: ElementRef<HTMLElement>;
_inkBar: MatInkBar;
constructor(
elementRef: ElementRef,
@Optional() dir: Directionality,
ngZone: NgZone,
changeDetectorRef: ChangeDetectorRef,
viewportRuler: ViewportRuler,
platform: Platform,
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
@Optional() @Inject(MAT_TABS_CONFIG) defaultConfig?: MatTabsConfig,
) {
super(elementRef, dir, ngZone, changeDetectorRef, viewportRuler, platform, animationMode);
this.disablePagination =
defaultConfig && defaultConfig.disablePagination != null
? defaultConfig.disablePagination
: false;
this.fitInkBarToContent =
defaultConfig && defaultConfig.fitInkBarToContent != null
? defaultConfig.fitInkBarToContent
: false;
}
override ngAfterContentInit() {
this._inkBar = new MatInkBar(this._items);
super.ngAfterContentInit();
}
override ngAfterViewInit() {
if (!this.tabPanel && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw new Error('A mat-tab-nav-panel must be specified via [tabPanel].');
}
}
}
/**
* Link inside of a `mat-tab-nav-bar`.
*/
@Component({
selector: '[mat-tab-link], [matTabLink]',
exportAs: 'matTabLink',
inputs: ['disabled', 'disableRipple', 'tabIndex', 'active', 'id'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
templateUrl: 'tab-link.html',
styleUrls: ['tab-link.css'],
host: {
'class': 'mdc-tab mat-mdc-tab-link mat-mdc-focus-indicator',
'[attr.aria-controls]': '_getAriaControls()',
'[attr.aria-current]': '_getAriaCurrent()',
'[attr.aria-disabled]': 'disabled',
'[attr.aria-selected]': '_getAriaSelected()',
'[attr.id]': 'id',
'[attr.tabIndex]': '_getTabIndex()',
'[attr.role]': '_getRole()',
'[class.mat-mdc-tab-disabled]': 'disabled',
'[class.mdc-tab--active]': 'active',
'(focus)': '_handleFocus()',
'(keydown)': '_handleKeydown($event)',
},
})
export class MatTabLink extends _MatTabLinkBaseWithInkBarItem implements MatInkBarItem, OnDestroy {
private readonly _destroyed = new Subject<void>();
constructor(
tabNavBar: MatTabNav,
elementRef: ElementRef,
@Optional() @Inject(MAT_RIPPLE_GLOBAL_OPTIONS) globalRippleOptions: RippleGlobalOptions | null,
@Attribute('tabindex') tabIndex: string,
focusMonitor: FocusMonitor,
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
) {
super(tabNavBar, elementRef, globalRippleOptions, tabIndex, focusMonitor, animationMode);
tabNavBar._fitInkBarToContent.pipe(takeUntil(this._destroyed)).subscribe(fitInkBarToContent => {
this.fitInkBarToContent = fitInkBarToContent;
});
}
override ngOnDestroy() {
this._destroyed.next();
this._destroyed.complete();
super.ngOnDestroy();
}
}
/**
* Tab panel component associated with MatTabNav.
*/
@Component({
selector: 'mat-tab-nav-panel',
exportAs: 'matTabNavPanel',
template: '<ng-content></ng-content>',
host: {
'[attr.aria-labelledby]': '_activeTabId',
'[attr.id]': 'id',
'class': 'mat-mdc-tab-nav-panel',
'role': 'tabpanel',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatTabNavPanel {
/** Unique id for the tab panel. */
@Input() id = `mat-tab-nav-panel-${nextUniqueId++}`;
/** Id of the active tab in the nav bar. */
_activeTabId?: string;
}