Skip to content

Commit f4f0d95

Browse files
devversionjelbourn
authored andcommitted
fix(material-experimental/mdc-form-field): update input alignment to match spec
Unfortunately we cannot leverage MDC for aligning controls vertically in the form-field. This is because we support custom form controls and MDC applies alignment to the input directly. This cannot work for our form-field as there is no guaranteed input in a control. This commit updates the alignment to be more in sync with the spec measured values / and the MDC implementation. For better insight, the offset required for the input box is now logically subtracted for an improved code health when we need to update again in the future.
1 parent 6d80203 commit f4f0d95

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,28 @@ $mat-form-field-hint-min-space: 1em !default;
1919
// their padding variables are calculated with respect to the vertical empty space of the
2020
// inputs. We take the explicit numbers provided by the Material Design specification.
2121
// https://material.io/components/text-fields/#specs
22-
$mat-form-field-no-label-padding-bottom: 16px;
23-
$mat-form-field-no-label-padding-top: 20px;
2422

25-
// Vertical spacing of the text-field if there is a label. MDC hard-codes the spacing
26-
// into their styles, but their spacing variables would not work for our form-field
27-
// structure anyway. This is because MDC's input elements are larger than the text, and
28-
// their padding variables are calculated with respect to the vertical empty space of the
29-
// inputs. We take the numbers provided by the Material Design specification. **Note** that
30-
// the drawn values in the spec are slightly shifted because the spec assumes that typed input
31-
// text exceeds the input element boundaries. We account for this since typed input text does
32-
// not overflow in browsers by default.
33-
$mat-form-field-with-label-input-padding-top: 24px;
34-
$mat-form-field-with-label-input-padding-bottom: 12px;
23+
// Dimensions in the spec assume that an input box does not need to account for larger
24+
// characters. In browsers though, inputs always add additional vertical spacing to account
25+
// for such potential characters. This means that all determined spacing from the spec needs
26+
// to account for the input offset. The offset is based on the default font size used in the
27+
// spec images. i.e. the input box for a 16dp input is 1px larger on top and bottom.
28+
// Related information: https://www.w3.org/TR/CSS21/visudet.html#inline-non-replaced.
29+
$mat-form-field-input-box-vertical-offset: 1px;
30+
31+
// Vertical spacing of the text-field if there is a label. MDC hard-codes the spacing into
32+
// their styles, but their spacing variables would not work for our form-field structure anyway.
33+
// This is because MDC's alignment depends on the input element to expand to full infix height.
34+
// We allow for arbitrary form controls and support dynamic height, so we manage the control
35+
// infix alignment through padding on the infix that works for any control. We manually measure
36+
// spacing as provided by the Material Design specification. The outlined dimensions in the
37+
// spec section do not match with the text fields shown in the overview or the ones implemented
38+
// 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;
41+
42+
// Vertical spacing of the text-field if there is no label. We manually measure the
43+
// spacing in the specs. See comment above for padding for text fields with label. The
44+
// 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;

0 commit comments

Comments
 (0)