Skip to content

Commit fdb30ad

Browse files
authored
fix(material/form-field): remove hardcoded 16px label padding (#25383)
1 parent 439852b commit fdb30ad

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
// structure for our form-field in order to support arbitrary height input elements.
5858
.mat-mdc-form-field-has-icon-prefix .mat-mdc-text-field-wrapper {
5959
padding-left: 0;
60+
// Signal to the TypeScript label calculation code that the label should be offset 16px less
61+
// due to resetting the padding above.
62+
--mat-mdc-form-field-label-offset-x: -16px;
6063
}
6164
.mat-mdc-form-field-has-icon-suffix .mat-mdc-text-field-wrapper {
6265
padding-right: 0;

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ const DEFAULT_SUBSCRIPT_SIZING: SubscriptSizing = 'fixed';
114114
*/
115115
const FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM = `translateY(-50%)`;
116116

117-
/**
118-
* Horizontal padding in pixels used by the MDC for the wrapper containing infix.
119-
* This value is extracted from MDC's Sass variables. See `$padding-horizontal`.
120-
*/
121-
const WRAPPER_HORIZONTAL_PADDING = 16;
122-
123117
/** Container for form controls that applies Material Design styling and behavior. */
124118
@Component({
125119
selector: 'mat-form-field',
@@ -669,19 +663,17 @@ export class MatFormField
669663
const textPrefixContainerWidth = textPrefixContainer?.getBoundingClientRect().width ?? 0;
670664
// If the directionality is RTL, the x-axis transform needs to be inverted. This
671665
// is because `transformX` does not change based on the page directionality.
672-
const labelHorizontalOffset =
673-
(this._dir.value === 'rtl' ? -1 : 1) *
674-
// If there's an icon prefix, we subtract the default horizontal padding as we
675-
// reset the horizontal padding in CSS too.
676-
((iconPrefixContainer ? iconPrefixContainerWidth - WRAPPER_HORIZONTAL_PADDING : 0) +
677-
textPrefixContainerWidth);
666+
const negate = this._dir.value === 'rtl' ? '-1' : '1';
667+
const prefixWidth = `${iconPrefixContainerWidth + textPrefixContainerWidth}px`;
668+
const labelOffset = `var(--mat-mdc-form-field-label-offset-x, 0px)`;
669+
const labelHorizontalOffset = `calc(${negate} * (${prefixWidth} + ${labelOffset}))`;
678670

679671
// Update the translateX of the floating label to account for the prefix container,
680672
// but allow the CSS to override this setting via a CSS variable when the label is
681673
// floating.
682674
floatingLabel.style.transform = `var(
683675
--mat-mdc-form-field-label-transform,
684-
${FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM} translateX(${labelHorizontalOffset}px
676+
${FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM} translateX(${labelHorizontalOffset})
685677
)`;
686678
}
687679

0 commit comments

Comments
 (0)