Skip to content

Commit 5b57863

Browse files
authored
fix(material/list): align color scheme between single and multi selection list (#26074)
MDC has the `mdc-list-item--selected` class which we were setting only on single selection `mat-list-option` to indicate using color that they're selected. After the recent change to indicate selection using a radio button, these look weird because the radio button inherits the list item theme palette while the background of the item stays as `primary`. These changes resolve the issue by treating single selection and multi-selection lists the same. **Note:** an alternative I was considering was to change the color so that it matches the radio button color, but then the appearance of the single selection list would be inconsistent with the multi-selection list.
1 parent f99af6d commit 5b57863

File tree

3 files changed

+3
-25
lines changed

3 files changed

+3
-25
lines changed

src/material/list/_interactive-list-theme.scss

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
@use 'sass:map';
22
@use '@material/ripple' as mdc-ripple;
3-
@use '../core/theming/theming';
43

54
// Mixin that provides colors for the various states of an interactive list-item. MDC
65
// has integrated styles for these states but relies on their complex ripples for it.
76
@mixin private-interactive-list-item-state-colors($config) {
87
$is-dark-theme: map.get($config, is-dark);
98
$active-base-color: if($is-dark-theme, white, black);
10-
$selected-color: theming.get-color-from-palette(map.get($config, primary));
119

1210
.mat-mdc-list-item-interactive {
1311
&::before {
@@ -18,21 +16,6 @@
1816
opacity: mdc-ripple.states-opacity($active-base-color, hover);
1917
}
2018

21-
&.mdc-list-item--selected {
22-
&::before {
23-
background: $selected-color;
24-
opacity: mdc-ripple.states-opacity($selected-color, selected);
25-
}
26-
27-
&:not(:focus):not(.mdc-list-item--disabled):hover::before {
28-
// The hover and selected opacities need to be combined to match with what the MDC
29-
// ripple state would render. More details here:
30-
// https://github.com/material-components/material-components-web/blob/348665978ce73694ad4518626dd70cdf5b984113/packages/mdc-ripple/_ripple-theme.scss#L450.
31-
opacity: mdc-ripple.states-opacity($selected-color, hover) +
32-
mdc-ripple.states-opacity($selected-color, selected);
33-
}
34-
}
35-
3619
&:focus::before {
3720
opacity: mdc-ripple.states-opacity($active-base-color, focus);
3821
}

src/material/list/list-option.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ export interface SelectionList extends MatListBase {
6464
host: {
6565
'class': 'mat-mdc-list-item mat-mdc-list-option mdc-list-item',
6666
'role': 'option',
67-
// As per MDC, only list items in single selection mode should receive the `--selected`
68-
// class. For multi selection, the checkbox is used as indicator.
69-
'[class.mdc-list-item--selected]': 'selected && !_selectionList.multiple',
7067
// Based on the checkbox/radio position and whether there are icons or avatars, we apply MDC's
7168
// list-item `--leading` and `--trailing` classes.
7269
'[class.mdc-list-item--with-leading-avatar]': '_hasProjected("avatars", "before")',

src/material/list/selection-list.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,16 +1041,14 @@ describe('MDC-based MatSelectionList without forms', () => {
10411041
fixture.detectChanges();
10421042

10431043
expect(selectList.selected).toEqual([testListItem1]);
1044-
expect(listOptions[1].nativeElement.classList.contains('mdc-list-item--selected')).toBe(true);
1044+
expect(listOptions[1].nativeElement.getAttribute('aria-selected')).toBe('true');
10451045

10461046
dispatchMouseEvent(testListItem2._hostElement, 'click');
10471047
fixture.detectChanges();
10481048

10491049
expect(selectList.selected).toEqual([testListItem2]);
1050-
expect(listOptions[1].nativeElement.classList.contains('mdc-list-item--selected')).toBe(
1051-
false,
1052-
);
1053-
expect(listOptions[2].nativeElement.classList.contains('mdc-list-item--selected')).toBe(true);
1050+
expect(listOptions[1].nativeElement.getAttribute('aria-selected')).toBe('false');
1051+
expect(listOptions[2].nativeElement.getAttribute('aria-selected')).toBe('true');
10541052
});
10551053

10561054
it('should not show check boxes', () => {

0 commit comments

Comments
 (0)