Skip to content

Commit 5852fcb

Browse files
devversionandrewseguin
authored andcommitted
fix(material-experimental/mdc-form-filed): missing focus and hover effect for fill
We recently updated to the latest canary version of MDC, but did not account for a new breaking change to the MDC text-field that required us to explicitly specify a `ripple` container element. material-components/material-components-web@c541ebe#diff-fa38508bb43a471ee25b746d85527fb5 Thinking more about the ripple styles in the MDC text-field unveiled that we should just provide our own implementation of the focus overlay since MDC ripples are too much bloat for the simple case of showing a focus and hover effect. We don't want to create a `MdcRipple` runtime instance, nor do we want to include CSS selectors for interaction ripples which are not used in the text-field at all.
1 parent 440ca6c commit 5852fcb

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

src/material-experimental/mdc-form-field/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ sass_library(
8282
name = "form_field_partials",
8383
srcs = [
8484
"_form-field-bottom-line.scss",
85+
"_form-field-focus-overlay.scss",
8586
"_form-field-sizing.scss",
8687
"_form-field-subscript.scss",
8788
"_mdc-text-field-structure-overrides.scss",
8889
"_mdc-text-field-textarea-overrides.scss",
8990
],
9091
deps = [
9192
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",
93+
"//src/material/core:core_scss_lib",
9294
],
9395
)
9496

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@use '@material/ripple/functions' as ripple-functions;
2+
@import '@material/textfield/mixins.import';
3+
@import '../../material/core/style/layout-common';
4+
5+
// MDC text-field uses `@material/ripple` in order to show a focus and hover effect for
6+
// text-fields. This is unnecessary because the ripples bring in a lot of unnecessary
7+
// styles and runtime code while the actual goal for the text-field is simply showing a
8+
// focus and hover effect. The ripples will unnecessarily provide CSS selectors and JS
9+
// runtime code for launching interaction ripples at pointer location. This is not needed
10+
// for the text-field, so we create our own minimal focus overlay styles. Our focus overlay
11+
// uses the exact same logic to compute the colors as in the default MDC text-field ripples.
12+
@mixin _mat-form-field-focus-overlay() {
13+
.mat-mdc-form-field-focus-overlay {
14+
@include mat-fill;
15+
opacity: 0;
16+
}
17+
}
18+
19+
@mixin _mat-form-field-focus-overlay-theme() {
20+
$focus-opacity: ripple-functions.states-opacity($mdc-text-field-ink-color, focus);
21+
$hover-opacity: ripple-functions.states-opacity($mdc-text-field-ink-color, hover);
22+
23+
.mat-mdc-form-field-focus-overlay {
24+
background-color: $mdc-text-field-ink-color;
25+
}
26+
27+
.mat-mdc-form-field:hover .mat-mdc-form-field-focus-overlay {
28+
opacity: $hover-opacity;
29+
}
30+
31+
.mat-mdc-form-field.mat-focused .mat-mdc-form-field-focus-overlay {
32+
opacity: $focus-opacity;
33+
}
34+
}

src/material-experimental/mdc-form-field/_mdc-form-field.scss

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@import '../mdc-helpers/mdc-helpers';
66
@import 'form-field-subscript';
77
@import 'form-field-bottom-line';
8+
@import 'form-field-focus-overlay';
89
@import 'mdc-text-field-theme-variable-refresh';
910

