Skip to content

Commit c74bee8

Browse files
committed
fix: update notch when label resizes
1 parent 98a2574 commit c74bee8

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
6060
}
6161

6262
_getNotchWidth() {
63-
if (this.open) {
64-
const NOTCH_ELEMENT_PADDING = 8;
65-
const NOTCH_ELEMENT_BORDER = 1;
66-
return this.labelWidth > 0
67-
? `calc(${this.labelWidth}px * var(--mat-mdc-form-field-floating-label-scale, 0.75) + ${
68-
NOTCH_ELEMENT_PADDING + NOTCH_ELEMENT_BORDER
69-
}px)`
70-
: '0px';
63+
if (!this.open || !this.labelWidth) {
64+
return null;
7165
}
72-
73-
return null;
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)`;
7471
}
7572
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
<label matFormFieldFloatingLabel
1818
[floating]="_shouldLabelFloat()"
1919
*ngIf="_hasFloatingLabel()"
20-
(cdkObserveContent)="_refreshOutlineNotchWidth()"
21-
[cdkObserveContentDisabled]="!_hasOutline()"
2220
[id]="_labelId"
2321
[attr.for]="_control.id"
24-
[attr.aria-owns]="_control.id">
22+
[attr.aria-owns]="_control.id"
23+
(resized)="_refreshOutlineNotchWidth()">
2524
<ng-content select="mat-label"></ng-content>
2625
<!--
2726
We set the required marker as a separate element, in order to make it easier to target if

src/material/form-field/form-field.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,12 @@
148148
@include mdc-line-ripple.core-styles($query: animation);
149149
}
150150
}
151+
152+
// Allow the label to grow 1px bigger than the notch.
153+
// If we see this actually happen we know we need to resize the notch.
154+
.mdc-notched-outline .mdc-floating-label {
155+
max-width: calc(100% + 1px);
156+
}
157+
.mdc-notched-outline--upgraded .mdc-floating-label--float-above {
158+
max-width: calc(100% * 4 / 3 + 1px);
159+
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,6 @@ export class MatFormField
313313
// Initial notch width update. This is needed in case the text-field label floats
314314
// on initialization, and renders inside of the notched outline.
315315
this._refreshOutlineNotchWidth();
316-
// Make sure fonts are loaded before calculating the width.
317-
// zone.js currently doesn't patch the FontFaceSet API so two calls to
318-
// _refreshOutlineNotchWidth is needed for this to work properly in async tests.
319-
// Furthermore if the font takes a long time to load we want the outline notch to be close
320-
// to the correct width from the start then correct itself when the fonts load.
321-
if (this._document?.fonts?.ready) {
322-
this._document.fonts.ready.then(() => {
323-
this._refreshOutlineNotchWidth();
324-
this._changeDetectorRef.markForCheck();
325-
});
326-
} else {
327-
// FontFaceSet is not supported in IE
328-
setTimeout(() => this._refreshOutlineNotchWidth(), 100);
329-
}
330316
// Enable animations now. This ensures we don't animate on initial render.
331317
this._subscriptAnimationState = 'enter';
332318
// Because the above changes a value used in the template after it was checked, we need

0 commit comments

Comments
 (0)