Skip to content

Commit

Permalink
fix(material-experimental/mdc-form-field): tweak styles to work with MDC
Browse files Browse the repository at this point in the history
typography

(cherry picked from commit e5db56f)
  • Loading branch information
mmalerba committed Feb 2, 2021
1 parent d9b10c4 commit 9518224
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/dev-app/mdc-input/mdc-input-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ <h4>Textarea</h4>
<mat-form-field style="margin-bottom: 4em">
<mat-label>Enter text to count</mat-label>
<textarea matInput #longHint></textarea>
<mat-hint>
Enter some text to count how many characters are in it. The character count is shown on
<mat-hint>Enter
some text to count how many characters are in it. The character count is shown on
the right.
</mat-hint>
<mat-hint align="end" style="white-space: nowrap" aria-live="polite">
Expand Down
16 changes: 5 additions & 11 deletions src/material-experimental/mdc-form-field/_form-field-sizing.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ $mat-form-field-hint-min-space: 1em !default;
// inputs. We take the explicit numbers provided by the Material Design specification.
// https://material.io/components/text-fields/#specs

// Dimensions in the spec assume that an input box does not need to account for larger
// characters. In browsers though, inputs always add additional vertical spacing to account
// for such potential characters. This means that all determined spacing from the spec needs
// to account for the input offset. The offset is based on the default font size used in the
// spec images. i.e. the input box for a 16dp input is 1px larger on top and bottom.
// Related information: https://www.w3.org/TR/CSS21/visudet.html#inline-non-replaced.
$mat-form-field-input-box-vertical-offset: 1px;
$mat-form-field-height: 56px;

// Vertical spacing of the text-field if there is a label. MDC hard-codes the spacing into
// their styles, but their spacing variables would not work for our form-field structure anyway.
Expand All @@ -36,11 +30,11 @@ $mat-form-field-input-box-vertical-offset: 1px;
// spacing as provided by the Material Design specification. The outlined dimensions in the
// spec section do not match with the text fields shown in the overview or the ones implemented
// by MDC. Note that we need to account for the input box offset. See above for more context.
$mat-form-field-with-label-input-padding-top: 28px - $mat-form-field-input-box-vertical-offset;
$mat-form-field-with-label-input-padding-bottom: 12px - $mat-form-field-input-box-vertical-offset;
$mat-form-field-with-label-input-padding-top: 24px;
$mat-form-field-with-label-input-padding-bottom: 8px;

// Vertical spacing of the text-field if there is no label. We manually measure the
// spacing in the specs. See comment above for padding for text fields with label. The
// same reasoning applies to the padding for text fields without label.
$mat-form-field-no-label-padding-bottom: 20px - $mat-form-field-input-box-vertical-offset;
$mat-form-field-no-label-padding-top: 20px - $mat-form-field-input-box-vertical-offset;
$mat-form-field-no-label-padding-bottom: 16px;
$mat-form-field-no-label-padding-top: 16px;
57 changes: 29 additions & 28 deletions src/material-experimental/mdc-form-field/_form-field-subscript.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,35 @@
.mat-mdc-form-field-subscript-wrapper {
box-sizing: border-box;
width: 100%;
// prevents multi-line errors from overlapping the control.
overflow: hidden;
position: relative;
}

// The horizontal padding between the edge of the text box and the start of the subscript text.
$subscript-horizontal-padding: 16px;

.mat-mdc-form-field-hint-wrapper,
.mat-mdc-form-field-error-wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
padding: 0 $subscript-horizontal-padding;
}

.mat-mdc-form-field-bottom-align::before {
content: '';
display: inline-block;
height: 16px;
}

// Scale down icons in the subscript to be the same size as the text.
.mat-mdc-form-field-subscript-wrapper .mat-icon {
width: 1em;
height: 1em;
font-size: inherit;
vertical-align: baseline;
.mat-mdc-form-field-subscript-wrapper,
.mat-mdc-form-field label {
.mat-icon {
width: 1em;
height: 1em;
font-size: inherit;
}
}

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

// The subscript wrapper has a minimum height to avoid that the form-field
// jumps when hints or errors are displayed.
.mat-mdc-form-field-subscript-wrapper {
min-height: $subscript-min-height;
font-size: $subscript-font-size;
margin-top: $subscript-margin-top;
padding: 0 $subscript-horizontal-padding;
.mat-mdc-form-field-subscript-wrapper,
.mat-mdc-form-field-bottom-align::before {
@include mdc-typography(caption, $query: $mat-typography-styles-query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
@mixin mat-mdc-private-text-field-structure-overrides() {
// Unset the border set by MDC. We move the border (which serves as the Material Design
// text-field bottom line) into its own element. This is necessary because we want the
// bottom-line to span across the whole form-field (including prefixes and suffixes). Also
// we ensure that font styles are inherited for input elements. We do this because inputs by
// default have explicit font styles from the user agent, and we set the desired font styles
// in the parent `mat-form-field` element (for better custom form-field control support).
// bottom-line to span across the whole form-field (including prefixes and suffixes).
.mat-mdc-input-element {
font: inherit;
border: none;
}

// In order to ensure proper alignment of the floating label, we reset its line-height.
// The line-height is not important as the element is absolutely positioned and only has one line
// of text.
.mat-mdc-form-field .mdc-floating-label {
line-height: normal;
}

// Reset the height that MDC sets on native input elements. We cannot rely on their
// fixed height as we handle vertical spacing differently. MDC sets a fixed height for
// inputs and modifies the baseline so that the textfield matches the spec. This does
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const MAT_ERROR = new InjectionToken<MatError>('MatError');
@Directive({
selector: 'mat-error',
host: {
'class': 'mat-mdc-form-field-error',
'class': 'mat-mdc-form-field-error mat-mdc-form-field-bottom-align',
'role': 'alert',
'[id]': 'id',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let nextUniqueId = 0;
@Directive({
selector: 'mat-hint',
host: {
'class': 'mat-mdc-form-field-hint',
'class': 'mat-mdc-form-field-hint mat-mdc-form-field-bottom-align',
'[class.mat-mdc-form-field-hint-end]': 'align === "end"',
'[id]': 'id',
// Remove align attribute to prevent it from interfering with layout.
Expand Down
5 changes: 3 additions & 2 deletions src/material-experimental/mdc-form-field/form-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@
<div matFormFieldLineRipple *ngIf="!_hasOutline()"></div>
</div>

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

Expand Down
2 changes: 1 addition & 1 deletion src/material/form-field/form-field-animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const matFormFieldAnimations: {
// TODO(mmalerba): Use angular animations for label animation as well.
state('enter', style({ opacity: 1, transform: 'translateY(0%)' })),
transition('void => enter', [
style({ opacity: 0, transform: 'translateY(-100%)' }),
style({ opacity: 0, transform: 'translateY(-5px)' }),
animate('300ms cubic-bezier(0.55, 0, 0.55, 0.2)'),
]),
])
Expand Down

0 comments on commit 9518224

Please sign in to comment.