@@ -48,6 +48,12 @@ import {MatLabel} from './label';
4848import { MatPlaceholder } from './placeholder' ;
4949import { MatPrefix } from './prefix' ;
5050import { MatSuffix } from './suffix' ;
51+ import { Directionality } from '@angular/cdk/bidi' ;
52+
53+
54+ let nextUniqueId = 0 ;
55+ const floatingLabelScale = 0.75 ;
56+ const outlineGapPadding = 5 ;
5157
5258
5359// Boilerplate for applying mixins to MatFormField.
@@ -56,10 +62,8 @@ export class MatFormFieldBase {
5662 constructor ( public _elementRef : ElementRef ) { }
5763}
5864
59- export const _MatFormFieldMixinBase = mixinColor ( MatFormFieldBase , 'primary' ) ;
60-
6165
62- let nextUniqueId = 0 ;
66+ export const _MatFormFieldMixinBase = mixinColor ( MatFormFieldBase , 'primary' ) ;
6367
6468
6569export type MatFormFieldAppearance = 'legacy' | 'standard' | 'fill' | 'outline' ;
@@ -118,7 +122,7 @@ export class MatFormField extends _MatFormFieldMixinBase
118122 private _labelOptions : LabelOptions ;
119123
120124 /** The form-field appearance style. */
121- @Input ( ) appearance : MatFormFieldAppearance = 'outline ' ;
125+ @Input ( ) appearance : MatFormFieldAppearance = 'legacy ' ;
122126
123127 /**
124128 * @deprecated Use `color` instead.
@@ -191,6 +195,10 @@ export class MatFormField extends _MatFormFieldMixinBase
191195 }
192196 private _floatLabel : FloatLabelType ;
193197
198+ _outlineGapWidth = 0 ;
199+
200+ _outlineGapStart = 0 ;
201+
194202 /** @deletion -target 7.0.0 */
195203 @ViewChild ( 'underline' ) underlineRef : ElementRef ;
196204
@@ -208,7 +216,8 @@ export class MatFormField extends _MatFormFieldMixinBase
208216 constructor (
209217 public _elementRef : ElementRef ,
210218 private _changeDetectorRef : ChangeDetectorRef ,
211- @Optional ( ) @Inject ( MAT_LABEL_GLOBAL_OPTIONS ) labelOptions : LabelOptions ) {
219+ @Optional ( ) @Inject ( MAT_LABEL_GLOBAL_OPTIONS ) labelOptions : LabelOptions ,
220+ @Optional ( ) private _dir : Directionality ) {
212221 super ( _elementRef ) ;
213222
214223 this . _labelOptions = labelOptions ? labelOptions : { } ;
@@ -255,6 +264,11 @@ export class MatFormField extends _MatFormFieldMixinBase
255264 this . _syncDescribedByIds ( ) ;
256265 this . _changeDetectorRef . markForCheck ( ) ;
257266 } ) ;
267+
268+ Promise . resolve ( ) . then ( ( ) => {
269+ this . _updateOutlineGap ( ) ;
270+ this . _changeDetectorRef . detectChanges ( ) ;
271+ } ) ;
258272 }
259273
260274 ngAfterContentChecked ( ) {
@@ -394,4 +408,32 @@ export class MatFormField extends _MatFormFieldMixinBase
394408 throw getMatFormFieldMissingControlError ( ) ;
395409 }
396410 }
411+
412+ /**
413+ * Updates the width and position of the gap in the outline. Only relevant for the outline
414+ * appearance.
415+ */
416+ private _updateOutlineGap ( ) {
417+ if ( this . appearance === 'outline' && this . _label && this . _label . nativeElement . children . length ) {
418+ const containerStart = this . _getStartEnd (
419+ this . _connectionContainerRef . nativeElement . getBoundingClientRect ( ) ) ;
420+ const labelStart = this . _getStartEnd (
421+ this . _label . nativeElement . children [ 0 ] . getBoundingClientRect ( ) ) ;
422+ let labelWidth = 0 ;
423+ for ( const child of this . _label . nativeElement . children ) {
424+ labelWidth += child . offsetWidth ;
425+ }
426+ this . _outlineGapStart = labelStart - containerStart - outlineGapPadding ;
427+ this . _outlineGapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2 ;
428+ } else {
429+ this . _outlineGapStart = 0 ;
430+ this . _outlineGapWidth = 0 ;
431+ }
432+ this . _changeDetectorRef . markForCheck ( ) ;
433+ }
434+
435+ /** Gets the start end of the rect considering the current directionality. */
436+ private _getStartEnd ( rect : ClientRect ) : number {
437+ return this . _dir && this . _dir . value === 'rtl' ? rect . right : rect . left ;
438+ }
397439}
0 commit comments