Skip to content

Commit 3e10809

Browse files
authored
feat(material-experimental/mdc-list): add support for marking the act… (#24433)
* feat(material-experimental/mdc-list): add support for marking the activated mat-nav-list item * fixup! feat(material-experimental/mdc-list): add support for marking the activated mat-nav-list item * fixup! feat(material-experimental/mdc-list): add support for marking the activated mat-nav-list item
1 parent db126b5 commit 3e10809

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ <h2>Dense lists</h2>
9191
<div>
9292
<h2>Nav lists</h2>
9393
<mat-nav-list>
94-
<a mat-list-item *ngFor="let link of links" href="http://www.google.com">
94+
<a mat-list-item *ngFor="let link of links" [href]="link.href" [activated]="isActivated(link.href)">
9595
{{ link.name }}
9696
</a>
9797
</mat-nav-list>
9898
<div *ngIf="infoClicked">
9999
More info!
100100
</div>
101101
<mat-nav-list>
102-
<a mat-list-item *ngFor="let link of links; last as last" href="http://www.google.com">
102+
<a mat-list-item *ngFor="let link of links; last as last" [href]="link.href" [activated]="isActivated(link.href)">
103103
<mat-icon matListItemIcon>folder</mat-icon>
104104
<span matListItemTitle>{{ link.name }}</span>
105105
</a>

src/dev-app/mdc-list/mdc-list-demo.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ export class MdcListDemo {
4646
},
4747
];
4848

49-
links: {name: string}[] = [{name: 'Inbox'}, {name: 'Outbox'}, {name: 'Spam'}, {name: 'Trash'}];
49+
links: {name: string; href: string}[] = [
50+
{name: 'Inbox', href: '/mdc-list#inbox'},
51+
{name: 'Outbox', href: '/mdc-list#outbox'},
52+
{name: 'Spam', href: '/mdc-list#spam'},
53+
{name: 'Trash', href: '/mdc-list#trash'},
54+
];
5055

5156
thirdLine = false;
5257
showBoxes = false;
@@ -72,4 +77,8 @@ export class MdcListDemo {
7277
alertItem(msg: string) {
7378
alert(msg);
7479
}
80+
81+
isActivated(href: string) {
82+
return window.location.href === new URL(href, window.location.href).toString();
83+
}
7584
}

src/material-experimental/mdc-list/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ng_module(
2525
] + glob(["**/*.html"]),
2626
deps = [
2727
"//src:dev_mode_types",
28+
"//src/cdk/coercion",
2829
"//src/cdk/collections",
2930
"//src/cdk/observers",
3031
"//src/material-experimental/mdc-core",

src/material-experimental/mdc-list/list.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {Platform} from '@angular/cdk/platform';
1010
import {
1111
ChangeDetectionStrategy,
1212
Component,
13+
Input,
1314
ContentChildren,
1415
ElementRef,
1516
Inject,
@@ -26,6 +27,7 @@ import {
2627
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2728
import {MatListBase, MatListItemBase} from './list-base';
2829
import {MatListItemLine, MatListItemMeta, MatListItemTitle} from './list-item-sections';
30+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
2931

3032
@Component({
3133
selector: 'mat-list',
@@ -46,6 +48,7 @@ export class MatList extends MatListBase {}
4648
exportAs: 'matListItem',
4749
host: {
4850
'class': 'mat-mdc-list-item mdc-list-item',
51+
'[class.mdc-list-item--activated]': 'activated',
4952
'[class.mdc-list-item--with-leading-avatar]': '_avatars.length !== 0',
5053
'[class.mdc-list-item--with-leading-icon]': '_icons.length !== 0',
5154
'[class.mdc-list-item--with-trailing-meta]': '_meta.length !== 0',
@@ -62,6 +65,16 @@ export class MatListItem extends MatListItemBase {
6265
@ViewChild('unscopedContent') _unscopedContent: ElementRef<HTMLSpanElement>;
6366
@ViewChild('text') _itemText: ElementRef<HTMLElement>;
6467

68+
/** Indicates whether an item in a `<mat-nav-list>` is the currently active page. */
69+
@Input()
70+
get activated() {
71+
return this._activated;
72+
}
73+
set activated(activated) {
74+
this._activated = coerceBooleanProperty(activated);
75+
}
76+
_activated = false;
77+
6578
constructor(
6679
element: ElementRef,
6780
ngZone: NgZone,

0 commit comments

Comments
 (0)