Skip to content

Commit 2cefb69

Browse files
crisbetoandrewseguin
authored andcommitted
fix(list): selection list checkbox theme overwritten by parent theme (#16939)
Fixes the theme of the pseudo checkbox inside the `mat-list-option` being overwritten if the list is placed inside of an element that has a different default theme. The issue comes from the default checkbox styles not being specific enough and the selection list not setting a class for `mat-accent`. Fixes #16891.
1 parent f797cac commit 2cefb69

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/material/core/selection/pseudo-checkbox/_pseudo-checkbox-theme.scss

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@
2727
color: $disabled-color;
2828
}
2929

30+
.mat-primary .mat-pseudo-checkbox-checked,
31+
.mat-primary .mat-pseudo-checkbox-indeterminate {
32+
background: mat-color(map-get($theme, primary));
33+
}
34+
3035
// Default to the accent color. Note that the pseudo checkboxes are meant to inherit the
3136
// theme from their parent, rather than implementing their own theming, which is why we
32-
// don't attach to the `mat-*` classes.
37+
// don't attach to the `mat-*` classes. Also note that this needs to be below `.mat-primary`
38+
// in order to allow for the color to be overwritten if the checkbox is inside a parent that
39+
// has `mat-accent` and is placed inside another parent that has `mat-primary`.
3340
.mat-pseudo-checkbox-checked,
3441
.mat-pseudo-checkbox-indeterminate,
3542
.mat-accent .mat-pseudo-checkbox-checked,
3643
.mat-accent .mat-pseudo-checkbox-indeterminate {
3744
background: mat-color(map-get($theme, accent));
3845
}
3946

40-
.mat-primary .mat-pseudo-checkbox-checked,
41-
.mat-primary .mat-pseudo-checkbox-indeterminate {
42-
background: mat-color(map-get($theme, primary));
43-
}
44-
4547
.mat-warn .mat-pseudo-checkbox-checked,
4648
.mat-warn .mat-pseudo-checkbox-indeterminate {
4749
background: mat-color(map-get($theme, warn));

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ describe('MatSelectionList without forms', () => {
150150
.toBe(true);
151151
});
152152

153+
it('should explicitly set the `accent` color', () => {
154+
const classList = listOptions[0].nativeElement.classList;
155+
156+
fixture.componentInstance.firstOptionColor = 'primary';
157+
fixture.detectChanges();
158+
159+
expect(classList).toContain('mat-primary');
160+
expect(classList).not.toContain('mat-accent');
161+
expect(classList).not.toContain('mat-warn');
162+
163+
fixture.componentInstance.firstOptionColor = 'accent';
164+
fixture.detectChanges();
165+
166+
expect(classList).not.toContain('mat-primary');
167+
expect(classList).toContain('mat-accent');
168+
expect(classList).not.toContain('mat-warn');
169+
});
170+
153171
it('should be able to deselect an option', () => {
154172
let testListItem = listOptions[2].injector.get<MatListOption>(MatListOption);
155173
let selectList =

src/material/list/selection-list.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ export class MatSelectionListChange {
101101
'[class.mat-list-item-with-avatar]': '_avatar || _icon',
102102
// Manually set the "primary" or "warn" class if the color has been explicitly
103103
// set to "primary" or "warn". The pseudo checkbox picks up these classes for
104-
// its theme. The accent theme palette is the default and doesn't need to be set.
104+
// its theme.
105105
'[class.mat-primary]': 'color === "primary"',
106+
// Even though accent is the default, we need to set this class anyway, because the list might
107+
// be placed inside a parent that has one of the other colors with a higher specificity.
108+
'[class.mat-accent]': 'color !== "primary" && color !== "warn"',
106109
'[class.mat-warn]': 'color === "warn"',
107110
'[attr.aria-selected]': 'selected',
108111
'[attr.aria-disabled]': 'disabled',

0 commit comments

Comments
 (0)