|
| 1 | +@use 'sass:map'; |
| 2 | +@use 'sass:math'; |
| 3 | +@use '@material/density' as mdc-density; |
| 4 | +@use '@material/textfield' as mdc-textfield; |
| 5 | +@use '../core/theming/inspection'; |
| 6 | + |
| 7 | +@use './form-field-sizing'; |
| 8 | + |
| 9 | +// Mixin that sets the vertical spacing for the infix container of filled form fields. |
| 10 | +// We need to apply spacing to the infix container because we removed the input padding |
| 11 | +// provided by MDC in order to support arbitrary form-field controls. |
| 12 | +@mixin _infix-vertical-spacing-filled($with-label-padding, $no-label-padding) { |
| 13 | + .mat-mdc-text-field-wrapper:not(.mdc-text-field--outlined) .mat-mdc-form-field-infix { |
| 14 | + padding-top: map.get($with-label-padding, top); |
| 15 | + padding-bottom: map.get($with-label-padding, bottom); |
| 16 | + } |
| 17 | + |
| 18 | + .mdc-text-field--no-label:not(.mdc-text-field--outlined):not(.mdc-text-field--textarea) |
| 19 | + .mat-mdc-form-field-infix { |
| 20 | + padding-top: map.get($no-label-padding, top); |
| 21 | + padding-bottom: map.get($no-label-padding, bottom); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Mixin that sets the vertical spacing for the infix container of outlined form fields. |
| 26 | +// We need to apply spacing to the infix container because we removed the input padding |
| 27 | +// provided by MDC in order to support arbitrary form-field controls. |
| 28 | +@mixin _infix-vertical-spacing-outlined($padding) { |
| 29 | + .mat-mdc-text-field-wrapper.mdc-text-field--outlined .mat-mdc-form-field-infix { |
| 30 | + padding-top: map.get($padding, top); |
| 31 | + padding-bottom: map.get($padding, bottom); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Mixin that includes the density styles for form fields. MDC provides their own density |
| 36 | +// styles for MDC text-field which we cannot use. MDC relies on input elements to stretch |
| 37 | +// vertically when the height is reduced as per density scale. This doesn't work for our |
| 38 | +// form field since we support custom form field controls without a fixed height. Instead, we |
| 39 | +// provide spacing that makes arbitrary controls align as specified in the Material Design |
| 40 | +// specification. In order to support density, we need to adjust the vertical spacing to be |
| 41 | +// based on the density scale. |
| 42 | +@mixin private-form-field-density($theme) { |
| 43 | + // Height of the form field that is based on the current density scale. |
| 44 | + $height: mdc-density.prop-value( |
| 45 | + $density-config: mdc-textfield.$density-config, |
| 46 | + $density-scale: inspection.get-theme-density($theme), |
| 47 | + $property-name: height, |
| 48 | + ); |
| 49 | + |
| 50 | + // Whether floating labels for filled form fields should be hidden. MDC hides the label in |
| 51 | + // their density styles when the height decreases too much. We match their density styles. |
| 52 | + $hide-filled-floating-label: $height < mdc-textfield.$minimum-height-for-filled-label; |
| 53 | + // We computed the desired height of the form-field using the density configuration. The |
| 54 | + // spec only describes vertical spacing/alignment in non-dense mode. This means that we |
| 55 | + // cannot update the spacing to explicit numbers based on the density scale. Instead, we |
| 56 | + // determine the height reduction and equally subtract it from the default `top` and `bottom` |
| 57 | + // padding that is provided by the Material Design specification. |
| 58 | + $vertical-deduction: math.div(mdc-textfield.$height - $height, 2); |
| 59 | + // Map that describes the padding for form-fields with label. |
| 60 | + $with-label-padding: ( |
| 61 | + top: form-field-sizing.$mat-form-field-with-label-input-padding-top - $vertical-deduction, |
| 62 | + bottom: form-field-sizing.$mat-form-field-with-label-input-padding-bottom - $vertical-deduction, |
| 63 | + ); |
| 64 | + // Map that describes the padding for form-fields without label. |
| 65 | + $no-label-padding: ( |
| 66 | + top: form-field-sizing.$mat-form-field-no-label-padding-top - $vertical-deduction, |
| 67 | + bottom: form-field-sizing.$mat-form-field-no-label-padding-bottom - $vertical-deduction, |
| 68 | + ); |
| 69 | + |
| 70 | + // We add a minimum height to the infix container in order to ensure that custom controls have |
| 71 | + // the same default vertical space as text-field inputs (with respect to the vertical padding). |
| 72 | + .mat-mdc-form-field-infix { |
| 73 | + min-height: $height; |
| 74 | + } |
| 75 | + |
| 76 | + // By default, MDC aligns the label using percentage. This will be overwritten based |
| 77 | + // on whether a textarea is used. This is not possible in our implementation of the |
| 78 | + // form-field because we do not know what type of form-field control is set up. Hence |
| 79 | + // we always use a fixed position for the label. This does not have any implications. |
| 80 | + .mat-mdc-text-field-wrapper .mat-mdc-form-field-flex .mat-mdc-floating-label { |
| 81 | + top: math.div($height, 2); |
| 82 | + } |
| 83 | + |
| 84 | + // For the outline appearance, we re-create the active floating label transform. This is |
| 85 | + // necessary because the transform for docked floating labels can be updated to account for |
| 86 | + // the width of prefix container. |
| 87 | + .mat-mdc-text-field-wrapper.mdc-text-field--outlined .mdc-notched-outline--upgraded |
| 88 | + .mdc-floating-label--float-above { |
| 89 | + --mat-mdc-form-field-label-transform: translateY( |
| 90 | + -#{mdc-textfield.get-outlined-label-position-y($height)}) |
| 91 | + scale(var(--mat-mdc-form-field-floating-label-scale, 0.75)); |
| 92 | + transform: var(--mat-mdc-form-field-label-transform); |
| 93 | + } |
| 94 | + |
| 95 | + // Add vertical spacing to the infix to ensure that outlined form fields have their controls |
| 96 | + // aligned as if there is no label. This is done similarly in MDC and is specified in the |
| 97 | + // Material Design specification. Outline form fields position the control as if there is no |
| 98 | + // label. This is because the label overflows the form-field and doesn't need space at the top. |
| 99 | + @include _infix-vertical-spacing-outlined($no-label-padding); |
| 100 | + |
| 101 | + // MDC hides labels for filled form fields when the form field height decreases. We match |
| 102 | + // this behavior in our custom density styles. |
| 103 | + @if $hide-filled-floating-label { |
| 104 | + // Update the spacing for filled form fields to account for the hidden floating label. |
| 105 | + @include _infix-vertical-spacing-filled( |
| 106 | + $no-label-padding, $no-label-padding); |
| 107 | + .mat-mdc-text-field-wrapper:not(.mdc-text-field--outlined) .mat-mdc-floating-label { |
| 108 | + display: none; |
| 109 | + } |
| 110 | + } |
| 111 | + @else { |
| 112 | + // By default, filled form fields align their controls differently based on whether there |
| 113 | + // is a label or not. MDC does this too, but we cannot rely on their styles as we support |
| 114 | + // arbitrary form field controls and MDC only applies their spacing to the `<input>` elements. |
| 115 | + @include _infix-vertical-spacing-filled( |
| 116 | + $with-label-padding, $no-label-padding); |
| 117 | + } |
| 118 | +} |
0 commit comments