Skip to content

Commit 16b65e8

Browse files
authored
fix(input): make input spacing match spec (#4788)
* make input match mocks better, WIP still WIP placeholder working, still WIP fixed main styles, now just themes... themes working again icon button support * add select to baseline demo * fix IE bug * address comments * address comments * remove forced font-size * integrate typography system * switch font-size to inherit * fix non-floating placeholders
1 parent 1d7b752 commit 16b65e8

File tree

6 files changed

+224
-108
lines changed

6 files changed

+224
-108
lines changed

src/demo-app/baseline/baseline-demo.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<input mdInput placeholder="Input" value="Text Input">
1515
</md-input-container>
1616
| Text 5 |
17+
<md-select placeholder="Select">
18+
<md-option value="option">Option</md-option>
19+
</md-select>
20+
| Text 6 |
1721
<md-input-container>
1822
<textarea mdInput placeholder="Input" mdTextareaAutosize>Textarea&#10;Line 2</textarea>
1923
</md-input-container>
@@ -38,6 +42,10 @@ <h1>
3842
<input mdInput placeholder="Input" value="Text Input">
3943
</md-input-container>
4044
| Text 5 |
45+
<md-select placeholder="Select">
46+
<md-option value="option">Option</md-option>
47+
</md-select>
48+
| Text 6 |
4149
<md-input-container>
4250
<textarea mdInput placeholder="Input" mdTextareaAutosize>Textarea&#10;Line 2</textarea>
4351
</md-input-container>

src/demo-app/input/input-demo.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<table style="width: 100%" cellspacing="0"><tr>
1010
<td>
11-
<md-input-container style="width: 100%">
11+
<md-input-container style="width: 100%" floatPlaceholder="never">
1212
<input mdInput placeholder="First name">
1313
</md-input-container>
1414
</td>
@@ -113,6 +113,13 @@ <h4>Icons</h4>
113113
<md-icon mdPrefix>attach_money</md-icon>
114114
<md-icon mdSuffix>mode_edit</md-icon>
115115
</md-input-container>
116+
117+
<h4>Icon buttons</h4>
118+
<md-input-container>
119+
<input mdInput placeholder="amount">
120+
<button md-icon-button mdPrefix><md-icon>attach_money</md-icon></button>
121+
<button md-icon-button mdSuffix><md-icon>mode_edit</md-icon></button>
122+
</md-input-container>
116123
</md-card-content>
117124
</md-card>
118125

src/lib/core/typography/_typography.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
$body-2: mat-typography-level(14px, 24px, 500),
2424
$body-1: mat-typography-level(14px, 20px, 400),
2525
$caption: mat-typography-level(12px, 12px, 400),
26-
$button: mat-typography-level(14px, 14px, 500)
26+
$button: mat-typography-level(14px, 14px, 500),
27+
// Line-height must be unit-less fraction of the font-size.
28+
$input: mat-typography-level(inherit, 1.125, 400)
2729
) {
2830
@return (
2931
font-family: $font-family,
@@ -37,7 +39,8 @@
3739
body-2: $body-2,
3840
body-1: $body-1,
3941
caption: $caption,
40-
button: $button
42+
button: $button,
43+
input: $input,
4144
);
4245
}
4346

@@ -61,7 +64,7 @@
6164
}
6265

6366
.mat-h2, #{$selector} h2 {
64-
@include mat-typography-level-to-styles($config, display-2)
67+
@include mat-typography-level-to-styles($config, display-2);
6568

6669
// Note: The spec doesn't mention letter spacing. The value comes from
6770
// eyeballing it until it looked exactly like the spec examples.;

src/lib/input/_input-theme.scss

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
}
5353

5454
.mat-input-underline {
55-
border-color: $input-underline-color;
55+
background-color: $input-underline-color;
5656
}
5757

5858
.mat-input-ripple {
@@ -91,20 +91,121 @@
9191
}
9292

