Skip to content

Commit ddbfe11

Browse files
committed
fix(material-experimental/mdc-list): avoid style conflicts with other list-based components
Currently the styles for the MDC-based list are included globally which causes conflicts with other list-based components like `mat-select` and `mat-autocomplete`. This can be seen by going to the MDC list demo and then the MDC select demo, and observing that the option text is now truncated. These changes resolve the issue by nesting the styles under the `mat-mdc-list-base` class.
1 parent 7cc42f5 commit ddbfe11

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
@mixin color($config-or-theme) {
1414
$config: theming.get-color-config($config-or-theme);
1515

16-
// MDC's state styles are tied in with their ripple. Since we don't use the MDC
17-
// ripple, we need to add the hover, focus and selected states manually.
18-
@include interactive-list-theme.private-interactive-list-item-state-colors($config);
16+
.mat-mdc-list-base {
17+
// MDC's state styles are tied in with their ripple. Since we don't use the MDC
18+
// ripple, we need to add the hover, focus and selected states manually.
19+
@include interactive-list-theme.private-interactive-list-item-state-colors($config);
20+
}
1921

2022
@include mdc-helpers.mat-using-mdc-theme($config) {
2123
@include mdc-list.deprecated-without-ripple($query: mdc-helpers.$mat-theme-styles-query);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {MatListBase} from './list-base';
1212
@Component({
1313
selector: 'mat-action-list',
1414
exportAs: 'matActionList',
15-
template: '<ng-content></ng-content>',
15+
template: '<div class="mdc-deprecated-list"><ng-content></ng-content></div>',
1616
host: {
17-
'class': 'mat-mdc-action-list mat-mdc-list-base mdc-deprecated-list',
17+
'class': 'mat-mdc-action-list mat-mdc-list-base',
1818
},
1919
styleUrls: ['list.css'],
2020
encapsulation: ViewEncapsulation.None,

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@
33
@use '../../material/core/style/layout-common';
44
@use '../../cdk/a11y';
55

6-
@include mdc-list.deprecated-without-ripple($query: mdc-helpers.$mat-base-styles-query);
7-
86
// MDC expects the list element to be a `<ul>`, since we use `<mat-list>` instead we need to
97
// explicitly set `display: block`
108
.mat-mdc-list-base {
9+
// This mixin needs to be nested so that styles don't leak into other
10+
// components based on the MDC list (e.g. select, autocomplete).
11+
@include mdc-list.deprecated-without-ripple($query: mdc-helpers.$mat-base-styles-query);
1112
display: block;
13+
14+
// MDC does have 2-line support, but it's a per-list setting, whereas ours applies to individual
15+
// items. Therefore, we need to add our own styles.
16+
.mat-mdc-2-line {
17+
height: 72px;
18+
19+
.mdc-deprecated-list-item__text {
20+
align-self: flex-start;
21+
}
22+
}
23+
24+
// MDC does not support more than 2 lines, so we need to add our own styles.
25+
.mat-mdc-3-line {
26+
height: 88px;
27+
28+
.mdc-deprecated-list-item__text {
29+
align-self: flex-start;
30+
}
31+
}
1232
}
1333

1434
// .mdc-deprecated-list-item__primary-text adds its own margin settings, so only reset if it doesn't
@@ -24,25 +44,6 @@
2444
}
2545

2646

27-
// MDC does have 2-line support, but it's a per-list setting, whereas ours applies to individual
28-
// items. Therefore, we need to add our own styles.
29-
.mat-mdc-2-line {
30-
height: 72px;
31-
32-
.mdc-deprecated-list-item__text {
33-
align-self: flex-start;
34-
}
35-
}
36-
37-
// MDC does not support more than 2 lines, so we need to add our own styles.
38-
.mat-mdc-3-line {
39-
height: 88px;
40-
41-
.mdc-deprecated-list-item__text {
42-
align-self: flex-start;
43-
}
44-
}
45-
4647
// MDC supports avatars, but it's a per-list setting, whereas ours applies to individual
4748
// items. Therefore, we need to add our own styles.
4849
.mat-mdc-list-avatar {
@@ -51,9 +52,12 @@
5152
$size: 40px;
5253
$margin-value: 72px - mdc-list.$deprecated-side-padding - $size;
5354

54-
width: $size;
55-
height: $size;
56-
border-radius: 50%;
55+
// These styles need the extra specificity in order to override the ones from MDC.
56+
.mat-mdc-list-base & {
57+
width: $size;
58+
height: $size;
59+
border-radius: 50%;
60+
}
5761

5862
// Avatars that come before the text have the .mdc-deprecated-list-item__graphic class.
5963
// MDC's .mdc-deprecated-list--avatar-list class would normally apply overrides to set the

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import {MatListBase, MatListItemBase} from './list-base';
2929
@Component({
3030
selector: 'mat-list',
3131
exportAs: 'matList',
32-
template: '<ng-content></ng-content>',
32+
template: '<div class="mdc-deprecated-list"><ng-content></ng-content></div>',
3333
host: {
34-
'class': 'mat-mdc-list mat-mdc-list-base mdc-deprecated-list',
34+
'class': 'mat-mdc-list mat-mdc-list-base',
3535
},
3636
styleUrls: ['list.css'],
3737
encapsulation: ViewEncapsulation.None,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {MatListBase} from './list-base';
1212
@Component({
1313
selector: 'mat-nav-list',
1414
exportAs: 'matNavList',
15-
template: '<ng-content></ng-content>',
15+
template: '<div class="mdc-deprecated-list"><ng-content></ng-content></div>',
1616
host: {
17-
'class': 'mat-mdc-nav-list mat-mdc-list-base mdc-deprecated-list',
17+
'class': 'mat-mdc-nav-list mat-mdc-list-base',
1818
},
1919
styleUrls: ['list.css'],
2020
encapsulation: ViewEncapsulation.None,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ export class MatSelectionListChange {
6060
selector: 'mat-selection-list',
6161
exportAs: 'matSelectionList',
6262
host: {
63-
'class': 'mat-mdc-selection-list mat-mdc-list-base mdc-deprecated-list',
63+
'class': 'mat-mdc-selection-list mat-mdc-list-base',
6464
'role': 'listbox',
6565
'[attr.aria-multiselectable]': 'multiple',
6666
},
67-
template: '<ng-content></ng-content>',
67+
template: '<div class="mdc-deprecated-list"><ng-content></ng-content></div>',
6868
styleUrls: ['list.css'],
6969
encapsulation: ViewEncapsulation.None,
7070
providers: [

0 commit comments

Comments
 (0)