@@ -52,6 +52,7 @@ import {MatFormFieldLineRipple} from './directives/line-ripple';
52
52
import { MatFormFieldNotchedOutline } from './directives/notched-outline' ;
53
53
import { MAT_PREFIX , MatPrefix } from './directives/prefix' ;
54
54
import { MAT_SUFFIX , MatSuffix } from './directives/suffix' ;
55
+ import { DOCUMENT } from '@angular/common' ;
55
56
56
57
/** Type for the available floatLabel values. */
57
58
export type FloatLabelType = 'always' | 'auto' ;
@@ -308,7 +309,8 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
308
309
private _platform : Platform ,
309
310
@Optional ( ) @Inject ( MAT_FORM_FIELD_DEFAULT_OPTIONS )
310
311
private _defaults ?: MatFormFieldDefaultOptions ,
311
- @Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) public _animationMode ?: string ) {
312
+ @Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) public _animationMode ?: string ,
313
+ @Inject ( DOCUMENT ) private _document ?: any ) {
312
314
if ( _defaults && _defaults . appearance ) {
313
315
this . appearance = _defaults . appearance ;
314
316
} else if ( _defaults && _defaults . hideRequiredMarker ) {
@@ -341,6 +343,20 @@ export class MatFormField implements AfterViewInit, OnDestroy, AfterContentCheck
341
343
// Initial notch width update. This is needed in case the text-field label floats
342
344
// on initialization, and renders inside of the notched outline.
343
345
this . _refreshOutlineNotchWidth ( ) ;
346
+ // Make sure fonts are loaded before calculating the width.
347
+ // zone.js currently doesn't patch the FontFaceSet API so two calls to
348
+ // _refreshOutlineNotchWidth is needed for this to work properly in async tests.
349
+ // Furthermore if the font takes a long time to load we want the outline notch to be close
350
+ // to the correct width from the start then correct itself when the fonts load.
351
+ if ( this . _document ?. fonts ?. ready ) {
352
+ this . _document . fonts . ready . then ( ( ) => {
353
+ this . _refreshOutlineNotchWidth ( ) ;
354
+ this . _changeDetectorRef . markForCheck ( ) ;
355
+ } ) ;
356
+ } else {
357
+ // FontFaceSet is not supported in IE
358
+ setTimeout ( ( ) => this . _refreshOutlineNotchWidth ( ) , 100 ) ;
359
+ }
344
360
// Enable animations now. This ensures we don't animate on initial render.
345
361
this . _subscriptAnimationState = 'enter' ;
346
362
// Because the above changes a value used in the template after it was checked, we need
0 commit comments