Skip to content

Commit 7de5f54

Browse files
mmalerbadevversion
andcommitted
fix(material-experimental/mdc-form-field): replace !important with CSS var (#24964)
* fix(material-experimental/mdc-form-field): replace !important with CSS var * fix(material-experimental/mdc-form-field): address feedback * fix(material-experimental/mdc-form-field): update src/material-experimental/mdc-form-field/form-field.ts Co-authored-by: Paul Gschwendtner <[email protected]> * fixup! fix(material-experimental/mdc-form-field): update src/material-experimental/mdc-form-field/form-field.ts Co-authored-by: Paul Gschwendtner <[email protected]> (cherry picked from commit 7c91dcf)
1 parent d0082e3 commit 7de5f54

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,12 @@
8484

8585
// For the outline appearance, we re-create the active floating label transform. This is
8686
// necessary because the transform for docked floating labels can be updated to account for
87-
// the width of prefix container. We need to re-create these styles with `!important` because
88-
// the horizontal adjustment for the label is applied through inline styles, and we want to
89-
// make sure that the label can still float as expected. It should be okay using `!important`
90-
// because it's unlikely that developers commonly overwrite the floating label transform.
87+
// the width of prefix container.
9188
.mat-mdc-text-field-wrapper.mdc-text-field--outlined .mdc-notched-outline--upgraded
9289
.mdc-floating-label--float-above {
93-
$outlined-label-floating-position-y: mdc-textfield.get-outlined-label-position-y($height);
94-
// This transform has been extracted from the MDC text-field styles. We can't access it
95-
// through a variable because MDC generates this label transform through a mixin.
96-
transform: translateY(-$outlined-label-floating-position-y) scale(0.75) !important;
90+
--mat-mdc-form-field-label-transform: translateY(
91+
-#{mdc-textfield.get-outlined-label-position-y($height)}) scale(0.75);
92+
transform: var(--mat-mdc-form-field-label-transform);
9793
}
9894

9995
// Add vertical spacing to the infix to ensure that outlined form fields have their controls

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,13 @@ export class MatFormField
668668
((iconPrefixContainer ? iconPrefixContainerWidth - WRAPPER_HORIZONTAL_PADDING : 0) +
669669
textPrefixContainerWidth);
670670

671-
// Update the transform the floating label to account for the prefix container. Note
672-
// that we do not want to overwrite the default transform for docked floating labels.
673-
floatingLabel.style.transform = `${FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM} translateX(${labelHorizontalOffset}px)`;
671+
// Update the translateX of the floating label to account for the prefix container,
672+
// but allow the CSS to override this setting via a CSS variable when the label is
673+
// floating.
674+
floatingLabel.style.transform = `var(
675+
--mat-mdc-form-field-label-transform,
676+
${FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM} translateX(${labelHorizontalOffset}px
677+
)`;
674678
}
675679

676680
/** Checks whether the form field is attached to the DOM. */

0 commit comments

Comments
 (0)