It seems my question went unanswered, so I will share the solutions I came up with for this issue. One is a bit rudimentary (Solution 2) and the other is what I consider more appropriate (Solution 2, even though it may seem otherwise).
Edit: Solution 2: A More Proper Approach
I attempted to make my code more configurable by making a small change - switching one variable to static. This variable is used to declare the ID that MatFormField uses in a class definition within itself, allowing us to customize the appearance of our component.
This variable is named controlType
. By using this variable, we can determine when our component is being used by directly referencing its class name following the naming convention
mat-form-field-type-<controlType>
. Since my
controlType = "app-text-editor"
, I can utilize it like this:
.mat-form-field-type-app-text-editor:not(.mat-form-field-should-float) .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: 2.5rem;
padding: .5em;
}
Original: Solution 1: A Hacky Approach
What I did was modify my component to use
encapsulation: ViewEncapsulation.None
, utilized the selector of my component in CSS as the primary identifier (in this case:
app-text-editor
) and then employed CSS Sibling selector to target the floating label and placeholder to adjust the position of my
TextEditor header and revert it to its default once the label is floating. The resulting CSS appears as follows:
app-text-editor {
// Styling for actual TextEditor
&.floating ~ .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: initial;
padding: initial;
}
& ~ .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: 2.5rem;
padding: .5em; // Used to properly align the content inside my contenteditable
}
Alternatively, with just CSS, it would appear as:
app-text-editor.floating ~ .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: initial;
padding: initial;
}
app-text-editor ~ .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: 2.5rem;
padding: .5em; /* Used to properly align the content inside my contenteditable */
}
Surprisingly, despite the somewhat unconventional method I used for repositioning, the animation transition still appears smooth.
If you are willing to dive into advanced CSS selectors (Can I Use: Advanced CSS3 selectors), the solution can be made even more concise:
SCSS:
app-text-editor {
// Styling for the actual TextEditor
&:not(.floating) ~ .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: 2.5rem;
padding: .5em; // Used to properly align the content inside my contenteditable
}
Pure CSS:
app-text-editor:not(.floating) ~ .mat-form-field-label-wrapper > .mat-form-field-label {
margin-top: 2.5rem;
padding: .5em; /* Used to properly align the content inside my contenteditable */
}