Skip to content

Commit 124f1cc

Browse files
committed
fix(material-experimental/mdc-form-field): replace !important with CSS var
1 parent d755c66 commit 124f1cc

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,11 @@
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:
91+
translateY(-#{mdc-textfield.get-outlined-label-position-y($height)}) scale(0.75);
9792
}
9893

9994
// 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: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ const DEFAULT_FLOAT_LABEL: FloatLabelType = 'auto';
101101
/** Default way that the subscript element height is set. */
102102
const DEFAULT_SUBSCRIPT_SIZING: SubscriptSizing = 'fixed';
103103

104-
/**
105-
* Default transform for docked floating labels in a MDC text-field. This value has been
106-
* extracted from the MDC text-field styles because we programmatically modify the docked
107-
* label transform, but do not want to accidentally discard the default label transform.
108-
*/
109-
const FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM = `translateY(-50%)`;
110-
111104
/**
112105
* Horizontal padding in pixels used by the MDC for the wrapper containing infix.
113106
* This value is extracted from MDC's Sass variables. See `$padding-horizontal`.
@@ -676,9 +669,8 @@ export class MatFormField
676669
((iconPrefixContainer ? iconPrefixContainerWidth - WRAPPER_HORIZONTAL_PADDING : 0) +
677670
textPrefixContainerWidth);
678671

679-
// Update the transform the floating label to account for the prefix container. Note
680-
// that we do not want to overwrite the default transform for docked floating labels.
681-
floatingLabel.style.transform = `${FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM} translateX(${labelHorizontalOffset}px)`;
672+
// Update the transform x of the floating label to account for the prefix container.
673+
floatingLabel.style.transform = `var(--mat-mdc-form-field-label-transform, translateX(${labelHorizontalOffset}px)`;
682674
}
683675

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

0 commit comments

Comments
 (0)