Skip to content

Commit 589a22d

Browse files
committed
fix(material-experimental/mdc-list): fix styles for normal lists
Fixes up various styles for the lists under the "normal lists" section of the demo. There are still some issues around the avatars and dividers on the last list
1 parent 9343d08 commit 589a22d

File tree

8 files changed

+128
-21
lines changed

8 files changed

+128
-21
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ <h3 mat-subheader>Items</h3>
1818
<mat-list-item *ngFor="let contact of contacts">
1919
<h3 mat-line>{{contact.name}}</h3>
2020
<p mat-line *ngIf="thirdLine">extra line</p>
21-
<p mat-line class="demo-secondary-text">{{contact.headline}}</p>
21+
<p mat-line>{{contact.headline}}</p>
2222
</mat-list-item>
2323
</mat-list>
2424

@@ -29,15 +29,15 @@ <h3 mat-subheader>Today</h3>
2929
<h4 mat-line>{{message.from}}</h4>
3030
<p mat-line>
3131
<span>{{message.subject}} -- </span>
32-
<span class="demo-secondary-text">{{message.message}}</span>
32+
<span>{{message.message}}</span>
3333
</p>
3434
<mat-divider inset *ngIf="!last"></mat-divider>
3535
</mat-list-item>
3636
<mat-divider></mat-divider>
3737
<mat-list-item *ngFor="let message of messages">
3838
<h4 mat-line>{{message.from}}</h4>
39-
<p mat-line> {{message.subject}} </p>
40-
<p mat-line class="demo-secondary-text">{{message.message}} </p>
39+
<p mat-line>{{message.subject}}</p>
40+
<p mat-line>{{message.message}}</p>
4141
</mat-list-item>
4242
</mat-list>
4343
</div>
@@ -54,7 +54,7 @@ <h3 mat-subheader>Items</h3>
5454
<mat-list dense>
5555
<mat-list-item *ngFor="let contact of contacts">
5656
<h3 mat-line>{{contact.name}}</h3>
57-
<p mat-line class="demo-secondary-text">{{contact.headline}}</p>
57+
<p mat-line>{{contact.headline}}</p>
5858
</mat-list-item>
5959
</mat-list>
6060

@@ -64,7 +64,7 @@ <h3 mat-subheader>Today</h3>
6464
<img mat-list-avatar [src]="message.image" alt="Image of {{message.from}}">
6565
<h4 mat-line>{{message.from}}</h4>
6666
<p mat-line> {{message.subject}} </p>
67-
<p mat-line class="demo-secondary-text">{{message.message}} </p>
67+
<p mat-line>{{message.message}} </p>
6868
</mat-list-item>
6969
</mat-list>
7070
</div>
@@ -90,7 +90,7 @@ <h2>Nav lists</h2>
9090
<a mat-list-item *ngFor="let link of links; last as last" href="http://www.google.com">
9191
<mat-icon mat-list-icon>folder</mat-icon>
9292
<span mat-line>{{ link.name }}</span>
93-
<span mat-line class="demo-secondary-text"> Description </span>
93+
<span mat-line> Description </span>
9494
<mat-divider inset *ngIf="!last"></mat-divider>
9595
</a>
9696
</mat-nav-list>

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {AfterContentInit, ElementRef, NgZone, OnDestroy, QueryList} from '@angular/core';
10+
import {setLines} from '@angular/material/core';
11+
import {Subscription} from 'rxjs';
12+
import {startWith} from 'rxjs/operators';
13+
914
export class MatListBase {}
1015

