-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathtab-nav-bar.ts
More file actions
440 lines (385 loc) · 13.6 KB
/
Copy pathtab-nav-bar.ts
File metadata and controls
440 lines (385 loc) · 13.6 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
/**
* @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 {
AfterContentInit,
AfterViewInit,
booleanAttribute,
ChangeDetectionStrategy,
Component,
ContentChildren,
ElementRef,
forwardRef,
Input,
numberAttribute,
OnDestroy,
QueryList,
ViewChild,
ViewEncapsulation,
inject,
HostAttributeToken,
signal,
computed,
} from '@angular/core';
import {
MAT_RIPPLE_GLOBAL_OPTIONS,
MatRipple,
RippleConfig,
RippleGlobalOptions,
RippleTarget,
ThemePalette,
_StructuralStylesLoader,
_animationsDisabled,
} from '../../core';
import {_IdGenerator, FocusableOption, FocusMonitor} from '@angular/cdk/a11y';
import {MatInkBar, InkBarItem} from '../ink-bar';
import {BehaviorSubject, Subject} from 'rxjs';
import {startWith, takeUntil} from 'rxjs/operators';
import {ENTER, SPACE} from '@angular/cdk/keycodes';
import {MAT_TABS_CONFIG, MatTabsConfig} from '../tab-config';
import {
MatPaginatedTabHeader,
MatPaginatedTabHeaderItem,
normalizeDuration,
} from '../paginated-tab-header';
import {CdkObserveContent} from '@angular/cdk/observers';
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
/**
* 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',
templateUrl: 'tab-nav-bar.html',
styleUrl: '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]': '_animationsDisabled',
'[style.--mat-tab-header-animation-duration]': 'animationDuration',
},
encapsulation: ViewEncapsulation.None,
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Eager,
imports: [MatRipple, CdkObserveContent],
})
export class MatTabNav extends MatPaginatedTabHeader implements AfterContentInit, AfterViewInit {
_focusedItem = signal<MatPaginatedTabHeaderItem | null>(null);
/** 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.value;
}
set fitInkBarToContent(value: boolean) {
this._fitInkBarToContent.next(value);
this._changeDetectorRef.markForCheck();
}
_fitInkBarToContent = new BehaviorSubject(false);
/** Whether tabs should be stretched to fill the header. */
@Input({alias: 'mat-stretch-tabs', transform: booleanAttribute})
stretchTabs: boolean = true;
/** Duration for the header animation. Will be normalized to milliseconds if no units are set. */
@Input({transform: normalizeDuration}) animationDuration: string = '';
/** Query list of all tab links of the tab navigation. */
@ContentChildren(forwardRef(() => MatTabLink), {descendants: true})
_items!: QueryList<MatTabLink>;
/**
* Theme color of the background of the tab nav. 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()
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({transform: booleanAttribute})
get disableRipple() {
return this._disableRipple();
}
set disableRipple(value: boolean) {
this._disableRipple.set(value);
}
private _disableRipple = signal(false);
/**
* Theme color of the nav bar. 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 = '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;
@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() {
const defaultConfig = inject<MatTabsConfig>(MAT_TABS_CONFIG, {optional: true});
super();
this.disablePagination =
defaultConfig && defaultConfig.disablePagination != null
? defaultConfig.disablePagination
: false;
this.fitInkBarToContent =
defaultConfig && defaultConfig.fitInkBarToContent != null
? defaultConfig.fitInkBarToContent
: false;
this.stretchTabs =
defaultConfig && defaultConfig.stretchTabs != null ? defaultConfig.stretchTabs : true;
}
protected _itemSelected() {
// noop
}
override ngAfterContentInit() {
this._inkBar = new MatInkBar(this._items);
// 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();
// Turn the `change` stream into a signal to try and avoid "changed after checked" errors.
this._keyManager!.change.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() =>
this._focusedItem.set(this._keyManager?.activeItem || null),
);
}
override ngAfterViewInit() {
if (!this.tabPanel && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw new Error('A mat-tab-nav-panel must be specified via [tabPanel].');
}
super.ngAfterViewInit();
}
/** 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;
if (this.tabPanel) {
this.tabPanel._activeTabId = items[i].id;
}
// Updating the `selectedIndex` won't trigger the `change` event on
// the key manager so we need to set the signal from here.
this._focusedItem.set(items[i]);
this._changeDetectorRef.markForCheck();
return;
}
}
this.selectedIndex = -1;
}
_getRole(): string | null {
return this.tabPanel ? 'tablist' : this._elementRef.nativeElement.getAttribute('role');
}
_hasFocus(link: MatTabLink): boolean {
return this._keyManager?.activeItem === link;
}
}
/**
* Link inside a `mat-tab-nav-bar`.
*/
@Component({
selector: '[mat-tab-link], [matTabLink]',
exportAs: 'matTabLink',
encapsulation: ViewEncapsulation.None,
templateUrl: 'tab-link.html',
styleUrl: 'tab-link.css',
host: {
'class': 'mdc-tab mat-mdc-tab-link mat-focus-indicator',
'[attr.aria-controls]': '_getAriaControls()',
'[attr.aria-current]': '_getAriaCurrent()',
'[attr.aria-disabled]': 'disabled',
'[attr.aria-selected]': '_getAriaSelected()',
'[attr.id]': 'id',
'[attr.tabIndex]': '_tabIndex()',
'[attr.role]': '_getRole()',
'[class.mat-mdc-tab-disabled]': 'disabled',
'[class.mdc-tab--active]': 'active',
'(focus)': '_handleFocus()',
'(keydown)': '_handleKeydown($event)',
},
imports: [MatRipple],
})
export class MatTabLink
extends InkBarItem
implements AfterViewInit, OnDestroy, RippleTarget, FocusableOption
{
private _tabNavBar = inject(MatTabNav);
elementRef = inject(ElementRef);
private _focusMonitor = inject(FocusMonitor);
private readonly _destroyed = new Subject<void>();
/** Whether the tab link is active or not. */
protected _isActive: boolean = false;
protected _tabIndex = computed(() =>
this._tabNavBar._focusedItem() === this ? this.tabIndex : -1,
);
/** Whether the link is active. */
@Input({transform: booleanAttribute})
get active(): boolean {
return this._isActive;
}
set active(value: boolean) {
if (value !== this._isActive) {
this._isActive = value;
this._tabNavBar.updateActiveLink();
}
}
/** Whether the tab link is disabled. */
@Input({transform: booleanAttribute})
disabled: boolean = false;
/** Whether ripples are disabled on the tab link. */
@Input({transform: booleanAttribute})
get disableRipple() {
return this._disableRipple();
}
set disableRipple(value: boolean) {
this._disableRipple.set(value);
}
private _disableRipple = signal(false);
@Input({
transform: (value: unknown) => (value == null ? 0 : numberAttribute(value)),
})
tabIndex: number = 0;
/**
* 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: string = inject(_IdGenerator).getId('mat-tab-link-');
constructor() {
super();
inject(_CdkPrivateStyleLoader).load(_StructuralStylesLoader);
const globalRippleOptions = inject<RippleGlobalOptions | null>(MAT_RIPPLE_GLOBAL_OPTIONS, {
optional: true,
});
const tabIndex = inject(new HostAttributeToken('tabindex'), {optional: true});
this.rippleConfig = globalRippleOptions || {};
this.tabIndex = tabIndex == null ? 0 : parseInt(tabIndex) || 0;
if (_animationsDisabled()) {
this.rippleConfig.animation = {enterDuration: 0, exitDuration: 0};
}
this._tabNavBar._fitInkBarToContent
.pipe(takeUntil(this._destroyed))
.subscribe(fitInkBarToContent => {
this.fitInkBarToContent = fitInkBarToContent;
});
}
/** Focuses the tab link. */
focus() {
this.elementRef.nativeElement.focus();
}
ngAfterViewInit() {
this._focusMonitor.monitor(this.elementRef);
}
override ngOnDestroy() {
this._destroyed.next();
this._destroyed.complete();
super.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 (event.keyCode === SPACE || event.keyCode === ENTER) {
if (this.disabled) {
event.preventDefault();
} else if (this._tabNavBar.tabPanel) {
// Only prevent the default action on space since it can scroll the page.
// Don't prevent enter since it can break link navigation.
if (event.keyCode === SPACE) {
event.preventDefault();
}
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');
}
}
/**
* 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,
})
export class MatTabNavPanel {
/** Unique id for the tab panel. */
@Input() id: string = inject(_IdGenerator).getId('mat-tab-nav-panel-');
/** Id of the active tab in the nav bar. */
_activeTabId?: string;
}