9393
@mixin mat-input-typography($config) {
94+
// The unit-less line-height from the font config.
95+
$mat-input-line-height: mat-line-height($config, input);
96+
97+
// The amount to scale the font for the floating label and subscript.
98+
$mat-input-subscript-font-scale: 0.75;
99+
// The amount to scale the font for the prefix and suffix icons.
100+
$mat-input-prefix-suffix-icon-font-scale: 1.5;
101+
102+
// The amount of space between the top of the line and the top of the actual text
103+
// (as a fraction of the font-size).
104+
$mat-input-line-spacing: ($mat-input-line-height - 1) / 2;
105+
// The padding on the infix. Mocks show half of the text size, but seem to measure from the edge
106+
// of the text itself, not the edge of the line; therefore we subtract off the line spacing.
107+
$mat-input-infix-padding: 0.5em - $mat-input-line-spacing;
108+
// The margin applied to the input-infix to reserve space for the floating label.
109+
$mat-input-infix-margin-top: 1em * $mat-input-line-height * $mat-input-subscript-font-scale;
110+
// Font size to use for the label and subscript text.
111+
$mat-input-subscript-font-size: $mat-input-subscript-font-scale * 100%;
112+
// Font size to use for the for the prefix and suffix icons.
113+
$mat-input-prefix-suffix-icon-font-size: $mat-input-prefix-suffix-icon-font-scale * 100%;
114+
// The space between the bottom of the input table and the subscript container. Mocks show half of
115+
// the text size, but this margin is applied to an element with the subscript text font size, so
116+
// we need to divide by the scale factor to make it half of the original text size. We again need
117+
// to subtract off the line spacing since the mocks measure to the edge of the text, not the edge
118+
// of the line.
119+
$mat-input-subscript-margin-top:
120+
0.5em / $mat-input-subscript-font-scale - $mat-input-line-spacing;
121+
// The padding applied to the input-wrapper to reserve space for the subscript, since it's
122+
// absolutely positioned. This is a combination of the subscript's margin and line-height, but we
123+
// need to multiply by the subscript font scale factor since the wrapper has a larger font size.
124+
$mat-input-wrapper-padding-bottom:
125+
($mat-input-subscript-margin-top + $mat-input-line-height) * $mat-input-subscript-font-scale;
126+
127+
// Applies a floating placeholder above the input itself.
128+
@mixin mat-input-placeholder-floating {
129+
transform: translateY(-$mat-input-infix-margin-top - $mat-input-infix-padding)
130+
scale($mat-input-subscript-font-scale)
131+
// We use perspecitve to fix the text blurriness as described here:
132+
// http://www.useragentman.com/blog/2014/05/04/fixing-typography-inside-of-2-d-css-transforms/
133+
// This results in a small jitter after the label floats on Firefox, which the
134+
// translateZ fixes.
135+
perspective(100px) translateZ(0.001px);
136+
// The tricks above used to smooth out the animation on chrome and firefox actually make things
137+
// worse on IE, so we don't include them in the IE version.
138+
-ms-transform: translateY(-$mat-input-infix-margin-top - $mat-input-infix-padding)
139+
scale($mat-input-subscript-font-scale);
140+
141+
width: 100% / $mat-input-subscript-font-scale;
142+
}
143+
94144
.mat-input-container {
95-
font-family: mat-font-family($config);
96-
line-height: normal;
145+
@include mat-typography-level-to-styles($config, input);
146+
}
147+
148+
.mat-input-wrapper {
149+
padding-bottom: $mat-input-wrapper-padding-bottom;
150+
}
151+
152+
.mat-input-prefix,
153+
.mat-input-suffix {
154+
// Allow icons in a prefix or suffix to adapt to the correct size.
155+
.mat-icon,
156+
.mat-datepicker-toggle {
157+
font-size: $mat-input-prefix-suffix-icon-font-size;
158+
}
159+
160+
// Allow icon buttons in a prefix or suffix to adapt to the correct size.
161+
.mat-icon-button {
162+
height: $mat-input-prefix-suffix-icon-font-scale * 1em;
163+
width: $mat-input-prefix-suffix-icon-font-scale * 1em;
164+
165+
.mat-icon {
166+
line-height: $mat-input-prefix-suffix-icon-font-scale;
167+
}
168+
}
169+
}
170+
171+
.mat-input-infix {
172+
padding: $mat-input-infix-padding 0;
173+
// Throws off the baseline if we do it as a real margin, so we do it as a border instead.
174+
border-top: $mat-input-infix-margin-top solid transparent;
175+
}
97176

98-
.mat-icon {
99-
font-size: 100%;
177+
.mat-input-element {
178+
&:-webkit-autofill + .mat-input-placeholder-wrapper .mat-float {
179+
@include mat-input-placeholder-floating;
100180
}
101181
}
102182

183+
.mat-input-placeholder-wrapper {
184+
top: -$mat-input-infix-margin-top;
185+
padding-top: $mat-input-infix-margin-top;
186+
}
187+
103188
.mat-input-placeholder {
104-
font-size: 100%;
189+
top: $mat-input-infix-margin-top + $mat-input-infix-padding;
190+
191+
// Show the placeholder above the input when it's not empty, or focused.
192+
&.mat-float:not(.mat-empty), .mat-focused &.mat-float {
193+
@include mat-input-placeholder-floating;
194+
}
195+
}
196+
197+
.mat-input-underline {
198+
// We want the underline to start at the end of the content box, not the padding box,
199+
// so we move it up by the padding amount;
200+
bottom: $mat-input-wrapper-padding-bottom;
105201
}
106202

107203
.mat-input-subscript-wrapper {
108-
font-size: 75%;
204+
font-size: $mat-input-subscript-font-size;
205+
margin-top: $mat-input-subscript-margin-top;
206+
207+
// We want the subscript to start at the end of the content box, not the padding box,
208+
// so we move it up by the padding amount (adjusted for the smaller font size);
209+
top: calc(100% - #{$mat-input-wrapper-padding-bottom / $mat-input-subscript-font-scale});
109210
}
110211
}

src/lib/input/input-container.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<div class="mat-input-wrapper">
2-
<div class="mat-input-table">
2+
<div class="mat-input-flex">
33
<div class="mat-input-prefix" *ngIf="_prefixChildren.length">
4-
<!-- TODO(andrewseguin): remove [md-prefix] -->
5-
<ng-content select="[mdPrefix], [matPrefix], [md-prefix]"></ng-content>
4+
<ng-content select="[mdPrefix], [matPrefix]"></ng-content>
65
</div>
76

87
<div class="mat-input-infix" [class.mat-end]="align == 'end'">
@@ -24,8 +23,7 @@
2423
</div>
2524

2625
<div class="mat-input-suffix" *ngIf="_suffixChildren.length">
27-
<!-- TODO(andrewseguin): remove [md-suffix] -->
28-
<ng-content select="[mdSuffix], [matSuffix], [md-suffix]"></ng-content>
26+
<ng-content select="[mdSuffix], [matSuffix]"></ng-content>
2927
</div>
3028
</div>
3129

0 commit comments

Comments
 (0)