Skip to content

Commit add3c33

Browse files
committed
feat(material/tabs): Responded to more PR feedback
1 parent 8e17465 commit add3c33

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/components-examples/material/tabs/tab-nav-bar-with-panel/tab-nav-bar-with-panel-example.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<!-- #docregion mat-tab-nav -->
2-
<nav mat-tab-nav-bar [backgroundColor]="background" [panel]="panel">
2+
<nav mat-tab-nav-bar [backgroundColor]="background" [tabPanel]="tabPanel">
33
<a mat-tab-link *ngFor="let link of links"
44
(click)="activeLink = link"
55
[active]="activeLink == link"> {{link}} </a>
66
<a mat-tab-link disabled>Disabled Link</a>
77
</nav>
8-
<mat-tab-nav-panel #panel></mat-tab-nav-panel>
8+
<mat-tab-nav-panel #tabPanel></mat-tab-nav-panel>
99
<!-- #enddocregion mat-tab-nav -->
1010

1111
<button mat-raised-button class="example-action-button" (click)="toggleBackground()">

src/dev-app/mdc-tabs/mdc-tabs-demo.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ <h2>Tab nav bar</h2>
129129
</nav>
130130

131131
<h2>Tab nav bar with panel</h2>
132-
<nav mat-tab-nav-bar [panel]="panel">
132+
<nav mat-tab-nav-bar [tabPanel]="tabPanel">
133133
<a mat-tab-link *ngFor="let link of links"
134134
(click)="activeLink = link"
135135
[active]="activeLink == link">{{link}}</a>
136136
<a mat-tab-link disabled>Disabled Link</a>
137137
</nav>
138-
<mat-tab-nav-panel #panel></mat-tab-nav-panel>
138+
<mat-tab-nav-panel #tabPanel></mat-tab-nav-panel>
139139
</div>

