Skip to content

fix(material-experimental/mdc-form-field): use a CSS var for the floating label scale #25178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
.mat-mdc-text-field-wrapper.mdc-text-field--outlined .mdc-notched-outline--upgraded
.mdc-floating-label--float-above {
--mat-mdc-form-field-label-transform: translateY(
-#{mdc-textfield.get-outlined-label-position-y($height)}) scale(0.75);
-#{mdc-textfield.get-outlined-label-position-y($height)})
scale(var(--mat-mdc-form-field-floating-label-scale, 0.75));
transform: var(--mat-mdc-form-field-label-transform);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
// For the non-upgraded notch label (i.e. when rendered on the server), also
// use the correct `body1` typography level.
.mdc-floating-label--float-above {
font-size: mdc-typography.get-size(body1) * 0.75;
font-size: calc(#{
mdc-typography.get-size(body1)} * var(--mat-mdc-form-field-floating-label-scale, 0.75));
}
.mdc-notched-outline--upgraded .mdc-floating-label--float-above {
font-size: mdc-typography.get-size(body1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import {
encapsulation: ViewEncapsulation.None,
})
export class MatFormFieldNotchedOutline implements AfterViewInit {
/** Width of the notch. */
@Input('matFormFieldNotchedOutlineWidth') width: number = 0;
/** Width of the label (original scale) */
@Input('matFormFieldNotchedOutlineLabelWidth') labelWidth: number = 0;

/** Whether the notch should be opened. */
@Input('matFormFieldNotchedOutlineOpen') open: boolean = false;
Expand All @@ -62,7 +62,9 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
_getNotchWidth() {
if (this.open) {
const NOTCH_ELEMENT_PADDING = 8;
return `${this.width > 0 ? this.width + NOTCH_ELEMENT_PADDING : 0}px`;
return this.labelWidth > 0
? `calc(${this.labelWidth}px * var(--mat-mdc-form-field-floating-label-scale, 0.75) + ${NOTCH_ELEMENT_PADDING}px)`
: '0px';
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-form-field/form-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<div class="mat-mdc-form-field-flex">
<div *ngIf="_hasOutline()" matFormFieldNotchedOutline
[matFormFieldNotchedOutlineOpen]="_shouldLabelFloat()"
[matFormFieldNotchedOutlineWidth]="_outlineNotchWidth">
[matFormFieldNotchedOutlineLabelWidth]="_labelWidth">
<ng-template [ngIf]="!_forceDisplayInfixLabel()">
<ng-template [ngTemplateOutlet]="labelTemplate"></ng-template>
</ng-template>
Expand Down
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-form-field/form-field.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
// Host element of the form-field. It contains the mdc-text-field wrapper
// and the subscript wrapper.
.mat-mdc-form-field {
// The scale to use for the form-field's label when its in the floating position.
--mat-mdc-form-field-floating-label-scale: 0.75;

display: inline-flex;
// This container contains the text-field and the subscript. The subscript
// should be displayed below the text-field. Hence the column direction.
Expand Down
12 changes: 3 additions & 9 deletions src/material-experimental/mdc-form-field/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ const FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM = `translateY(-50%)`;
*/
const WRAPPER_HORIZONTAL_PADDING = 16;

/** Amount by which to scale the label when the form field is focused. */
const LABEL_SCALE = 0.75;

/** Container for form controls that applies Material Design styling and behavior. */
@Component({
selector: 'mat-form-field',
Expand Down Expand Up @@ -270,8 +267,8 @@ export class MatFormField
/** State of the mat-hint and mat-error animations. */
_subscriptAnimationState = '';

/** Width of the outline notch. */
_outlineNotchWidth = 0;
/** Width of the label element (at scale=1). */
_labelWidth = 0;

/** Gets the current form field control */
get _control(): MatFormFieldControl<any> {
Expand Down Expand Up @@ -558,10 +555,7 @@ export class MatFormField
if (!this._hasOutline() || !this._floatingLabel) {
return;
}
// The outline notch should be based on the label width, but needs to respect the scaling
// applied to the label if it actively floats. Since the label always floats when the notch
// is open, the MDC text-field floating label scaling is respected in notch width calculation.
this._outlineNotchWidth = this._floatingLabel.getWidth() * LABEL_SCALE;
this._labelWidth = this._floatingLabel.getWidth();
}

/** Does any extra processing that is required when handling the hints. */
Expand Down