Skip to content

Commit 7013946

Browse files
committed
refactor(material-experimental/mdc-chips): reduce specificity and bundle size
Reworks the theme of the MDC-based chips to produce less specific and more compact CSS.
1 parent 5564269 commit 7013946

File tree

3 files changed

+30
-87
lines changed

3 files changed

+30
-87
lines changed

src/dev-app/chips/chips-demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ <h4>With avatar and icons</h4>
7070
</mat-chip>
7171

7272
<mat-chip>
73-
<img src="https://material.angularjs.org/material2_assets/ngconf/Mal.png" matChipAvatar>
73+
<img src="https://material.angular.io/assets/img/examples/shiba2.jpg" matChipAvatar>
7474
Mal
7575
</mat-chip>
7676

7777
<mat-chip selected="true" color="warn">
78-
<img src="https://material.angularjs.org/material2_assets/ngconf/Husi.png" matChipAvatar>
78+
<img src="https://material.angular.io/assets/img/examples/shiba2.jpg" matChipAvatar>
7979
Husi
8080
<button matChipRemove>
8181
<mat-icon>cancel</mat-icon>

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ <h4>With avatar, icons, and color</h4>
5555
</mat-chip>
5656

5757
<mat-chip>
58-
<img src="https://material.angularjs.org/material2_assets/ngconf/Mal.png" matChipAvatar>
58+
<img src="https://material.angular.io/assets/img/examples/shiba2.jpg" matChipAvatar>
5959
Mal
6060
</mat-chip>
6161

6262
<mat-chip highlighted="true" color="warn">
63-
<img src="https://material.angularjs.org/material2_assets/ngconf/Husi.png" matChipAvatar>
63+
<img src="https://material.angular.io/assets/img/examples/shiba2.jpg" matChipAvatar>
6464
Husi
6565
<button matChipRemove>
6666
<mat-icon>cancel</mat-icon>

src/material-experimental/mdc-chips/_chips-theme.scss

+26-83
Original file line numberDiff line numberDiff line change
@@ -9,95 +9,35 @@
99
@use '../../material/core/typography/typography';
1010
@use '../../material/core/theming/theming';
1111