11-
export class MatListItemBase {}
16+
export class MatListItemBase implements AfterContentInit, OnDestroy {
17+
lines: QueryList<ElementRef<Element>>;
18+
19+
private _subscriptions = new Subscription();
20+
21+
constructor(protected _element: ElementRef, protected _ngZone: NgZone) {}
22+
23+
ngAfterContentInit() {
24+
this._ngZone.runOutsideAngular(() => {
25+
this._subscriptions.add(this.lines.changes.pipe(startWith(this.lines))
26+
.subscribe((lines: QueryList<ElementRef<Element>>) => {
27+
lines.forEach((line: ElementRef<Element>, index: number) => {
28+
line.nativeElement.classList.toggle('mdc-list-item__primary-text', index === 0);
29+
line.nativeElement.classList.toggle('mdc-list-item__secondary-text', index !== 0);
30+
});
31+
setLines(lines, this._element, 'mat-mdc');
32+
}));
33+
});
34+
}
35+
36+
ngOnDestroy() {
37+
this._subscriptions.unsubscribe();
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
<ng-content select="[mat-list-avatar],[matListAvatar],[mat-list-icon],[matListIcon]"></ng-content>
12
<span class="mdc-list-item__text"><ng-content></ng-content></span>

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,53 @@
88
.mat-mdc-list-base {
99
display: block;
1010
}
11+
12+
// While MDC expects only span elements under mdc-list-item__text, we have historically supported
13+
// using other elements (e.g. h1, h2, etc). This resets some common properties that might cause
14+
// problems.
15+
// TODO(mmalerba): Do we actually want to do this? We could switch to expecting spans like MDC and
16+
// let people reset as necessary instead.
17+
.mat-mdc-list-base .mdc-list-item__text > *,
18+
.mat-mdc-list-base .mdc-list-item__text > * {
19+
padding: 0;
20+
}
21+
22+
// .mdc-list-item__secondary-text adds its own font settings, so only reset if it doesn't have that
23+
// class
24+
.mat-mdc-list-base .mdc-list-item__text > :not(.mdc-list-item__secondary-text),
25+
.mat-mdc-list-base .mdc-list-item__text > :not(.mdc-list-item__secondary-text) {
26+
font-size: inherit;
27+
font-weight: normal;
28+
}
29+
30+
// .mdc-list-item__primary-text adds its own margin settings, so only reset if it doesn't have that
31+
// class
32+
.mat-mdc-list-base .mdc-list-item__text > :not(.mdc-list-item__primary-text),
33+
.mat-mdc-list-base .mdc-list-item__text > :not(.mdc-list-item__primary-text) {
34+
margin: 0;
35+
36+
// Fixes the gap between the 2nd & 3rd lines.
37+
&.mdc-list-item__secondary-text {
38+
margin-top: -3px;
39+
}
40+
}
41+
42+
43+
// MDC does have 2-line support, but it applies to the entire list, whereas ours applies to
44+
// individual items. Therefore, we need to add our own styles.
45+
.mat-mdc-2-line {
46+
height: 72px;
47+
48+
.mdc-list-item__text {
49+
align-self: flex-start;
50+
}
51+
}
52+
53+
// MDC does not support more than 2 lines, so we need to add our own styles.
54+
.mat-mdc-3-line {
55+
height: 88px;
56+
57+
.mdc-list-item__text {
58+
align-self: flex-start;
59+
}
60+
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ChangeDetectionStrategy, Component, Directive, ViewEncapsulation} from '@angular/core';
9+
import {
10+
ChangeDetectionStrategy,
11+
Component,
12+
ContentChildren,
13+
Directive, ElementRef,
14+
QueryList,
15+
ViewEncapsulation
16+
} from '@angular/core';
17+
import {MatLine} from '@angular/material/core';
1018
import {MatListBase, MatListItemBase} from './list-base';
1119

1220
/**
@@ -15,7 +23,7 @@ import {MatListBase, MatListItemBase} from './list-base';
1523
*/
1624
@Directive({
1725
selector: '[mat-list-avatar], [matListAvatar]',
18-
host: {'class': 'mat-mdc-list-avatar'}
26+
host: {'class': 'mat-mdc-list-avatar mdc-list-item__graphic'}
1927
})
2028
export class MatListAvatarCssMatStyler {}
2129

@@ -25,7 +33,7 @@ export class MatListAvatarCssMatStyler {}
2533
*/
2634
@Directive({
2735
selector: '[mat-list-icon], [matListIcon]',
28-
host: {'class': 'mat-mdc-list-icon'}
36+
host: {'class': 'mat-mdc-list-icon mdc-list-item__graphic'}
2937
})
3038
export class MatListIconCssMatStyler {}
3139

@@ -35,7 +43,9 @@ export class MatListIconCssMatStyler {}
3543
*/
3644
@Directive({
3745
selector: '[mat-subheader], [matSubheader]',
38-
host: {'class': 'mat-mdc-subheader'}
46+
// TODO(mmalerba): MDC's subheader font looks identical to the list item font, figure out why and
47+
// make a change in one of the repos to visually distinguish.
48+
host: {'class': 'mat-mdc-subheader mdc-list-group__subheader'}
3949
})
4050
export class MatListSubheaderCssMatStyler {}
4151

@@ -62,4 +72,7 @@ export class MatList extends MatListBase {}
6272
encapsulation: ViewEncapsulation.None,
6373
changeDetection: ChangeDetectionStrategy.OnPush,
6474
})
65-
export class MatListItem extends MatListItemBase {}
75+
export class MatListItem extends MatListItemBase {
76+
@ContentChildren(MatLine, {read: ElementRef, descendants: true}) lines:
77+
QueryList<ElementRef<Element>>;
78+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {NgModule} from '@angular/core';
10+
import {MatLineModule} from '@angular/material/core';
1011
import {MatDividerModule} from '@angular/material/divider';
1112
import {MatActionList} from './action-list';
1213
import {
@@ -20,6 +21,7 @@ import {MatNavList} from './nav-list';
2021
import {MatListOption, MatSelectionList} from './selection-list';
2122

2223
@NgModule({
24+
imports: [MatLineModule],
2325
exports: [
2426
MatList,
2527
MatActionList,
@@ -31,6 +33,7 @@ import {MatListOption, MatSelectionList} from './selection-list';
3133
MatListIconCssMatStyler,
3234
MatListSubheaderCssMatStyler,
3335
MatDividerModule,
36+
MatLineModule,
3437
],
3538
declarations: [
3639
MatList,

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ChangeDetectionStrategy, Component, forwardRef, ViewEncapsulation} from '@angular/core';
9+
import {
10+
ChangeDetectionStrategy,
11+
Component,
12+
ContentChildren, ElementRef,
13+
forwardRef,
14+
QueryList,
15+
ViewEncapsulation
16+
} from '@angular/core';
1017
import {NG_VALUE_ACCESSOR} from '@angular/forms';
18+
import {MatLine} from '@angular/material/core';
1119
import {MatListBase, MatListItemBase} from './list-base';
1220

1321
const MAT_SELECTION_LIST_VALUE_ACCESSOR: any = {
@@ -49,4 +57,7 @@ export class MatSelectionList extends MatListBase {}
4957
encapsulation: ViewEncapsulation.None,
5058
changeDetection: ChangeDetectionStrategy.OnPush,
5159
})
52-
export class MatListOption extends MatListItemBase {}
60+
export class MatListOption extends MatListItemBase {
61+
@ContentChildren(MatLine, {read: ElementRef, descendants: true}) lines:
62+
QueryList<ElementRef<Element>>;
63+
}

src/material/core/line/line.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ export class MatLine {}
3131
* Helper that takes a query list of lines and sets the correct class on the host.
3232
* @docs-private
3333
*/
34-
export function setLines(lines: QueryList<MatLine>, element: ElementRef<HTMLElement>) {
34+
export function setLines(lines: QueryList<unknown>, element: ElementRef<HTMLElement>,
35+
prefix = 'mat') {
3536
// Note: doesn't need to unsubscribe, because `changes`
3637
// gets completed by Angular when the view is destroyed.
3738
lines.changes.pipe(startWith(lines)).subscribe(({length}) => {
38-
setClass(element, 'mat-2-line', false);
39-
setClass(element, 'mat-3-line', false);
40-
setClass(element, 'mat-multi-line', false);
39+
setClass(element, `${prefix}-2-line`, false);
40+
setClass(element, `${prefix}-3-line`, false);
41+
setClass(element, `${prefix}-multi-line`, false);
4142

4243
if (length === 2 || length === 3) {
43-
setClass(element, `mat-${length}-line`, true);
44+
setClass(element, `${prefix}-${length}-line`, true);
4445
} else if (length > 3) {
45-
setClass(element, `mat-multi-line`, true);
46+
setClass(element, `${prefix}-multi-line`, true);
4647
}
4748
});
4849
}

0 commit comments

Comments
 (0)