Skip to content

Commit d29fb66

Browse files
crisbetotinayuangao
authored andcommitted
fix(pseudo-checkbox): simplify theming (#6082)
Currently the pseudo checkbox both inherits its colors via CSS and has a `color` input. This means that if we wanted to the keep it in sync with its parent component (e.g. a multi-select), we'd have to add some boilerplate that listens to changes, goes through and updates all of the checkboxes, and triggers change detection while avoiding the "changed after check" assertions. Since the component is meant to be really lean and to inherit its colors from the parent, these changes remove the `color` input altogether in order to reduce the complexity and be clearer regarding the intended usage. Since the same is true for the `md-option` component, I've removed the `md-options.{{ palette }}` selectors as well. Fixes #6079.
1 parent 9ac5d13 commit d29fb66

File tree

4 files changed

+30
-45
lines changed

4 files changed

+30
-45
lines changed

src/lib/core/option/_option-theme.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
background: mat-color($background, hover);
1717
}
1818

19-
&.mat-selected.mat-primary, .mat-primary &.mat-selected {
19+
.mat-primary &.mat-selected {
2020
color: mat-color($primary);
2121
}
2222

23-
&.mat-selected.mat-accent, .mat-accent &.mat-selected {
23+
.mat-accent &.mat-selected {
2424
color: mat-color($accent);
2525
}
2626

27-
&.mat-selected.mat-warn, .mat-warn &.mat-selected {
27+
.mat-warn &.mat-selected {
2828
color: mat-color($warn);
2929
}
3030

src/lib/core/option/option.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<span [ngSwitch]="_isCompatibilityMode" *ngIf="multiple">
22
<mat-pseudo-checkbox class="mat-option-pseudo-checkbox" *ngSwitchCase="true"
3-
[state]="selected ? 'checked' : ''" color="primary"></mat-pseudo-checkbox>
3+
[state]="selected ? 'checked' : ''"></mat-pseudo-checkbox>
44
<md-pseudo-checkbox class="mat-option-pseudo-checkbox" *ngSwitchDefault
5-
[state]="selected ? 'checked' : ''" color="primary"></md-pseudo-checkbox>
5+
[state]="selected ? 'checked' : ''"></md-pseudo-checkbox>
66
</span>
77

88
<ng-content></ng-content>

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
@import '../../theming/theming';
22

3-
@mixin _mat-pseudo-checkbox-inner-content-theme($theme, $palette-name) {
4-
$palette: map-get($theme, $palette-name);
5-
$color: mat-color($palette);
6-
7-
.mat-pseudo-checkbox-checked.mat-#{$palette-name},
8-
.mat-pseudo-checkbox-indeterminate.mat-#{$palette-name},
9-
.mat-#{$palette-name} .mat-pseudo-checkbox-checked,
10-
.mat-#{$palette-name} .mat-pseudo-checkbox-indeterminate {
11-
background: $color;
12-
}
13-
}
14-
153
@mixin mat-pseudo-checkbox-theme($theme) {
164
$is-dark-theme: map-get($theme, is-dark);
175
$primary: map-get($theme, primary);
@@ -35,11 +23,28 @@
3523
}
3624
}
3725

38-
@include _mat-pseudo-checkbox-inner-content-theme($theme, primary);
39-
@include _mat-pseudo-checkbox-inner-content-theme($theme, accent);
40-
@include _mat-pseudo-checkbox-inner-content-theme($theme, warn);
26+
// Default to the accent color. Note that the pseudo checkboxes are meant to inherit the
27+
// theme from their parent, rather than implementing their own theming, which is why we
28+
// don't attach to the `mat-*` classes.
29+
.mat-pseudo-checkbox-checked,
30+
.mat-pseudo-checkbox-indeterminate,
31+
.mat-accent .mat-pseudo-checkbox-checked,
32+
.mat-accent .mat-pseudo-checkbox-indeterminate {
33+
background: mat-color(map-get($theme, accent));
34+
}
35+
36+
.mat-primary .mat-pseudo-checkbox-checked,
37+
.mat-primary .mat-pseudo-checkbox-indeterminate {
38+
background: mat-color(map-get($theme, primary));
39+
}
40+
41+
.mat-warn .mat-pseudo-checkbox-checked,
42+
.mat-warn .mat-pseudo-checkbox-indeterminate {
43+
background: mat-color(map-get($theme, warn));
44+
}
4145

42-
.mat-pseudo-checkbox-checked, .mat-pseudo-checkbox-indeterminate {
46+
.mat-pseudo-checkbox-checked,
47+
.mat-pseudo-checkbox-indeterminate {
4348
&.mat-pseudo-checkbox-disabled {
4449
background: $disabled-color;
4550
}

src/lib/core/selection/pseudo-checkbox/pseudo-checkbox.ts

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

9-
import {
10-
Component,
11-
ViewEncapsulation,
12-
Input,
13-
ElementRef,
14-
Renderer2,
15-
ChangeDetectionStrategy,
16-
} from '@angular/core';
17-
import {CanColor, mixinColor} from '../../common-behaviors/color';
9+
import {Component, ViewEncapsulation, Input, ChangeDetectionStrategy} from '@angular/core';
1810

1911
export type MdPseudoCheckboxState = 'unchecked' | 'checked' | 'indeterminate';
2012

21-
22-
// Boilerplate for applying mixins to MdChip.
23-
/** @docs-private */
24-
export class MdPseudoCheckboxBase {
25-
constructor(public _renderer: Renderer2, public _elementRef: ElementRef) {}
26-
}
27-
export const _MdPseudoCheckboxBase = mixinColor(MdPseudoCheckboxBase, 'accent');
28-
29-
3013
/**
3114
* Component that shows a simplified checkbox without including any kind of "real" checkbox.
3215
* Meant to be used when the checkbox is purely decorative and a large number of them will be
3316
* included, such as for the options in a multi-select. Uses no SVGs or complex animations.
17+
* Note that theming is meant to be handled by the parent element, e.g.
18+
* `mat-primary .mat-pseudo-checkbox`.
3419
*
3520
* Note that this component will be completely invisible to screen-reader users. This is *not*
3621
* interchangeable with <md-checkbox> and should *not* be used if the user would directly interact
@@ -44,7 +29,6 @@ export const _MdPseudoCheckboxBase = mixinColor(MdPseudoCheckboxBase, 'accent');
4429
changeDetection: ChangeDetectionStrategy.OnPush,
4530
selector: 'md-pseudo-checkbox, mat-pseudo-checkbox',
4631
styleUrls: ['pseudo-checkbox.css'],
47-
inputs: ['color'],
4832
template: '',
4933
host: {
5034
'class': 'mat-pseudo-checkbox',
@@ -53,14 +37,10 @@ export const _MdPseudoCheckboxBase = mixinColor(MdPseudoCheckboxBase, 'accent');
5337
'[class.mat-pseudo-checkbox-disabled]': 'disabled',
5438
},
5539
})
56-
export class MdPseudoCheckbox extends _MdPseudoCheckboxBase implements CanColor {
40+
export class MdPseudoCheckbox {
5741
/** Display state of the checkbox. */
5842
@Input() state: MdPseudoCheckboxState = 'unchecked';
5943

6044
/** Whether the checkbox is disabled. */
6145
@Input() disabled: boolean = false;
62-
63-
constructor(elementRef: ElementRef, renderer: Renderer2) {
64-
super(renderer, elementRef);
65-
}
6646
}

0 commit comments

Comments
 (0)