Skip to content

Commit f90b03b

Browse files
committed
fix: update notch size outside of angular change detection
1 parent c74bee8 commit f90b03b

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

src/material/form-field/directives/floating-label.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
EventEmitter,
1414
inject,
1515
Input,
16+
NgZone,
1617
OnDestroy,
1718
Output,
1819
} from '@angular/core';
@@ -52,19 +53,19 @@ export class MatFormFieldFloatingLabel implements AfterViewInit, OnDestroy {
5253

5354
@Output() resized = new EventEmitter<void>();
5455

56+
private _ngZone = inject(NgZone);
57+
5558
private _resizeObserver = inject(SharedResizeObserver);
5659

5760
private _stopResizeObserver = () => {};
5861

5962
constructor(private _elementRef: ElementRef<HTMLElement>) {}
6063

6164
ngAfterViewInit() {
62-
this._stopResizeObserver = this._resizeObserver.observe(
63-
this._elementRef.nativeElement,
64-
() => this.resized.emit(),
65-
{
65+
this._stopResizeObserver = this._ngZone.runOutsideAngular(() =>
66+
this._resizeObserver.observe(this._elementRef.nativeElement, () => this.resized.emit(), {
6667
box: 'border-box',
67-
},
68+
}),
6869
);
6970
}
7071

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="mdc-notched-outline__leading"></div>
2-
<div class="mdc-notched-outline__notch" [style.width]="_getNotchWidth()">
2+
<div class="mdc-notched-outline__notch" #notch>
33
<ng-content></ng-content>
44
</div>
55
<div class="mdc-notched-outline__trailing"></div>

src/material/form-field/directives/notched-outline.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ElementRef,
1414
Input,
1515
NgZone,
16+
ViewChild,
1617
ViewEncapsulation,
1718
} from '@angular/core';
1819

@@ -35,12 +36,11 @@ import {
3536
encapsulation: ViewEncapsulation.None,
3637
})
3738
export class MatFormFieldNotchedOutline implements AfterViewInit {
38-
/** Width of the label (original scale) */
39-
@Input('matFormFieldNotchedOutlineLabelWidth') labelWidth: number = 0;
40-
4139
/** Whether the notch should be opened. */
4240
@Input('matFormFieldNotchedOutlineOpen') open: boolean = false;
4341

42+
@ViewChild('notch') _notch: ElementRef;
43+
4444
constructor(private _elementRef: ElementRef<HTMLElement>, private _ngZone: NgZone) {}
4545

4646
ngAfterViewInit(): void {
@@ -59,14 +59,15 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
5959
}
6060
}
6161

62-
_getNotchWidth() {
63-
if (!this.open || !this.labelWidth) {
64-
return null;
62+
_setNotchWidth(labelWidth: number) {
63+
if (!this.open || !labelWidth) {
64+
this._notch.nativeElement.style.width = '';
65+
} else {
66+
const NOTCH_ELEMENT_PADDING = 8;
67+
const NOTCH_ELEMENT_BORDER = 1;
68+
this._notch.nativeElement.style.width = `calc(${labelWidth}px * var(--mat-mdc-form-field-floating-label-scale, 0.75) + ${
69+
NOTCH_ELEMENT_PADDING + NOTCH_ELEMENT_BORDER
70+
}px)`;
6571
}
66-
const NOTCH_ELEMENT_PADDING = 8;
67-
const NOTCH_ELEMENT_BORDER = 1;
68-
return `calc(${this.labelWidth}px * var(--mat-mdc-form-field-floating-label-scale, 0.75) + ${
69-
NOTCH_ELEMENT_PADDING + NOTCH_ELEMENT_BORDER
70-
}px)`;
7172
}
7273
}

src/material/form-field/form-field.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
<div class="mat-mdc-form-field-focus-overlay" *ngIf="!_hasOutline() && !_control.disabled"></div>
4545
<div class="mat-mdc-form-field-flex">
4646
<div *ngIf="_hasOutline()" matFormFieldNotchedOutline
47-
[matFormFieldNotchedOutlineOpen]="_shouldLabelFloat()"
48-
[matFormFieldNotchedOutlineLabelWidth]="_labelWidth">
47+
[matFormFieldNotchedOutlineOpen]="_shouldLabelFloat()">
4948
<ng-template [ngIf]="!_forceDisplayInfixLabel()">
5049
<ng-template [ngTemplateOutlet]="labelTemplate"></ng-template>
5150
</ng-template>

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ export class MatFormField
218218
}
219219
this._appearance = newAppearance;
220220
if (this._appearance === 'outline' && this._appearance !== oldValue) {
221-
this._refreshOutlineNotchWidth();
222-
223221
// If the appearance has been switched to `outline`, the label offset needs to be updated.
224222
// The update can happen once the view has been re-checked, but not immediately because
225223
// the view has not been updated and the notched-outline floating label is not present.
@@ -267,9 +265,6 @@ export class MatFormField
267265
/** State of the mat-hint and mat-error animations. */
268266
_subscriptAnimationState = '';
269267

270-
/** Width of the label element (at scale=1). */
271-
_labelWidth = 0;
272-
273268
/** Gets the current form field control */
274269
get _control(): MatFormFieldControl<any> {
275270
return this._explicitFormFieldControl || this._formFieldControl;
@@ -310,9 +305,6 @@ export class MatFormField
310305
// Initial focus state sync. This happens rarely, but we want to account for
311306
// it in case the form field control has "focused" set to true on init.
312307
this._updateFocusState();
313-
// Initial notch width update. This is needed in case the text-field label floats
314-
// on initialization, and renders inside of the notched outline.
315-
this._refreshOutlineNotchWidth();
316308
// Enable animations now. This ensures we don't animate on initial render.
317309
this._subscriptAnimationState = 'enter';
318310
// Because the above changes a value used in the template after it was checked, we need
@@ -538,10 +530,11 @@ export class MatFormField
538530

539531
/** Refreshes the width of the outline-notch, if present. */
540532
_refreshOutlineNotchWidth() {
541-
if (!this._hasOutline() || !this._floatingLabel) {
542-
return;
533+
if (!this._hasOutline() || !this._floatingLabel || !this._shouldLabelFloat()) {
534+
this._notchedOutline?._setNotchWidth(0);
535+
} else {
536+
this._notchedOutline?._setNotchWidth(this._floatingLabel.getWidth());
543537
}
544-
this._labelWidth = this._floatingLabel.getWidth();
545538
}
546539

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

0 commit comments

Comments
 (0)