Skip to content

Commit 5094eaa

Browse files
committed
fix(material-experimental/mdc-form-field): tweak styles to work with MDC
typography
1 parent 8992161 commit 5094eaa

File tree

8 files changed

+48
-44
lines changed

8 files changed

+48
-44
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ <h4>Textarea</h4>
315315
<mat-form-field style="margin-bottom: 4em">
316316
<mat-label>Enter text to count</mat-label>
317317
<textarea matInput #longHint></textarea>
318-
<mat-hint>
319-
Enter some text to count how many characters are in it. The character count is shown on
318+
<mat-hint>Enter
319+
some text to count how many characters are in it. The character count is shown on
320320
the right.
321321
</mat-hint>
322322
<mat-hint align="end" style="white-space: nowrap" aria-live="polite">

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ $mat-form-field-input-box-vertical-offset: 1px;
3636
// spacing as provided by the Material Design specification. The outlined dimensions in the
3737
// spec section do not match with the text fields shown in the overview or the ones implemented
3838
// by MDC. Note that we need to account for the input box offset. See above for more context.
39-
$mat-form-field-with-label-input-padding-top: 28px - $mat-form-field-input-box-vertical-offset;
40-
$mat-form-field-with-label-input-padding-bottom: 12px - $mat-form-field-input-box-vertical-offset;
39+
$mat-form-field-with-label-input-padding-top: 24px - $mat-form-field-input-box-vertical-offset;
40+
$mat-form-field-with-label-input-padding-bottom: 8px - $mat-form-field-input-box-vertical-offset;
4141

4242
// Vertical spacing of the text-field if there is no label. We manually measure the
4343
// spacing in the specs. See comment above for padding for text fields with label. The
4444
// same reasoning applies to the padding for text fields without label.
45-
$mat-form-field-no-label-padding-bottom: 20px - $mat-form-field-input-box-vertical-offset;
46-
$mat-form-field-no-label-padding-top: 20px - $mat-form-field-input-box-vertical-offset;
45+
$mat-form-field-no-label-padding-bottom: 16px - $mat-form-field-input-box-vertical-offset;
46+
$mat-form-field-no-label-padding-top: 16px - $mat-form-field-input-box-vertical-offset;

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,34 @@
88
.mat-mdc-form-field-subscript-wrapper {
99
box-sizing: border-box;
1010
width: 100%;
11-
// prevents multi-line errors from overlapping the control.
12-
overflow: hidden;
11+
position: relative;
12+
}
13+
14+
.mat-mdc-form-field-hint-wrapper,
15+
.mat-mdc-form-field-error-wrapper {
16+
position: absolute;
17+
top: 0;
18+
left: 0;
19+
right: 0;
20+
}
21+
22+
.mat-mdc-form-field-bottom-align {
23+
line-height: normal;
24+
}
25+
26+
.mat-mdc-form-field-bottom-align::before {
27+
content: '\200B';
28+
vertical-align: bottom;
1329
}
1430

