Skip to content

Commit

Permalink
feat(material-experimental/mdc-list): add support for marking the act… (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
mmalerba authored Feb 18, 2022
1 parent db126b5 commit 3e10809
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/dev-app/mdc-list/mdc-list-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ <h2>Dense lists</h2>
<div>
<h2>Nav lists</h2>
<mat-nav-list>
<a mat-list-item *ngFor="let link of links" href="http://www.google.com">
<a mat-list-item *ngFor="let link of links" [href]="link.href" [activated]="isActivated(link.href)">
{{ link.name }}
</a>
</mat-nav-list>
<div *ngIf="infoClicked">
More info!
</div>
<mat-nav-list>
<a mat-list-item *ngFor="let link of links; last as last" href="http://www.google.com">
<a mat-list-item *ngFor="let link of links; last as last" [href]="link.href" [activated]="isActivated(link.href)">
<mat-icon matListItemIcon>folder</mat-icon>
<span matListItemTitle>{{ link.name }}</span>
</a>
Expand Down
11 changes: 10 additions & 1 deletion src/dev-app/mdc-list/mdc-list-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export class MdcListDemo {
},
];

links: {name: string}[] = [{name: 'Inbox'}, {name: 'Outbox'}, {name: 'Spam'}, {name: 'Trash'}];
links: {name: string; href: string}[] = [
{name: 'Inbox', href: '/mdc-list#inbox'},
{name: 'Outbox', href: '/mdc-list#outbox'},
{name: 'Spam', href: '/mdc-list#spam'},
{name: 'Trash', href: '/mdc-list#trash'},
];

thirdLine = false;
showBoxes = false;
Expand All @@ -72,4 +77,8 @@ export class MdcListDemo {
alertItem(msg: string) {
alert(msg);
}

isActivated(href: string) {
return window.location.href === new URL(href, window.location.href).toString();
}
}
1 change: 1 addition & 0 deletions src/material-experimental/mdc-list/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ng_module(
] + glob(["**/*.html"]),
deps = [
"//src:dev_mode_types",
"//src/cdk/coercion",
"//src/cdk/collections",
"//src/cdk/observers",
"//src/material-experimental/mdc-core",
Expand Down
13 changes: 13 additions & 0 deletions src/material-experimental/mdc-list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Platform} from '@angular/cdk/platform';
import {
ChangeDetectionStrategy,
Component,
Input,
ContentChildren,
ElementRef,
Inject,
Expand All @@ -26,6 +27,7 @@ import {
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
import {MatListBase, MatListItemBase} from './list-base';
import {MatListItemLine, MatListItemMeta, MatListItemTitle} from './list-item-sections';
import {coerceBooleanProperty} from '@angular/cdk/coercion';

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

/** Indicates whether an item in a `<mat-nav-list>` is the currently active page. */
@Input()
get activated() {
return this._activated;
}
set activated(activated) {
this._activated = coerceBooleanProperty(activated);
}
_activated = false;

constructor(
element: ElementRef,
ngZone: NgZone,
Expand Down

0 comments on commit 3e10809

Please sign in to comment.