1011
// Mixin that overwrites the default MDC text-field color styles to be based on
@@ -34,12 +35,13 @@
3435
@mixin mat-form-field-theme-mdc($theme) {
3536
@include mat-using-mdc-theme($theme) {
3637
@include _mdc-text-field-refresh-theme-variables() {
37-
@include mdc-text-field-core-styles($query: $mat-theme-styles-query);
38+
@include mdc-text-field-without-ripple($query: $mat-theme-styles-query);
3839
@include mdc-floating-label-core-styles($query: $mat-theme-styles-query);
3940
@include mdc-notched-outline-core-styles($query: $mat-base-styles-query);
4041
@include mdc-line-ripple-core-styles($query: $mat-theme-styles-query);
4142
@include _mat-form-field-subscript-theme();
4243
@include _mat-form-field-bottom-line-theme();
44+
@include _mat-form-field-focus-overlay-theme();
4345

4446
.mat-mdc-form-field.mat-accent {
4547
@include _mdc-text-field-color-styles(secondary);
@@ -48,25 +50,13 @@
4850
.mat-mdc-form-field.mat-warn {
4951
@include _mdc-text-field-color-styles(error);
5052
}
51-
52-
// MDC text-field intends to hide the ripples in the outline appearance. The styles for
53-
// this collide with other styles from the structure styles. This is because the ripples
54-
// are made invisible by using the `mdc-ripple.states-base-color` mixin. The mixin makes the
55-
// ripples `transparent` by generating `content: none` instead. This means that the style
56-
// will collide with the default `content` for ripple pseudo elements. Depending on how
57-
// themes and component styles are inserted, the ripples will not hide properly. To ensure
58-
// that the ripples are not rendered in the outline appearance, we copy the mixin call but
59-
// increase the specificity.
60-
.mat-mdc-text-field-wrapper.mdc-text-field--outlined {
61-
@include mdc-ripple.states-base-color(transparent);
62-
}
6353
}
6454
}
6555
}
6656

6757
@mixin mat-form-field-typography-mdc($config) {
6858
@include mat-using-mdc-typography($config) {
69-
@include mdc-text-field-core-styles($query: $mat-typography-styles-query);
59+
@include mdc-text-field-without-ripple($query: $mat-typography-styles-query);
7060
@include mdc-floating-label-core-styles($query: $mat-typography-styles-query);
7161
@include mdc-text-field-core-styles($query: $mat-typography-styles-query);
7262
@include _mat-form-field-subscript-typography($config);

src/material-experimental/mdc-form-field/form-field.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
[class.mdc-text-field--disabled]="_control.disabled"
2626
[class.mdc-text-field--invalid]="_control.errorState"
2727
(click)="_control.onContainerClick && _control.onContainerClick($event)">
28+
<div class="mat-mdc-form-field-focus-overlay" *ngIf="!_hasOutline()"></div>
2829
<div class="mat-mdc-form-field-flex">
2930
<div *ngIf="_hasOutline()" matFormFieldNotchedOutline
3031
[matFormFieldNotchedOutlineOpen]="_shouldLabelFloat()"

src/material-experimental/mdc-form-field/form-field.scss

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
@import 'form-field-sizing';
44
@import 'form-field-subscript';
55
@import 'form-field-bottom-line';
6+
@import 'form-field-focus-overlay';
67
@import 'mdc-text-field-textarea-overrides';
78
@import 'mdc-text-field-structure-overrides';
89

9-
// Base styles for MDC notched-outline, MDC floating label and MDC text-field.
10+
// Base styles for MDC text-field, notched-outline, floating label and line-ripple.
11+
@include mdc-text-field-without-ripple($query: $mat-base-styles-without-animation-query);
1012
@include mdc-notched-outline-core-styles($query: $mat-base-styles-without-animation-query);
1113
@include mdc-floating-label-core-styles($query: $mat-base-styles-without-animation-query);
12-
@include mdc-text-field-core-styles($query: $mat-base-styles-without-animation-query);
1314
@include mdc-line-ripple-core-styles($query: $mat-base-styles-without-animation-query);
1415

1516
// MDC text-field overwrites.
1617
@include _mat-mdc-text-field-textarea-overrides();
1718
@include _mat-mdc-text-field-structure-overrides();
1819

19-
// Include the subscript and bottom-line styles.
20+
// Include the subscript, bottom-line and focus-overlay styles.
2021
@include _mat-form-field-subscript();
2122
@include _mat-form-field-bottom-line();
23+
@include _mat-form-field-focus-overlay();
2224

2325
// Host element of the form-field. It contains the mdc-text-field wrapper
2426
// and the subscript wrapper.
@@ -57,8 +59,8 @@
5759
// In order to make it possible for developers to disable animations for form-fields,
5860
// we only activate the animation styles if animations are not explicitly disabled.
5961
.mat-mdc-form-field:not(.mat-form-field-no-animations) {
62+
@include mdc-text-field-without-ripple($query: animation);
6063
@include mdc-notched-outline-core-styles($query: animation);
6164
@include mdc-floating-label-core-styles($query: animation);
62-
@include mdc-text-field-core-styles($query: animation);
6365
@include mdc-line-ripple-core-styles($query: animation);
6466
}

0 commit comments

Comments
 (0)