1531
// Scale down icons in the subscript to be the same size as the text.
16-
.mat-mdc-form-field-subscript-wrapper .mat-icon {
17-
width: 1em;
18-
height: 1em;
19-
font-size: inherit;
20-
vertical-align: baseline;
32+
.mat-mdc-form-field-subscript-wrapper,
33+
.mat-mdc-form-field label {
34+
.mat-icon {
35+
width: 1em;
36+
height: 1em;
37+
font-size: inherit;
38+
}
2139
}
2240

2341
// Clears the floats on the hints. This is necessary for the hint animation to work.
@@ -47,29 +65,11 @@
4765
// helper text in our MDC based form field
4866
@mixin mat-mdc-private-form-field-subscript-typography($config-or-theme) {
4967
$config: mat-get-typography-config($config-or-theme);
50-
// The unit-less line-height from the font config.
51-
$line-height: mat-line-height($config, input);
52-
// The amount to scale the font for the subscript.
53-
$subscript-font-scale: 0.75;
54-
// Font size to use for the subscript text.
55-
$subscript-font-size: $subscript-font-scale * 100%;
56-
// The space between the bottom of the text-field area and the subscript. Mocks in the spec show
57-
// half of the text size, but this margin is applied to an element with the subscript text font
58-
// size, so we need to divide by the scale factor to make it half of the original text size.
59-
$subscript-margin-top: 0.5em / $subscript-font-scale;
60-
// The minimum height applied to the subscript to reserve space for subscript text. This is a
61-
// combination of the subscript's margin and line-height, but we need to multiply by the
62-
// subscript font scale factor since the subscript has a reduced font size.
63-
$subscript-min-height: ($subscript-margin-top + $line-height) * $subscript-font-scale;
64-
// The horizontal padding between the edge of the text box and the start of the subscript text.
65-
$subscript-horizontal-padding: 16px;
6668

6769
// The subscript wrapper has a minimum height to avoid that the form-field
6870
// jumps when hints or errors are displayed.
69-
.mat-mdc-form-field-subscript-wrapper {
70-
min-height: $subscript-min-height;
71-
font-size: $subscript-font-size;
72-
margin-top: $subscript-margin-top;
73-
padding: 0 $subscript-horizontal-padding;
71+
.mat-mdc-form-field-subscript-wrapper,
72+
.mat-mdc-form-field-bottom-align::before {
73+
@include mdc-typography(caption, $query: $mat-typography-styles-query);
7474
}
7575
}

src/material-experimental/mdc-form-field/_mdc-text-field-structure-overrides.scss

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
@mixin mat-mdc-private-text-field-structure-overrides() {
1010
// Unset the border set by MDC. We move the border (which serves as the Material Design
1111
// text-field bottom line) into its own element. This is necessary because we want the
12-
// bottom-line to span across the whole form-field (including prefixes and suffixes). Also
13-
// we ensure that font styles are inherited for input elements. We do this because inputs by
14-
// default have explicit font styles from the user agent, and we set the desired font styles
15-
// in the parent `mat-form-field` element (for better custom form-field control support).
12+
// bottom-line to span across the whole form-field (including prefixes and suffixes).
1613
.mat-mdc-input-element {
17-
font: inherit;
1814
border: none;
1915
}
2016

17+
// In order to ensure proper alignment of the floating label, we reset its line-height.
18+
// The line-height is not important as the element is absolutely positioned and only has one line
19+
// of text.
20+
.mat-mdc-form-field .mdc-floating-label {
21+
line-height: normal;
22+
}
23+
2124
// Reset the height that MDC sets on native input elements. We cannot rely on their
2225
// fixed height as we handle vertical spacing differently. MDC sets a fixed height for
2326
// inputs and modifies the baseline so that the textfield matches the spec. This does

src/material-experimental/mdc-form-field/directives/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const MAT_ERROR = new InjectionToken<MatError>('MatError');
2121
@Directive({
2222
selector: 'mat-error',
2323
host: {
24-
'class': 'mat-mdc-form-field-error',
24+
'class': 'mat-mdc-form-field-error mat-mdc-form-field-bottom-align',
2525
'role': 'alert',
2626
'[id]': 'id',
2727
},

src/material-experimental/mdc-form-field/directives/hint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let nextUniqueId = 0;
1414
@Directive({
1515
selector: 'mat-hint',
1616
host: {
17-
'class': 'mat-mdc-form-field-hint',
17+
'class': 'mat-mdc-form-field-hint mat-mdc-form-field-bottom-align',
1818
'[class.mat-mdc-form-field-hint-end]': 'align === "end"',
1919
'[id]': 'id',
2020
// Remove align attribute to prevent it from interfering with layout.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@
6464
<div matFormFieldLineRipple *ngIf="!_hasOutline()"></div>
6565
</div>
6666

67-
<div class="mat-mdc-form-field-subscript-wrapper"
67+
<div class="mat-mdc-form-field-subscript-wrapper mat-mdc-form-field-bottom-align"
6868
[ngSwitch]="_getDisplayedMessages()">
69-
<div *ngSwitchCase="'error'" [@transitionMessages]="_subscriptAnimationState">
69+
<div class="mat-mdc-form-field-error-wrapper" *ngSwitchCase="'error'"
70+
[@transitionMessages]="_subscriptAnimationState">
7071
<ng-content select="mat-error"></ng-content>
7172
</div>
7273

src/material/form-field/form-field-animations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const matFormFieldAnimations: {
2626
// TODO(mmalerba): Use angular animations for label animation as well.
2727
state('enter', style({ opacity: 1, transform: 'translateY(0%)' })),
2828
transition('void => enter', [
29-
style({ opacity: 0, transform: 'translateY(-100%)' }),
29+
style({ opacity: 0, transform: 'translateY(-5px)' }),
3030
animate('300ms cubic-bezier(0.55, 0, 0.55, 0.2)'),
3131
]),
3232
])

0 commit comments

Comments
 (0)