Skip to content

Commit 55e4f86

Browse files
committed
separate the "floating" logic from the "empty" logic
1 parent 7f9e218 commit 55e4f86

File tree

7 files changed

+42
-42
lines changed

7 files changed

+42
-42
lines changed

src/lib/form-field/_form-field-theme.scss

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Placeholder colors. Required is used for the `*` star shown in the placeholder.
1616
$placeholder-color: mat-color($foreground, secondary-text);
17-
$floating-placeholder-color: mat-color($primary);
17+
$focused-placeholder-color: mat-color($primary);
1818
$required-placeholder-color: mat-color($accent);
1919

2020
// Underline colors.
@@ -38,7 +38,7 @@
3838
}
3939

4040
.mat-focused .mat-form-field-placeholder {
41-
color: $floating-placeholder-color;
41+
color: $focused-placeholder-color;
4242

4343
&.mat-accent {
4444
color: $underline-color-accent;
@@ -49,11 +49,8 @@
4949
}
5050
}
5151

52-
.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-placeholder,
53-
.mat-focused .mat-form-field-placeholder.mat-form-field-float {
54-
.mat-form-field-required-marker {
55-
color: $required-placeholder-color;
56-
}
52+
.mat-focused .mat-form-field-required-marker {
53+
color: $required-placeholder-color;
5754
}
5855

5956
.mat-form-field-underline {
@@ -84,7 +81,7 @@
8481
color: $underline-color-warn;
8582

8683
&.mat-accent,
87-
&.mat-form-field-float .mat-form-field-required-marker {
84+
.mat-form-field-required-marker {
8885
color: $underline-color-warn;
8986
}
9087
}
@@ -184,8 +181,10 @@
184181
border-top: $infix-margin-top solid transparent;
185182
}
186183

187-
.mat-form-field-autofill-control {
188-
&:-webkit-autofill + .mat-form-field-placeholder-wrapper .mat-form-field-float {
184+
.mat-form-field-can-float {
185+
&.mat-form-field-should-float .mat-form-field-placeholder,
186+
.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-placeholder-wrapper
187+
.mat-form-field-placeholder {
189188
@include _mat-form-field-placeholder-floating(
190189
$subscript-font-scale, $infix-padding, $infix-margin-top);
191190
}
@@ -198,13 +197,6 @@
198197

199198
.mat-form-field-placeholder {
200199
top: $infix-margin-top + $infix-padding;
201-
202-
// Show the placeholder above the control when it's not empty, or focused.
203-
&.mat-form-field-float:not(.mat-form-field-empty),
204-
.mat-focused &.mat-form-field-float {
205-
@include _mat-form-field-placeholder-floating($subscript-font-scale,
206-
$infix-padding, $infix-margin-top);
207-
}
208200
}
209201

210202
.mat-form-field-underline {

src/lib/form-field/form-field-control.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export abstract class MdFormFieldControl<T> {
3636
/** Whether the control is empty. */
3737
readonly empty: boolean;
3838

39+
/** Whether the `MdFormField` label should try to float. */
40+
readonly shouldPlaceholderFloat: boolean;
41+
3942
/** Whether the control is required. */
4043
readonly required: boolean;
4144

src/lib/form-field/form-field.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
[attr.aria-owns]="_control.id"
1616
[class.mat-empty]="_control.empty && !_shouldAlwaysFloat"
1717
[class.mat-form-field-empty]="_control.empty && !_shouldAlwaysFloat"
18-
[class.mat-float]="_canPlaceholderFloat"
19-
[class.mat-form-field-float]="_canPlaceholderFloat"
2018
[class.mat-accent]="color == 'accent'"
2119
[class.mat-warn]="color == 'warn'"
2220
#placeholder

src/lib/form-field/form-field.scss

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,6 @@ $mat-form-field-underline-height: 1px !default;
6262
min-width: 0;
6363
}
6464

65-
// Pseudo-class for Chrome and Safari auto-fill to move the placeholder to the floating position.
66-
// This is necessary because these browsers do not actually fire any events when a form auto-fill is
67-
// occurring. Once the autofill is committed, a change event happen and the regular md-form-field
68-
// classes take over to fulfill this behaviour. Assumes the autofill is non-empty.
69-
.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-placeholder-wrapper {
70-
// The control is still technically empty at this point, so we need to hide non-floating
71-
// placeholders to prevent overlapping with the autofilled value.
72-
.mat-form-field-placeholder {
73-
display: none;
74-
}
75-
76-
.mat-form-field-float {
77-
display: block;
78-
transition: none;
79-
}
80-
}
81-
8265
// Used to hide the placeholder overflow on IE, since IE doesn't take transform into account when
8366
// determining overflow.
8467
.mat-form-field-placeholder-wrapper {
@@ -121,19 +104,35 @@ $mat-form-field-underline-height: 1px !default;
121104
// Hide the placeholder initially, and only show it when it's floating or the control is empty.
122105
display: none;
123106

124-
&.mat-form-field-empty,
125-
&.mat-form-field-float:not(.mat-form-field-empty),
126-
.mat-focused &.mat-form-field-float {
127-
display: block;
128-
}
129-
130107
[dir='rtl'] & {
131108
transform-origin: 100% 0;
132109
left: auto;
133110
right: 0;
134111
}
135112
}
136113

114+
.mat-form-field-empty.mat-form-field-placeholder,
115+
.mat-form-field-can-float.mat-form-field-should-float .mat-form-field-placeholder {
116+
display: block;
117+
}
118+
119+
// Pseudo-class for Chrome and Safari auto-fill to move the placeholder to the floating position.
120+
// This is necessary because these browsers do not actually fire any events when a form auto-fill is
121+
// occurring. Once the autofill is committed, a change event happen and the regular md-form-field
122+
// classes take over to fulfill this behaviour.
123+
.mat-form-field-autofill-control:-webkit-autofill + .mat-form-field-placeholder-wrapper
124+
.mat-form-field-placeholder {
125+
// The form field will be considered empty if it is autofilled, and therefore the placeholder will
126+
// be shown. Therefore we need to override it to hidden...
127+
display: none;
128+
129+
// ...and re-show it only if it's able to float.
130+
.mat-form-field-can-float & {
131+
display: block;
132+
transition: none;
133+
}
134+
}
135+
137136
// Disable the placeholder animation when the control is not empty (this prevents placeholder
138137
// animating up when the value is set programmatically).
139138
.mat-form-field-placeholder:not(.mat-form-field-empty) {

src/lib/form-field/form-field.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ let nextUniqueId = 0;
7272
'class': 'mat-input-container mat-form-field',
7373
'[class.mat-input-invalid]': '_control.errorState',
7474
'[class.mat-form-field-invalid]': '_control.errorState',
75+
'[class.mat-form-field-can-float]': '_canPlaceholderFloat',
76+
'[class.mat-form-field-should-float]': '_control.shouldPlaceholderFloat || _shouldAlwaysFloat',
7577
'[class.mat-focused]': '_control.focused',
7678
'[class.mat-primary]': 'color == "primary"',
7779
'[class.mat-accent]': 'color == "accent"',

src/lib/input/input.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ export class MdInput implements MdFormFieldControl<any>, OnChanges, OnDestroy, D
280280
!this._isBadInput();
281281
}
282282

283+
// Implemented as part of MdFormFieldControl.
284+
get shouldPlaceholderFloat(): boolean { return this.focused || !this.empty; }
285+
283286
// Implemented as part of MdFormFieldControl.
284287
setDescribedByIds(ids: string[]) { this._ariaDescribedby = ids.join(' '); }
285288

src/lib/select/select.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,9 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
11791179
this.focus();
11801180
this.open();
11811181
}
1182+
1183+
// Implemented as part of MdFormFieldControl.
1184+
get shouldPlaceholderFloat() { return !this.empty; }
11821185
}
11831186

11841187
/** Clamps a value n between min and max values. */

0 commit comments

Comments
 (0)