src/material-experimental/mdc-tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ export class MatTabNav extends _MatTabNavBase implements AfterContentInit {
131131
styleUrls: ['tab-link.css'],
132132
host: {
133133
'class': 'mdc-tab mat-mdc-tab-link mat-mdc-focus-indicator',
134-
'[attr.aria-controls]': '_tabNavBar.panel ? _tabNavBar.panel.id : null',
135-
'[attr.aria-current]': '(active && !_tabNavBar.panel) ? "page" : null',
134+
'[attr.aria-controls]': '_tabNavBar.tabPanel ? _tabNavBar.tabPanel.id : null',
135+
'[attr.aria-current]': '(active && !_tabNavBar.tabPanel) ? "page" : null',
136136
'[attr.aria-disabled]': 'disabled',
137-
'[attr.aria-selected]': '_tabNavBar.panel ? (active ? "true" : "false") : null',
137+
'[attr.aria-selected]': '_tabNavBar.tabPanel ? (active ? "true" : "false") : null',
138138
'[attr.id]': 'id',
139139
'[attr.tabIndex]': '_getTabIndex()',
140140
'[class.mat-mdc-tab-disabled]': 'disabled',

src/material/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ export abstract class _MatTabNavBase
6464
implements AfterContentChecked, AfterContentInit, OnDestroy
6565
{
6666
/** Query list of all tab links of the tab navigation. */
67-
abstract override _items: QueryList<MatPaginatedTabHeaderItem & {active: boolean; id: string}>;
67+
abstract override _items: QueryList<
68+
MatPaginatedTabHeaderItem & {active: boolean; id: string; index: number}>;
6869

6970
/** Background color of the tab nav. */
7071
@Input()
@@ -101,7 +102,7 @@ export abstract class _MatTabNavBase
101102
* follows the ARIA link / navigation landmark pattern. If provided, it follows the
102103
* ARIA tabs design pattern.
103104
*/
104-
@Input() panel?: MatTabNavPanel;
105+
@Input() tabPanel?: MatTabNavPanel;
105106

106107
constructor(
107108
elementRef: ElementRef,
@@ -124,6 +125,7 @@ export abstract class _MatTabNavBase
124125
// selectedIndex is up-to-date by the time the super class starts looking for it.
125126
this._items.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => {
126127
this.updateActiveLink();
128+
this.updateIndices();
127129
});
128130

129131
super.ngAfterContentInit();
@@ -142,8 +144,8 @@ export abstract class _MatTabNavBase
142144
this.selectedIndex = i;
143145
this._changeDetectorRef.markForCheck();
144146

145-
if (this.panel) {
146-
this.panel._activeTabId = items[i].id;
147+
if (this.tabPanel) {
148+
this.tabPanel._activeTabId = items[i].id;
147149
}
148150

149151
return;
@@ -154,6 +156,19 @@ export abstract class _MatTabNavBase
154156
this.selectedIndex = -1;
155157
this._inkBar.hide();
156158
}
159+
160+
/** Updates the indices of the tabs within the nav bar. */
161+
updateIndices() {
162+
if (!this._items) {
163+
return;
164+
}
165+
166+
const items = this._items.toArray();
167+
168+
for (let i = 0; i < items.length; i++) {
169+
items[i].index = i;
170+
}
171+
}
157172
}
158173

159174
/**
@@ -260,6 +275,9 @@ export class _MatTabLinkBase
260275
/** Unique id for the tab. */
261276
@Input() id = `mat-tab-link-${nextUniqueId++}`;
262277

278+
/** Index of the tab within the nav bar. Managed by the nav bar. */
279+
index = -1;
280+
263281
constructor(
264282
private _tabNavBar: _MatTabNavBase,
265283
/** @docs-private */ public elementRef: ElementRef,
@@ -294,31 +312,26 @@ export class _MatTabLinkBase
294312
_handleFocus() {
295313
// Since we allow navigation through tabbing in the nav bar, we
296314
// have to update the focused index whenever the link receives focus.
297-
this._tabNavBar.focusIndex = this._getIndex();
315+
this._tabNavBar.focusIndex = this.index;
298316
}
299317

300318
_handleKeydown(event: KeyboardEvent) {
301-
if (this._tabNavBar.panel && event.keyCode === SPACE) {
319+
if (this._tabNavBar.tabPanel && event.keyCode === SPACE) {
302320
this.elementRef.nativeElement.click();
303321
}
304322
}
305323

306324
/** Returns the tab's tabindex. */
307325
_getTabIndex(): number {
308-
if (!this._tabNavBar.panel) {
326+
if (!this._tabNavBar.tabPanel) {
309327
return this.tabIndex;
310328
}
311329

312330
if (!this._tabNavBar._items) {
313331
return this._isActive ? 0 : -1;
314332
}
315333

316-
return this._tabNavBar.focusIndex === this._getIndex() ? 0 : -1;
317-
}
318-
319-
/** Returns this item's index in the nav bar. */
320-
_getIndex(): number {
321-
return this._tabNavBar._items.toArray().indexOf(this);
334+
return (this._tabNavBar.focusIndex === this.index) ? 0 : -1;
322335
}
323336

324337
static ngAcceptInputType_active: BooleanInput;
@@ -336,13 +349,13 @@ export class _MatTabLinkBase
336349
inputs: ['disabled', 'disableRipple', 'tabIndex'],
337350
host: {
338351
'class': 'mat-tab-link mat-focus-indicator',
339-
'[attr.aria-controls]': '_tabNavBar.panel ? _tabNavBar.panel.id : null',
340-
'[attr.aria-current]': '(active && !_tabNavBar.panel) ? "page" : null',
352+
'[attr.aria-controls]': '_tabNavBar.tabPanel ? _tabNavBar.tabPanel.id : null',
353+
'[attr.aria-current]': '(active && !_tabNavBar.tabPanel) ? "page" : null',
341354
'[attr.aria-disabled]': 'disabled',
342-
'[attr.aria-selected]': '_tabNavBar.panel ? (active ? "true" : "false") : null',
355+
'[attr.aria-selected]': '_tabNavBar.tabPanel ? (active ? "true" : "false") : null',
343356
'[attr.id]': 'id',
344357
'[attr.tabIndex]': '_getTabIndex()',
345-
'[attr.role]': '_tabNavBar.panel ? "tab" : null',
358+
'[attr.role]': '_tabNavBar.tabPanel ? "tab" : null',
346359
'[class.mat-tab-disabled]': 'disabled',
347360
'[class.mat-tab-label-active]': 'active',
348361
'(focus)': '_handleFocus()',

0 commit comments

Comments
 (0)