12-
@mixin _base-chip($is-dark) {
13-
$on-surface-state-content: if($is-dark, mdc-color-palette.$grey-50, mdc-color-palette.$grey-900);
14-
$surface:
15-
color.mix(mdc-theme-color.prop-value(on-surface), mdc-theme-color.prop-value(surface), 12%);
16-
17-
// TODO(crisbeto): ideally the $label-text-color would be something along the lines of:
18-
// `if($is-dark, mdc-color-palette.$grey-100, mdc-color-palette.$grey-800)`, because it allows
19-
// the user to distinguish between when the primary action is focused versus the remove icon.
20-
// The problem is that the color contrast is worse and there are a lot of Google screenshot tests
21-
// that expect the darker color.
22-
$label-text-color: $on-surface-state-content;
23-
24-
@include mdc-chip-theme.theme-styles((
25-
flat-focus-outline-color: $on-surface-state-content,
26-
flat-unselected-focus-outline-color: $on-surface-state-content,
27-
focus-label-text-color: $on-surface-state-content,
28-
focus-outline-color: $on-surface-state-content,
29-
hover-label-text-color: $on-surface-state-content,
30-
pressed-label-text-color: $on-surface-state-content,
31-
unselected-focus-label-text-color: $on-surface-state-content,
32-
unselected-hover-label-text-color: $on-surface-state-content,
33-
unselected-pressed-label-text-color: $on-surface-state-content,
34-
with-trailing-icon-trailing-icon-color: $on-surface-state-content,
35-
with-leading-icon-leading-icon-color: $on-surface-state-content,
36-
37-
elevated-disabled-container-color: $surface,
38-
elevated-container-color: $surface,
39-
elevated-unselected-container-color: $surface,
40-
41-
with-icon-unselected-icon-color: $label-text-color,
42-
unselected-label-text-color: $label-text-color,
43-
label-text-color: $label-text-color,
44-
disabled-label-text-color: $label-text-color,
45-
));
46-
47-
// We should be able to customize this using the `with-icon-selected-disabled-icon-color` key,
48-
// but the MDC validation blocks us, even though they have a lookup for it.
49-
@include mdc-chip-theme.checkmark-color((disabled: $label-text-color));
12+
// Customizes the appearance of a chip. Note that ideally we would be doing this using the
13+
// `theme-styles` mixin, however it has the following problems:
14+
// 1. Some of MDC's base styles have **very** high specificity. E.g. setting the background of a
15+
// non-selected, enabled chip uses a selector like `.chip:not(.selected):not(.disabled)` instead of
16+
// just `.chip`. This specificity increase has a ripple effect over all other components that are
17+
// built on top of ours, making overrides extremely difficult and brittle.
18+
// 2. Including the individual mixins allows us to avoid a lot of unnecessary CSS (~35kb in the
19+
// dev app theme).
20+
@mixin _chip-variant($background, $foreground) {
21+
@include mdc-chip-theme.container-color($background);
22+
@include mdc-chip-theme.icon-color($foreground);
23+
@include mdc-chip-theme.trailing-action-color($foreground);
24+
@include mdc-chip-theme.checkmark-color($foreground);
25+
@include mdc-chip-theme.text-label-color($foreground);
26+
27+
// Technically the avatar is only supposed to have an image, but we also allow for icons.
28+
// Set the color so the icons inherit the correct color.
29+
.mat-mdc-chip-avatar {
30+
color: $foreground;
31+
}
5032
}
5133

5234
@mixin _colored-chip($palette) {
5335
$background: theming.get-color-from-palette($palette);
5436
$foreground: theming.get-color-from-palette($palette, default-contrast);
5537

56-
&.mat-mdc-chip-selected {
57-
@include mdc-chip-theme.theme-styles((
58-
elevated-selected-container-color: $background,
59-
flat-selected-container-color: $background,
60-
61-
selected-focus-label-text-color: $foreground,
62-
selected-hover-label-text-color: $foreground,
63-
selected-pressed-label-text-color: $foreground,
64-
with-icon-selected-focus-icon-color: $foreground,
65-
with-icon-selected-hover-icon-color: $foreground,
66-
with-icon-selected-pressed-icon-color: $foreground,
67-
with-trailing-icon-trailing-icon-color: $foreground,
68-
69-
with-icon-focus-icon-color: $foreground,
70-
with-icon-hover-icon-color: $foreground,
71-
with-icon-pressed-icon-color: $foreground,
72-
73-
selected-label-text-color: $foreground,
74-
with-icon-selected-icon-color: $foreground,
75-
76-
// TODO: the previous styles preserved the color when disabled and selected. It seems like
77-
// this could be supported by passing in `selected-disabled-label-text-color`, but the
78-
// theme key validation rejects it.
79-
));
80-
}
81-
82-
38+
&.mat-mdc-chip-selected,
8339
&.mat-mdc-chip-highlighted {
84-
$icon-color: $foreground;
85-
$icon-map: (
86-
enabled: $icon-color,
87-
disabled: $icon-color,
88-
hover: $icon-color,
89-
focus: $icon-color
90-
);
91-
92-
@include mdc-chip-theme.container-color($background);
93-
@include mdc-chip-theme.icon-color($icon-map);
94-
@include mdc-chip-theme.trailing-action-color($icon-map);
95-
@include mdc-chip-theme.text-label-color((
96-
enabled: $foreground,
97-
disabled: $foreground,
98-
hover: $foreground,
99-
focus: $foreground
100-
));
40+
@include _chip-variant($background, $foreground);
10141
}
10242
}
10343

@@ -111,7 +51,10 @@
11151

11252
@include mdc-helpers.mat-using-mdc-theme($config) {
11353
.mat-mdc-standard-chip {
114-
@include _base-chip($is-dark);
54+
@include _chip-variant(
55+
color.mix(mdc-theme-color.prop-value(on-surface), mdc-theme-color.prop-value(surface), 12%),
56+
if($is-dark, mdc-color-palette.$grey-50, mdc-color-palette.$grey-900)
57+
);
11558

11659
&.mat-primary {
11760
@include _colored-chip($primary);

0 commit comments

Comments
 (0)