Skip to content

Commit 9c45770

Browse files
committed
fix(button-toggle): use solid border color
Usually the theme divider color is rgba, which means that it can look differently, depending on the color behind it. As a result, the border of a selected button toggle is different from a deselected one, because its background color is darker. These changes switch to using a solid color to ensure that we have always have a consistent border. These changes also add a new theming utility function that converts an rgba color to hex, if the consumer knows the background. We've been using it in a couple of places already, but now it's being moved out into a reusable function.
1 parent f0c7a25 commit 9c45770

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/material/badge/_badge-theme.scss

+4-5
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,16 @@ $mat-badge-large-size: $mat-badge-default-size + 6;
132132

133133
.mat-badge-disabled {
134134
.mat-badge-content {
135-
$app-background: mat-color($background, 'background');
136-
$badge-color: mat-color($foreground, disabled-button);
137-
138135
// The disabled color usually has some kind of opacity, but because the badge is overlayed
139136
// on top of something else, it won't look good if it's opaque. If it is a color *type*,
140137
// we convert it into a solid color by taking the opacity from the rgba value and using
141138
// the value to determine the percentage of the background to put into foreground when
142139
// mixing the colors together.
140+
$badge-color: mat-color($foreground, disabled-button);
141+
$app-background: mat-color($background, 'background');
142+
143143
@if (type-of($badge-color) == color and type-of($app-background) == color) {
144-
$badge-opacity: opacity($badge-color);
145-
background: mix($app-background, rgba($badge-color, 1), (1 - $badge-opacity) * 100%);
144+
background: mat-rgba-to-hex($badge-color, $app-background);
146145
}
147146
@else {
148147
background: $badge-color;

src/material/button-toggle/_button-toggle-theme.scss

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111
$config: mat-get-color-config($config-or-theme);
1212
$foreground: map-get($config, foreground);
1313
$background: map-get($config, background);
14-
$divider-color: mat-color($foreground, divider);
14+
$theme-divider-color: mat-color($foreground, divider);
15+
16+
// By default the theme usually has an rgba color for the dividers, which can
17+
// stack up with the background of a button toggle. This can cause the border
18+
// of a selected toggle to look different from an deselected one. We use a solid
19+
// color to ensure that the border always stays the same.
20+
$divider-color: if(type-of($theme-divider-color) == color,
21+
mat-rgba-to-hex($theme-divider-color, mat-color($background, card)),
22+
$theme-divider-color
23+
);
1524

1625
.mat-button-toggle-standalone,
1726
.mat-button-toggle-group {

src/material/core/theming/_theming.scss

+9
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,12 @@ $_mat-theme-generate-default-density: true !default;
290290
color: $theme-or-color-config
291291
));
292292
}
293+
294+
295+
// Approximates an rgba color into a solid hex color, given a background color.
296+
@function mat-rgba-to-hex($color, $background-color) {
297+
// We convert the rgba color into a solid one by taking the opacity from the rgba
298+
// value and using it to determine the percentage of the background to put
299+
// into foreground when mixing the colors together.
300+
@return mix($background-color, rgba($color, 1), (1 - opacity($color)) * 100%);
301+
}

src/material/sort/_sort-theme.scss

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
$foreground: map-get($config, foreground);
88

99
.mat-sort-header-arrow {
10-
$table-background: mat-color($background, 'card');
11-
$text-color: mat-color($foreground, secondary-text);
12-
1310
// Because the arrow is made up of multiple elements that are stacked on top of each other,
1411
// we can't use the semi-transparent color from the theme directly. If the value is a color
1512
// *type*, we convert it into a solid color by taking the opacity from the rgba value and
1613
// using the value to determine the percentage of the background to put into foreground
1714
// when mixing the colors together. Otherwise, if it resolves to something different
1815
// (e.g. it resolves to a CSS variable), we use the color directly.
16+
$text-color: mat-color($foreground, secondary-text);
17+
$table-background: mat-color($background, 'card');
18+
1919
@if (type-of($table-background) == color and type-of($text-color) == color) {
20-
$text-opacity: opacity($text-color);
21-
color: mix($table-background, rgba($text-color, 1), (1 - $text-opacity) * 100%);
20+
color: mat-rgba-to-hex($text-color, $table-background);
2221
}
2322
@else {
2423
color: $text-color;

0 commit comments

Comments
 (0)